Laravel 以一对多关系更新/编辑图像
我有两个具有一对多关系的模型事件和事件图像,我可以上载链接到一个事件的多个图像,但问题是当我试图通过更新链接到该事件的所有图像进行编辑时,只上载一个图像,而不是所有其他图像,所以我的问题是,我如何一次或单独更新与该事件相关的所有图像?请帮忙,我们将不胜感激 事件迁移Laravel 以一对多关系更新/编辑图像,laravel,image,relationship,updates,one-to-many,Laravel,Image,Relationship,Updates,One To Many,我有两个具有一对多关系的模型事件和事件图像,我可以上载链接到一个事件的多个图像,但问题是当我试图通过更新链接到该事件的所有图像进行编辑时,只上载一个图像,而不是所有其他图像,所以我的问题是,我如何一次或单独更新与该事件相关的所有图像?请帮忙,我们将不胜感激 事件迁移 public function up() { Schema::create('events', function (Blueprint $table) { $table->id(); $tabl
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();
});
}
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();
});
}
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();
});
}
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();
});
}
这是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,
]);
}
}