У меня две модели, и обе имеют отношение «один ко многим» с внешним ключом VehiclesBrand
, и всякий раз, когда я хочу получить свойство Tblbrand
, используя модель транспортного средства, например {{$vehicle->brand->BrandName }}
, возникает ошибка, и то же самое, когда я вывод с помощью функции dd () он возвращает истинный ответ, а также, когда я выводю Tblvehicle::all()
, он возвращается с пустым массивом отношений, я не знаю почему.
1.App \ Models \ Tblbrand
2.App \ Models \ Tblvehicle
Модель Tblbrand:
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class Tblbrand
*/
class Tblbrand extends Model
{
protected $table = 'tblbrands';
public $timestamps = false;
protected $primaryKey = 'id';
protected $dates = [
'CreationDate',
'UpdationDate'
];
protected $fillable = [
'BrandName',
'CreationDate',
'UpdationDate'
];
public function vehicle(){
return $this->hasMany('App\Models\Tblvehicle','VehiclesBrand');
}
}
Модель Tblvehicle:
<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class Tblvehicle
* @package App\Models
*/
class Tblvehicle extends Model
{
protected $table = 'tblvehicles';
public $timestamps = false;
protected $casts = [
'VehiclesBrand' => 'int',
'PricePerDay' => 'int',
'ModelYear' => 'int',
'SeatingCapacity' => 'int',
'AirConditioner' => 'int',
'PowerDoorLocks' => 'int',
'AntiLockBrakingSystem' => 'int',
'BrakeAssist' => 'int',
'PowerSteering' => 'int',
'DriverAirbag' => 'int',
'PassengerAirbag' => 'int',
'PowerWindows' => 'int',
'CDPlayer' => 'int',
'CentralLocking' => 'int',
'CrashSensor' => 'int',
'LeatherSeats' => 'int'
];
protected $dates = [
'RegDate',
'UpdationDate'
];
protected $fillable = [
'VehiclesTitle',
'VehiclesBrand',
'VehiclesOverview',
'PricePerDay',
'FuelType',
'ModelYear',
'SeatingCapacity',
'Vimage1',
'Vimage2',
'Vimage3',
'Vimage4',
'Vimage5',
'AirConditioner',
'PowerDoorLocks',
'AntiLockBrakingSystem',
'BrakeAssist',
'PowerSteering',
'DriverAirbag',
'PassengerAirbag',
'PowerWindows',
'CDPlayer',
'CentralLocking',
'CrashSensor',
'LeatherSeats',
'RegDate',
'UpdationDate'
];
public function brand(){
return $this->belongsTo('App\Models\Tblbrand','id');
}
public function booking(){
return $this->hasMany('\App\Models\Tblbooking');
}
}
carrental.index.blade. php :
@foreach ($vehicles as $vehicle)
<div class="col-list-3">
<div class="recent-car-list">
<div class="car-info-box">
<a href="{{route('vehicles-details',$vehicle->id) }}">
<img src="{{asset('admin/img/vehicleimages')}{{$vehicle->Vimage1 }}" class="img-responsive" alt="image">
</a>
<ul>
<li>
<i class="fa fa-car" aria-hidden="true"></i>{{ $vehicle->FuelType }}
</li>
<li>
<i class="fa fa-calendar" aria-hidden="true"></i>{{ $vehicle->ModelYear}} Model
</li>
<li>
<i class="fa fa-user" aria-hidden="true"></i>{{ $vehicle->SeatingCapacity }} seats
</li>
</ul>
</div>
<div class="car-title-m">
<h6><a href="{{ route('vehicles-details',$vehicle->id) }}" >{{$vehicle->brand->BrandName}}{{ $vehicle->VehiclesTitle }}</a></h6>
<span class="price">Rs {{ $vehicle->PricePerDay }}/Day</span>
</div>
<div class="inventory_info_m">
<p>{{ $vehicle->VehiclesOverview }}</p>
</div>
</div>
</div>
@endforeach