gpt4 book ai didi

php - 不工作 Laravel 多对多关系

转载 作者:行者123 更新时间:2023-11-29 15:32:12 25 4
gpt4 key购买 nike

我尝试获取多对多关系中的数据,其显示NULL值,我使用php artisantinker来检查我的多对多关系也显示 Null Value

这是我的TenderMaster模型

  class TenderMaster extends Model
{
protected $table = 'hmcs_tender';
public $timestamps = false;
protected $primarykey = 'id';


public function Ports()
{
return $this->belongsToMany('App\Tender_Module\Masters\Air\AirPortCode','hmsc_bid_origin_port_comm','bid_id','port_id');
}
}

这是我的 AirPortCode 模型

class AirPortCode extends Model
{
Protected $table = 'hmsc_air_port_codes';
public $timestamps = false;
Protected $primaryKey = 'id';

public function Tenders()
{
return $this->belongsToMany('App\Tender_Module\Admin_Module\TenderMaster','hmsc_bid_origin_port_comm','port_id','bid_id');
}
}

这是我的 Blade.php,我使用此方法将数据获取到多对多关系{{$tenders->Ports['port_id']}}

 @foreach($tender as $key => $tenders)


<tbody>
<td>{{$tenders->bid_name}}</td>
<td>{{$tenders->bid_name}}</td>
<td>{{$tenders->bid_year}}</td>
<td>{{$tenders->shipping_mode}}</td>
<td>{{$tenders->start_date}}</td>
<td>{{$tenders->Ports['port_id']}}</td>
<td>{{$tenders->bid_name}}</td>
<td style="text-align:center">
<button class="btn btn-success btn-sm">View</button>
<button class="btn btn-warning btn-sm">Edit</button>
<button class="btn btn-primary btn-sm">Send</button>
<button class="btn btn-primary btn-sm">Quotation</button>
<button class="btn btn-danger btn-sm">Delete</button>

</td>
</tbody>
@endforeach

这是我的 table 。表名是 hmsc_tender 主键是 id enter image description here

这是我的多对多关系连接表,表名称是hmsc_bid_origin_port_comm enter image description here

最佳答案

尝试

 public function Ports()
{
return $this->belongsToMany('App\Tender_Module\Masters\Air\AirPortCode','hmsc_bid_origin_port_comm','port_id','bid_id');
}

public function Tenders()
{
return $this->belongsToMany('App\Tender_Module\Admin_Module\TenderMaster','hmsc_bid_origin_port_comm','bid_id','port_id');
}

关于php - 不工作 Laravel 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58622928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com