【发布时间】:2013-05-23 13:43:15
【问题描述】:
我想检索属于一个 author_id(在本例中为 author2)的所有任务详细信息(task_title 等)。
测试表
author_id task_id
author2 task_1
author2 task_2
任务表
task_id task_title
task_1 task_title_1
task_2 task_title_2
作者表
author_id author_name
author_2 authorTwo
模型测试.php
public function tasks()
{
return $this->belongsTo('Task','task_id');
}
TestsController.php
public function index()
{
$test=Test::find('author2')->tasks()->get();
return View::make('tests.index', compact('tests'));
}
和查询SQL:
select * from `tests` where `author_id` = 'author2' limit 1
select * from `tasks` where `tasks`.`task_id` = 'task1'
但实际上在任务表中,与作者 2 相关的值不止一个(在本例中为任务 1 和任务 2),但由于 sql 仅说明了任务 1。
如何取消限制 1 的限制以检索属于作者 2 的所有任务?
【问题讨论】:
标签: php laravel laravel-4 eloquent