【发布时间】:2020-01-10 13:21:21
【问题描述】:
我对 Laravel 完全陌生。
有人可以指导我如何在两个模型BorrowersRequest 和Cars 之间建立关系吗?
我已经在请求状态上完成了它,它也是BorrowersRequest 的外键,它完全可以工作。现在我在想为什么它不适用于Cars。
型号
class BorrowerRequest extends Model
{
public $timestamps = false;
public function requestStatus() {
return $this->belongsTo('App\RequestStatus');
}
public function requestedCar() {
return $this->belongsTo('App\Car');
}
}
这在我的 borrowershistory 页面中。请求状态运行良好。
<tr>
<td><p class="text-muted"><small>Car to be Rented:</small></td>
<td><h6>{{ $borrower_request->requestedCar->car_name }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Date of Return:</small></td>
<td><h6>{{ $borrower_request->return_date }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Request Status:</small></td>
<td><h6><em>{{ $borrower_request->requestStatus->request_status }}</em>
</h6></td>
</tr>
这是我的控制器
public function viewBorrowManager()
{
$borrower_requests = BorrowerRequest::all();
$request_statuses = RequestStatus::all();
return view('/borrowmanager', [
'borrower_requests' => $borrower_requests,
'request_statuses' => $request_statuses
]);
}
和我的迁移
public function up()
{
Schema::create('borrower_requests', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('car_id');
$table->timestamps();
$table->string('borrowers_name', 50);
$table->string('email')->unique();
$table->bigInteger('contact_number');
$table->date('return_date');
$table->unsignedBigInteger('request_status_id')->default(0);
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('car_id')->references('id')->on('cars');
$table->foreign('request_status_id')->references('id')->on('request_statuses');
});
}
【问题讨论】:
-
有错误吗?
-
嗨,是的,我很抱歉我忘了包括。它说尝试获取非对象的属性“car_name”(查看:C:\Users\Ethel\Documents\zuitte\CSP2\resources\views\borrowershistory.blade.php)
标签: php laravel laravel-5 eloquent orm