Laravel资源:: all包含来自另一个表的值
I'm learning Laravel right now and i have following tables and resources (models, controllers, ect.):
tickets
- id
- title
- projectID
- statusID
projects
- id
- title
status
- id
- title
I have to make a list of my Tickets on the Startpage. Not nessesary to say that i need the Project- and Statustiltles and not the IDs. Currently i do:
Route::get('/', function()
{
$tickets = Ticket::all();
return View::make('layout')->with('tickets', $tickets);
});
My current output is:
tickets->id, tickets->title, tickets->projectID, tickets->statusID
The output i want is
tickets->id, tickets->title, tickets->projects->title, tickets->status->title
So i hope anyone can understand what i'm trying to ask here and maybe provide me some help. Thank you!
Resolution: I had to set the foreign_keys first in my DB. Then i used the relationships mentioned in the answers and it works fine.
My Model:
class Ticket extends \Eloquent {
protected $fillable = [];
public function project()
{
return $this->hasOne('Project', 'id', 'projectID');
}
public function status()
{
return $this->hasOne('Status', 'id', 'statusID');
}
}
My View:
@foreach($tickets as $key => $value)
...
<td>{{ $value->project->title }}</td>
<td>{{ $value->status->title }}</td>
...
@endforeach
我正在学习Laravel,我有以下表和资源(模型,控制器等):
tickets
-id
-title
- projectID
-statusID
projects
-id
- title
status
-id
- title
code> pre>
我必须在“起始页”上列出我的故障单。 没有必要说我需要Project-和Statustiltles而不是ID。 目前我这样做: p>
Route :: get('/',function()
{
$ tickets = Ticket :: all();
return视图: :make('layout') - &gt; with('tickets',$ tickets);
});
code> pre>
我当前的输出是: p >
tickets-&gt; id,tickets-&gt; title,tickets-&gt; projectID,tickets-&gt; statusID
code> pre>
我想要的输出是 p>
tickets-&gt; id,tickets-&gt; title,tickets-&gt; projects-&gt; title,tickets-&gt; status-&gt; title
code> pre>
所以我希望任何人都能理解我在这里要问的内容,也许可以给我一些帮助。 谢谢! p>
解决方案 strong>:我必须先在我的数据库中设置foreign_keys。 然后我使用了答案中提到的关系,它工作正常。 p>
我的模型: p>
class Ticket extends \ Eloquent {
protected $ fillable = [];
公共函数项目()
{
返回$ this-&gt; hasOne('Project','id','projectID');
}
public 功能状态()
{
返回$ this-&gt; hasOne('状态','id','statusID');
}
}
code> pre>
我的观点: p>
@foreach($ tickets as $ key =&gt; $ value)
...
&lt; td&gt; {{$ value- &gt; project-&gt; title}}&lt; / td&gt;
&lt; td&gt; {{$ value-&gt; status-&gt; title}}&lt; / td&gt;
...
@ endforeach
code> pre>
div>
If you configure you relationships correctly you can do that without problems using the Laravel Eager Loading feature, for example:
Eager Loading (Laravel docs)
Eager loading exists to alleviate the N + 1 query problem...
class Ticket extends Eloquent {
public function project()
{
return $this->belongsTo('Project', 'projectID', 'id');
}
public function status()
{
return $this->belongsTo('Status', 'statusID', 'id');
}
}
Now, just call the fields you want, for example:
foreach (Ticket::all() as $ticket)
{
echo $ticket->project->title;
echo $ticket->status->title;
}
Obs.: In your return object/array you can't see the relationships fields unless you do manual joins, etc. So, just configure your relationships and call the fields you want.
Sorry for my english
Define relationships specifying custom foreign keys (defaults would be status_id
and project_id
for your models):
// Ticket model
public function project()
{
return $this->belongsTo('Project', 'projectID');
}
public function status()
{
return $this->belongsTo('Status', 'statusID');
}
Then eager load related models:
$tickets = Ticket::with('project','status')->get();
// accessing:
foreach ($tickets as $ticket)
{
$ticket->status; // Status model with all its properties
$ticket->project; // Project model
}