Я совершенно новичок в 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');
}
}
, это в моей истории заемщиков Page. Статус запроса работает отлично.
<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');
});
}