[英]Using has many through relationship in Laravel
How can I get or display company column from contractors table to put it in my blade?如何从承包商表中获取或显示公司列以将其放入我的刀片中? I already try figured it out by using hasManyThrough relationship but it doesn't work since all example that I made reference to is not having the same flow with me.我已经尝试通过使用 hasManyThrough 关系来解决它,但它不起作用,因为我参考的所有示例都与我没有相同的流程。
contractors table承包商表
id
company
residentials table住宅表
id
contractor_id
buyers table买家表
id
residential_id
This is the Model这是模型
Contractor.php承包商.php
<?php
namespace 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');
}
}
If I do this kind of code, it will be $contractor->buyer.如果我执行这种代码,它将是 $contractor->buyer。 While I want it to be $buyer->contractor->company虽然我希望它是 $buyer->contractor->company
Buyer.php买家.php
<?php
namespace 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);
}
}
Residential.php住宅.php
<?php
namespace 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买家控制器.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]);
}
This is the view that I'm working on and there is no looping since this is a profile for buyers login.这是我正在处理的视图,没有循环,因为这是买家登录的配置文件。
index.blade.php index.blade.php
<h4 class="heading"><b><center>Buyer's Details</center></b></h4>
<ul class="list-unstyled list-justify">
<li><b>Buyer ID: </b>{{$buyer->buyer_id}}</li>
<li><b>Name: </b>{{$buyer->name}}</li>
<li><b>Address: </b>{{$buyer->address}}</li>
<li><b>Residence: </b>{{$buyer->residential->name}}</li>
<li><b>Phone Number: </b>{{$buyer->phone_no}}</li>
<li><b>Email: </b>{{$buyer->email}}</li>
<li><b>Contractor: </b>{{$buyer->contractor->company}}</li>
</ul>
Based on the above view code, I already did the the relationship for Residence: But I'm not able to do it for Contractor: I hope there is someone to help me.根据上面的查看代码,我已经为Residence做了关系:但是我无法为Contractor做:我希望有人帮助我。
try this尝试这个
then u can do那么你可以做
$buyer->contractor->company
to到
$buyer->residential->contractor->company
u have alredy added belongsTo
added to 2 model你已经添加了belongsTo
添加到2个模型
In controller在控制器中
$buyers = Buyer::where('user_id', '=', Auth::id())->first();
return view('buyers.index',['buyer'=>$buyers]);
then call in blade file然后调用刀片文件
$buyers->residential->contractor->company
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.