我正在尝试运行hasMany,但我真的不知道为什么我没有得到任何结果! 两个表:Company,Joborder,每个作业都有一个我想使用的laravel中的EmiratesTo关系获取的公司名称和地址
<?php
namespace App;
class Company extends Model
{
protected $table = 'company';
protected $primaryKey = 'company_id';
public function jobs() {
return $this->hasMany('App\Joborder');
}
}
我的工作订单页面
<?php
namespace App;
class Joborder extends Model
{
protected $table = "joborder";
public $primaryKey = "joborder_id";
protected $dates = [
'start_date',
'created_at',
'updated_at'
];
public function company(){
return $this->belongsTo('App\Company');
}
}
这是PageController:
public function joblist() {
$joblist = Joborder::all();
$newInstance = new Joborder;
$company = $newInstance->company();
dd($company->name);
dd($joblist);
return view('pages.joblist')->with('jobs',$joblist);
}
这是我页面运行后的第dd位
BelongsTo {#217 ▼
#foreignKey: "company_id"
#otherKey: "company_id"
#relation: "company"
#query: Builder {#218 ▶}
#parent: Joborder {#205 ▼
#table: "joborder"
+primaryKey: "joborder_id"
#dates: array:3 [▼
0 => "start_date"
1 => "created_at"
2 => "updated_at"
]
#guarded: array:1 [▼
0 => "id"
]
#fillable: []
#connection: null
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
+wasRecentlyCreated: false
}
#related: Company {#212 ▼
#table: "company"
#primaryKey: "company_id"
#guarded: array:1 [▼
0 => "id"
]
#fillable: []
#connection: null
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
+wasRecentlyCreated: false
}
}
我在company中有company_id作为pkey,在joborder中具有fkey 感谢您的帮助!
答案 0 :(得分:3)
您应该致电
$company = $newInstance->company()->get();
代替
$company = $newInstance->company();
在PageController:jobList
中。
现在,您正在通过关系数据本身来重现关系本身。
或者,您可以将company
作为属性来访问,该属性应自动返回相关数据,而不是关系本身:
$company = $newInstance->company;
答案 1 :(得分:1)
您需要在两个关系中同时指定foreignKey
和localKey
,因为您没有ID作为主键
公司模型
public function jobs() {
return $this->hasMany('App\Joborder','company_id','company_id');
}
工作模式
public function company(){
return $this->belongsTo('App\Company', 'company_id', 'company_id');
}
现在这样获取
public function joblist() {
$joblist = Joborder::with('company')->all();
dd($joblist);
return view('pages.joblist')->with($joblist);
}
如果您需要提取公司的选定字段,则可以在急切加载$query->select('name', 'address');
中指定它
public function joblist() {
$joblist = Joborder::with(['company' => function($query){
$query->select('name', 'address');
}])->get();
return view('pages.joblist')->with($joblist);
}
在刀片服务器中,您可以像这样访问公司数据
@foreach($joblist as $job)
@if($job->company)
{{$job->company->name}}
{{$job->company->address}}
@endif
@endforeach
答案 2 :(得分:0)
尝试类似的方法
public function joblist() {
$joblist = Joborder::with('company')->get();
return view('pages.joblist')->with('jobs',$joblist);
}
确保公司表中有工作清单的ID。
还要检查外键名称,否则您必须通过joborder_id
或任何外键名称:
public function company(){
return $this->belongsTo('App\Company', 'joborder_id');
}