好吧,我有两张表:(它们有更多的列,但这些是重要的)
项目 id PK AI customer_id(FK 客户)
客户 ID PK AI 名称
现在,在我的索引页面上,我想在表格中显示项目监听,在“客户”列中,我想显示客户的名称而不是 ID,因为 ID 对要查看的人没有任何意义桌子。
但由于某种原因,我无法让它工作。我已经用谷歌搜索并研究了如何在没有成功的情况下做到这一点..所以我希望我能在这里得到我做错了什么的答案。
这是我的设置:
项目.php
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model {
protected $table = 'projects';
protected $fillable = [
'customer_id', 'user_owner', 'user_project_leader' , 'name', 'description', 'status', 'offered_price',
'estimate', 'project_start', 'deadline', 'free_text', 'total_price', 'created_at', 'updated_at'
];
public function customer()
{
return $this->hasOne('Customer');
}
}
客户.php
<?php namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model {
protected $table = 'customers';
protected $fillable = [
'name', 'reference_person', 'created_at', 'updated_at'
];
}
项目控制器.php
public function index(){
$projects = Project::with('customer')->get();
return view('projects.index', compact('projects'));
}
最后,我的 index.php
@foreach($projects as $project)
<tr>
<td>{{ $project->name }}</td>
<td>{{ $project->user_owner }}</td>
@foreach($project->customer as $pcustomer)
<td>{{ $pcustomer->name }}</td>
@endforeach
<td>{{ $project->deadline }}</td>
<td>{{ $project->offered_price }}</td>
<td> 00h</td>
</tr>
@endforeach
我得到的错误是“找不到类客户”。当它清楚地存在于我的文件夹中并用于其他目的时。