我正在引用http://culttt.com/2014/03/17/eloquent-tricks-better-repositories/,我在非对象上调用了带有()的成员函数。错误来自 AbstractRepository类。
public function make(array $with = array())
{
return $this->model->with($with);
}
这里是我的SpeakerRepository接口。
<?php namespace Amh\Storage;
interface SpeakerRepository
{
public function all();
public function find($id);
public function create($input);
public function update($input);
public function delete($id);
}
<?php namespace Amh\Storage;
use Illuminate\Database\Eloquent\Model;
use App\Models\Speaker as Speaker;
use File;
use Input;
use Amh\FileService\Photo\File as ImageFile;
class EloquentSpeakerRepository extends AbstractEloquentRepository implements SpeakerRepository
{
protected $speaker;
protected $file;
public function __construct(ImageFile $file,Speaker $speaker)
{
$this->file = $file;
$this->speaker = $speaker;
}
public function create($input)
{
$imageName = $this->file->createImageName($input['image']);
try
{
$this->file->store($input['image'],$imageName);
$input = array('name'=>$input['name'],
'image'=> 'photos/'.$imageName,
'desc'=>$input['desc']
);
return Speaker::create($input);
}
catch(Exception $e)
{
$this->file->delete($imageName);
echo $e->getMessage();
}
}
public function update($input)
{
$speaker = $this->find($input['id']);
$oldFile = $speaker->image;
$imageName = $this->file->createImageName($input['image']);
try
{
$this->file->store($input['image'],$imageName);
$speaker->name = $input['name'];
$speaker->image = 'photos/'.$imageName;
$speaker->desc = $input['desc'];
$speaker->save();
$this->file->delete($oldFile);
return true;
}
catch(Exception $e)
{
$this->file->delete($imageName);
echo $e->getMessage();
}
}
/**
* Delete speaker information from database and speaker's image from the specified location
*/
public function delete($id)
{
$speaker = $this->find($id);
$filePath = $speaker->image;
$this->file->delete($filePath);
return $speaker->delete();
}
}
<?php namespace Amh\Storage;
use Illuminate\Database\Eloquent\Model;
abstract class AbstractEloquentRepository
{
protected $model;
public function __construct()
{
}
public function all(array $with = array())
{
$entity = $this->make($with);
return $entity->get();
}
public function find($id, array $with = array())
{
$entity = $this->make($with);
return $entity->find($id);
}
public function getFirstBy($key, $value, array $with = array())
{
return $this->make($with)->where($key, '=', $value)->first();
}
public function getManyBy($key, $value, array $with = array())
{
return $this->make($with)->where($key, '=',$value)->get();
}
public function make(array $with = array())
{
return $this->model->with($with);
}
public function has($relation, array $with = array())
{
$entity = $this->make($with);
return $entity->has($relation)->get();
}
}
<?php namespace Amh\Storage;
use App\Models\Speaker;
use Reference;
use Illuminate\Support\ServiceProvider;
use Amh\FileService\Photo\File as ImageFile;
class StorageServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('Amh\Storage\SpeakerRepository','Amh\Storage\EloquentSpeakerRepository');
$this->registerSpeakerRepository();
}
public function registerSpeakerRepository()
{
$this->app->bind('SpeakerRepository',function()
{
return new EloquentSpeakerRepository($this->app->make('Amh\FileService\Photo\File'),new EloquentSpeakerRepository);
}
);
}
}
这里的问题是谁?感谢..