

我在 App\Models 中的模型,我添加了 company_id 和 year 作為主鍵
class CompanyMasterCuti extends Model
{
use HasFactory;
protected $table = 'company_master_cuti';
protected $fillable = [
'company_id', 'year', 'cuti', 'created', 'created_by', 'modified', 'modified_by',
];
protected $guarded = [];
protected $keyType = 'string';
public $timestamps = false;
protected $primaryKey = ['company_id', 'year'];
public $incrementing = false;
public function company() {
return $this->belongsTo('App\Models\Company', 'company_id', 'id');
}
}
我在控制器中的代碼
public function show($company_id, $year) {
$master_cuti = CompanyMasterCuti::where('company_id', $request->company_id)->where('year', $request->year)->first();
return view('master-cuti.show', compact('master_cuti'));
}
我在 index.blade.php 中的代碼指向路由顯示
@forelse($master_cuti as $m_cuti)
<a href="{{ route('master-cuti.show', [$m_cuti->company_id, $m_cuti->year] ) }}">
<i class="badge-circle badge-circle-light-secondary bx bxs-show font-medium-1 text-success"></i>
</a>
@endforelse
我在 show.blade.php 中的代碼
<div class="card-content">
<div class="card-body">
<ul class="list-group p-2" style="padding-top: 5px !important">
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="mr-3">Tahun</span>
<span>{{ $master_cuti->year }}</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="mr-3">Cuti</span>
<span>{{ $master_cuti->cuti}}</span>
</li>
</ul>
</div>
</div>
路線
Route::resource('master-cuti', CompanyMasterCutiController::class);
我想顯示基于 company_id 和 year 的資料,這是 table company_master_cuti 的主鍵。我的代碼不正確,因為我試圖從控制器回傳資料,但資料為空,我的代碼有什么問題?我的問題有什么解決方案嗎?
uj5u.com熱心網友回復:
Laravel 不支持復合主鍵,請在此處查看。如果您想要一個復合主鍵,您需要手動完成所有作業。
我建議將其id作為主鍵,并將company_id, year對定義為unique鍵。
您仍然可以忽略id作為主鍵并使用company_id, year對作為主鍵,但正如我提到的那樣,這將是手動作業。檢查路由和控制器更改。
Route::get('master-cuti/{company_id}/{year}/show', CompanyMasterCutiController::class)->name('master-cuti.show');
public function show($company_id, $year) {
$master_cuti = CompanyMasterCuti::where('company_id', $company_id)->where('year', $year)->first();
return view('master-cuti.show', compact('master_cuti'));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448539.html
下一篇:Can'tphpartisanserve:failedtoopenstream:Nosuchfileordirectoryin/home/online21
