问题描述
我有两个具有一对多关系的Event和Eventimages模型,我可以上传链接到一个事件的许多图像,但是问题是当我尝试通过更新链接到该事件的所有图像来进行编辑时,只有一个图像已上传以替换所有其他图像,因此我的问题是如何一次或单独更新与该事件相关的所有图像?请给予任何帮助。
事件迁移
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迁移
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,$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 (将#修改为@)