我想检索属于一个author_id的所有任务详细信息(task_title等)(在本例中为author2)。
测试表
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
模型test.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'
但实际上在tasks表中,有多个与作者2相关的值(在本例中为task 1和task2),但是sql只说明了task1。
如何删除限制1限制以检索属于作者2的所有任务?
答案 0 :(得分:3)
这有多个问题。主要是:
belongsToMany()
get()
与where()
结合使用。以下链接指向您开始使用的文档 - http://four.laravel.com/docs/eloquent
答案 1 :(得分:1)
Test::where("author_id","=","author2")->get()
顺便说一句,您不应该将find
用于任何非主键的事情。这是find的目的:它取一个项目,因为它假定它搜索的密钥是主要的自动增量(即唯一)