jeudi 11 avril 2019

Get Foreign key data in laravel

I have two tables room and room_images. In room_images table, we have multiple images of one room. Now i want to show those images in room edit blade template.


    rooms table:   | room_image table:
    id (PK)        | id
    title          | room_id (FK)
    desc           | room_image


Room Edit Controller

`public function edit($id)
 { 
    $room = Room::find($id);
    if(auth()->user()->id !== $room->user_id){
        return redirect('/rooms')->withStatus(__('Unauthorized Page'));
    }
    return view('rooms.edit')->with('room' , $room);
 }`

Room Modal: Room.php

`public function user()
 {
    return $this->belongsTo('App\User');
 }
 public function tbl_roomimages()
 {
    return $this->hasMany('App\RoomImage');
 }`

Room Image Modal: RoomImage.php

`public function tbl_rooms()
 {
   return $this->belongsTo('App\Room','room_id');
 }`

Here is the database column pictures: Room images tbl

Room tbl



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire