一对多关系中的图像更新/编辑

问题描述

我有两个具有一对多关系的Event和Eventimages模型,我可以上传链接一个事件的许多图像,但是问题是当我尝试通过更新链接到该事件的所有图像进行编辑时,只有一个图像是上传的图像替换了所有其他图像,因此我的问题是如何一次或单独更新与该事件相关的所有图像?请任何帮助将不胜感激。

这是我的活动模型

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

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

事件迁移

   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();
   });
  }

这是Eventimage模型

class Eventimage extends Model
{

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

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

}

Eventimage迁移

       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();
    });
  }

“查看”编辑页面

    <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>

这是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,$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,]);
   }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)