好的,我有两个表:(它们有更多的列,但这些是重要的)
项目id PK AI customer_id (FK客户)
客户id PK AI名称
现在,在我的索引页面上,我想在一个表和" customer“列中显示项目侦听,我想显示的是客户的名字,而不是ID,因为ID对将要查看表的人没有任何意义。
但出于某种原因,我无法让它开始工作。我已经搜索并研究了如何在没有success..so的情况下做到这一点,我希望我能在这里得到一个关于我做错了什么的答案。
这是我的设计:
Project.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');
}
}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'
];
}ProjectsController.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我得到的错误是“未找到类客户”。当它明显地在我的文件夹中,并用于其他用途时。
发布于 2014-10-23 15:53:34
首先,确保运行composer dump-autoload。
第二件事-我认为你需要用:
return $this->hasOne('\App\models\Customer');在你的关系里
https://stackoverflow.com/questions/26530257
复制相似问题