Home > Back-end >  How to display data from 2 tables in Laravel?
How to display data from 2 tables in Laravel?

Time:10-06

Restaurant Model

class Restaurant extends Model
{
    use HasFactory;
    public $timestamps = false;

    public function menus(){
        return $this->hasMany(Menu::class, 'resto_id');
    }
    public function restoimages(){
        return $this->hasOne(RestoImage::class, 'resto_id');
    }
}

RestoImage Model

class RestoImage extends Model
{
    
    use HasFactory;
    public $timestamps = false;
    protected $fillable = ['image','resto_id'];
    public function restaurants(){
        return $this->belongsTo(Restaurant::class, 'resto_id');
    }

}

View File

<div class="card mb-4">
<div class="card-body">
    <table id="datatablesSimple">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Image</th>
                <th>Address</th>
                <th colspan="2">Operations</th>
            </tr>
        </thead>
        <tbody>
        @foreach($data as $item)
            <tr>
                <td>{{$item->id}}</td> 
                <td>{{$item->name}}</td> 
                <td>{{$item->email}}</td>
                <td>{{$item->restoimages->image}}</td>
                <td>{{$item->address}}</td> 
                <td><button><a href="{{route('editresto', $item->id)}}">EDIT</a></button></td>           
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
</div>

RestoController

public function list(){
        $data = Restaurant::with('restoimages')->get();
        $restoimages = RestoImage::all();
        return view('list', compact('data', 'restoimages'));
    }

I have 2 tables, one is restaurants and other is resto_images. When an admin inserts new restaurant using a form. Record will be inserted in aboce 2 tables. resto_images table has column resto_id but when I tried to display image from resto_images table, it throws error (Attempt to read property "image" on null). Please correct me if I am wrong. Thanks in advance.

CodePudding user response:

in your view, you can use the optional function of Laravel or check that there is real value in your relationship with a if,

    <td>{{ optional($item->restoimages)->image}}</td>

or

    <td>{{ $item->restoimages ? $item->restoimages->image : null }}</td>

Dont forget the img tag if you want to show the image and css attribute to resize the image.

CodePudding user response:

If your images are directly in the public folder, then :

<td style="width:150px;height:100px;"> //some style to resize images
  @if ($item->restoimages)
     <img src="{{ asset($item->restoimages->image) }}" alt="">
  @else
     <p>no image</p>
  @endif
</td>

But if your images are in public/folderName, then :

<td style="width:150px;height:100px;">
  @if ($item->restoimages)
     <img src="{{ asset('folderName/' . $item->restoimages->image) }}" alt="">
  @else
     <p>no image</p>
  @endif
</td>
  • Related