Adams_'s avatar

Images update/edit in one-to-many relationship

I have two model Event and EventImages with One-To-Many relationship, I can upload many images linked to one event but the problem is when I tried to edit by updating all images linked to that event, which one only image is uploaded replacing all the other images, so my question is how can I update all the images related to that event all at once or individually? Please any help will be much appreciated.

Here is my Event Model

    class Event extends Model
{
     protected $fillable = ['title', 'date', 'time', 'venue', 'body'];

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

Event Migration

   public function up()
  {
   Schema::create('events', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('date');
    $table->string('time');
    $table->string('venue');
    $table->mediumText('body');
    $table->timestamps();
   });
  }

This is EventImage Model

class EventImage extends Model
{

protected $fillable = ['images', 'caption','event_id'];

public function event()
{
   return $this->belongsTo('App\Event');
}

}

EventImage migration

       public function up()
   {
      Schema::create('event_images', function (Blueprint $table) {
       $table->id();   
       $table->string('images');
       $table->string('caption');
       $table->integer('event_id')->unsigned();
       $table->foreign('event_id')->references('id')->on('events')
       ->onDelete('cascade');
       $table->timestamps();
    });
  }

The View edit page

    <div class="container mt-5" style="width:700px">
  <h2>CREATE EVENT</h2>
  <form class="" action="{{ route('updateevent', $event->id)}}" method="post" 
  enctype="multipart/form-data">
  @csrf
  {{method_field('PUT')}}

    <div class="form-group">
      <label for="title">Title</label>
      <input class="form-control" type="text" name="title" value="{{$event- 
     >title}}">
    </div>

    <div class="form-group ">
    <label  class="" for="date">Date</label>
    <input class="form-control" id="date"  type="date" name="date" 
      placeholder="" value="{{$event->date}}">
    </div>

    <div class="form-group">
      <label for="time">Time</label>
      <input class="form-control" type="time" name="time" value="{{$event- 
      >time}}">
    </div>

    <div class="form-group">
      <label for="caption">Caption</label>
      <input  class="form-control" type="text" name="caption" value=" 
     {{$image->caption}}">
      </div>

      <div class="location-group">
      <label for="venue">Venue</label>
      <input class="form-control" type="text" name="venue" value="{{$event- 
     >venue}}">
    </div>

    <div class="form-group">
      <label for="body">Body</label>
      <textarea class="form-control" name="body" rows="8" cols="80">{{$event- 
     >body}}</textarea>
    </div>

    <div class="location-group">
      <label for="images">Uplada Image</label>
      <input multiple="multiple" class="form-control" type="file" 
     name="images[]" value="">
    </div>

      <input type="submit" name="submit" class="btn btn-primary" 
      value="Submit"/>
  </form>

This is the EventController

public function store(Request $request)
{


    //Handle File Upload
 if ($request->hasFile('images')) {

    $files = $request->file('images');
    // Get filename with extention 

    foreach($files as $file) {
    $filenamewithExt = $file->getClientOriginalName();
    // Get just filename
    $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
    // Get just Extention
    $extension = $file->getClientOriginalExtension();
    // Filename to store
    $filenameToStore = $filename.'_'.time().'.'.$extension;
    // Upload Image
    $path = $file->storeAs('public/images', $filenameToStore);
    }
} else {
    $filenameToStore = 'noimage.jpg';
}



// Create EventImages
 $event = Event::create($request->all());

   foreach($request->images as $image) {

      $images = $image->store('images');
      $filenamewithExt = $image->getClientOriginalName();
// Get just filename
      $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
// Get just Extention
      $extension = $image->getClientOriginalExtension();
// Filename to store
      $filenameToStore = $filename.'_'.time().'.'.$extension;
   EventImage::create([
    'event_id' => $event->id,
    'images'   => $filenameToStore,
    'caption'  => $request->caption,
    
  ]);
 }


public function update(Request $request, $id)
  {
            //Handle File Upload
 if ($request->hasFile('images')) {

    $files = $request->file('images');
    // Get filename with extention 

    foreach($files as $file) {
    $filenamewithExt = $file->getClientOriginalName();
    // Get just filename
    $filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
    // Get just Extention
    $extension = $file->getClientOriginalExtension();
    // Filename to store
    $filenameToStore = $filename.'_'.time().'.'.$extension;
    // Upload Image
    $path = $file->storeAs('public/images', $filenameToStore);
    }
} else {
    $filenameToStore = 'noimage.jpg';
}



// UPDATE EventImages
  $event = Event::find($id);
  $event->update($request->all());
  foreach($request->images as $image) {

$images = $image->store('images');
$filenamewithExt = $image->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
// Get just Extention
$extension = $image->getClientOriginalExtension();
// Filename to store
$filenameToStore = $filename.'_'.time().'.'.$extension;



$event->images()->update([
    'event_id' => $event->id,
    'images'   => $filenameToStore,
    'caption'  => $request->caption,
    
    ]);
   }
}
0 likes
1 reply

Please or to participate in this conversation.