I tried doing this for a website but my 'hotel' table has both an 'id' and a 'hotel_id' column. I heard eloquent recognizes the associated table name with _id as the primary key when defining a relationship. How would I define my relationship with the comments to the hotel? I have tried this in my Hotel model
public function comments() { return $this->hasMany(Comments::class, 'hotel_id') }
this is my controller for the page
public function hotelPage($hotel_id)
{
$hotels = Hotel::select(
'hotel.hotel_id'
,'hotel.name'
,'hotel.photo_url'
,'hotel.address'
,'hotel.desc_en'
//,'hotel.hotel_url'
,DB::raw("(CASE WHEN
COALESCE(hotel.hotel_url,'')!='' THEN
LOWER(CONCAT(hotel.hotel_url,'?aid=*******'))
ELSE
CONCAT(hotel.hotel_url,'http://ift.tt/2rxkBS1')
END)
as hotel_url"))
->where('hotel.hotel_id',$hotel_id)
->get();
return view('hotel.hotelPage', ['hotels' => $hotels]);
}
and when I load the view
@foreach($hotels->comments as $comment) @endforeach
I get the error "Property [comments] does not exist on this collection instance./ / /hotelPage.blade.php"
Please help, I am using laravel 5.4
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2qdpUlG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire