vendredi 26 juin 2020

Delete image in laravel through model relationship

.Multiplae images are stored from another model but i can not delete the images one by one. messedup in eloquent. how to delete the image with this model relationship

This is my product model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['title','image'];

    public function images(){
      return $this->hasMany('App\ProductImage');
    }
}

this is product image model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductImage extends Model
{
    public function product(){
      return $this->belongsTo('App\Product');
    }
}

This is view with delete link

@foreach($product->images as $image)
 <form class="product_image" action="" method="post">
@csrf
@method('DELETE')
    <img style="width:100%; height:100%;" src="">
        <button class="product_image_delete" type="submit">
           <i class="fa fa-trash" aria-hidden="true"></i>
         </button>
 </form>
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire