您好我正在使用自定义存储库,我很乐意查询一个表来检索数据,如下所示:
public function getAll()
{
// get all logged in users projects order by project name asc and paginate 9 per page
return \Auth::user()->projects()->orderBy('project_name', 'ASC')->paginate(9);
}
在我的控制器中我只需拨打
public function __construct(ProjectRepositoryInterface $project) {
$this->project = $project;
}
public function index()
{
$projects = $this->project->getAll();
echo View::make('projects.index', compact('projects'));
}
我的观点如下:
@if (Auth::check())
@if (count($projects) > 0)
@foreach ($projects as $project)
{{ $project->project_name }}
@endforeach
@else
<p>No records, would you like to create some...</p>
@endif
{{ $projects->links; }}
@endif
但是在我的项目表中,我有一个status_id和一个client_id,我想用登录用户检索这些表的记录,但我不知道如何构建我的查询,是否有人有任何指导?
答案 0 :(得分:1)
根据laravel [documentation] http://laravel.com/docs/eloquent#relationships,在项目模型中,您可以添加以下函数:
class Project extends Eloquent {
public function clients()
{
return $this->hasMany('Client');
}
}
在您的客户端模型中,您可以添加与函数关系的反转:
class Client extends Eloquent {
public function project()
{
return $this->belongsTo('Project');
}
}
然后,您可以使用以下函数检索数据:
$clients = Project::find(1)->clients;