如何从承包商表中获取或显示公司列并将其放入我的刀片中?我已经尝试通过使用 hasManyThrough 关系来解决这个问题,但它不起作用,因为我引用的所有示例都与我没有相同的流程。承包商表id
company住宅表id
contractor_id买家表id
residential_id这是模型承包商.php<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Contractor extends Model{ protected $table = 'contractors'; protected $fillable = ['user_id','company','address','phone_no','email','avatar']; public function residential() { return $this->hasMany(Residential::class); } public function buyers() { return $this->hasManyThrough('App\Buyer', 'App\Residential'); } public function buyer() { return $this->hasOneThrough('App\Buyer', 'App\Residential'); }}如果我执行这种代码,它将是$contractor->buyer。虽然我希望它是 $buyer->contractor->company买家.php<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Buyer extends Model{ protected $fillable = ['buyer_id', 'name', 'address', 'phone_no', 'email', 'avatar', 'user_id', 'residential_id']; public function residential() { return $this->belongsTo(Residential::class); }}住宅.php<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Residential extends Model{ protected $fillable = ['name','res_code','postcode','city','state','image','contractor_id']; public function contractor() { return $this->belongsTo(Contractor::class); } public function buyer() { return $this->hasMany(Buyer::class); }}BuyerController.php public function index(Request $request) { if(Auth::user()->role == 'buyer') { $buyers = Buyer::where('user_id', '=', Auth::id())->first(); return view('buyers.index',['buyer'=>$buyers]); }这是我正在处理的视图,并且没有循环,因为这是买家登录的个人资料。根据上面的查看代码,我已经为Residence做好了关系:但我无法为Contractor做这件事:我希望有人帮助我。
2 回答
当年话下
TA贡献1890条经验 获得超9个赞
尝试这个
那么你可以做
$buyer->contractor->company
到
$buyer->residential->contractor->company
你已经添加belongsTo
了 2 个模型
潇潇雨雨
TA贡献1833条经验 获得超4个赞
在控制器中
$buyers = Buyer::where('user_id', '=', Auth::id())->first(); return view('buyers.index',['buyer'=>$buyers]);
然后调用blade文件
$buyers->residential->contractor->company
- 2 回答
- 0 关注
- 107 浏览
添加回答
举报
0/150
提交
取消