所以我正在嘗試為一個專案上傳圖片,其中每個圖片都可以有自己的描述或標題。我正在嘗試通過專案創建表單來做到這一點。現在,一切正常,除了由于嵌套的 foreach 回圈,我在 project_images 表中得到重復的條目,請看一下代碼的總結結構,特別是 storProject 方法中的邏輯,如果有人可以提供幫助,我將永遠感激. 謝謝你。
我正在使用 Laravel 和 Livewire。
楷模
專案模型
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->integer('budget');
$table->string('proposed_by');
$table->string('executed_by');
$table->string('status')->nullable();
$table->integer('progress_indicator')->nullable();
$table->date('starting_date');
$table->date('completion_date')->nullable();
$table->string('cover_image')->nullable();
$table->mediumText('body')->nullable();
$table->timestamps();
});
}
Project_images 模型
public function up()
{
Schema::create('project_images', function (Blueprint $table) {
$table->id();
$table->string('images');
$table->string('caption')->nullable();
$table->UnsignedBigInteger('project_id');
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
$table->timestamps();
});
}
表格
<x-slot name="content">
<x-errors />
<div class="space-y-8 divide-y divide-gray-200 mt-10">
<form method="#" enctype="multipart/form-data">
@if ($projectImages)
Images Preview:
<div class="flex flex-row m-8 ">
@foreach ($projectImages as $key => $projectImage)
<div class="flex flex-col gap-3">
<img width="100%" src="{{ $projectImage->temporaryUrl() }}">
<x-input placeholder="Caption"
wire:model.defer="captions.{{ $key }}" />
</div>
@endforeach
</div>
@endif
</div>
<label hljs-number">2 text-gray-500">Upload
Projects Images</label>
<div hljs-string">">
<label
hljs-number">32 border-2 border-dashed hover:bg-gray-500 hover:border-gray-300">
<div hljs-number">7">
<p hljs-number">1 text-sm tracking-wider text-gray-400 group-hover:text-gray-600">
Select a photo</p>
</div>
<input wire:model="projectImages" type="file" multiple hljs-number">0" />
</label>
</div>
<x-textarea wire:model.lazy="body" label="Body" placeholder="write your article" />
</form>
</div>
</x-slot>
<x-slot name="footer">
@if ($projectId)
<div hljs-number">3 justify-end">
<x-button wire:click="CancelConfirmation" label="Cancel" flat />
<x-button type="submit" wire:click="updateProject" label="Update" wire:loading.attr="disabled"
primary />
</div>
@else
<div hljs-number">3 justify-end">
<x-button wire:click="CancelConfirmation" label="Cancel" flat />
<x-button type="submit" wire:click="storeProject" label="Create" wire:loading.attr="disabled"
primary />
</div>
@endif
</x-slot>
</x-jet-dialog-modal>
專案組件
public function storeProject()
{
$this->validate([
'title' => 'required',
'status' => 'required',
'budget' => 'required',
'proposedBy' => 'required',
'executedBy' => 'required',
'startingDate' => 'required',
'completionDate' => 'required',
'progressIndicator' => 'required',
'body' => 'required',
'image' => 'image|max:1024|nullable'
]);
$image_name = $this->image->getClientOriginalName();
$this->image->storeAs('public/photos', $image_name);
$project = new Project();
$project->user_id = auth()->user()->id;
$project->title = $this->title;
$project->status = $this->status;
$project->budget = $this->budget;
$project->proposed_by = $this->proposedBy;
$project->executed_by = $this->executedBy;
$project->starting_date = Carbon::create($this->startingDate);
$project->completion_date = Carbon::create($this->completionDate);
$project->progress_indicator = $this->progressIndicator;
$project->body = $this->body;
$project->cover_image = $image_name;
$project->save();
foreach ($this->projectImages as $projectImage) {
$image_name = $projectImage->getClientOriginalName();
$projectImage->storeAs('public/photos', $image_name);
foreach ($this->captions as $caption) {
$projectImages = new ProjectImages();
$projectImages->project_id = $project->id;
$projectImages->images = $image_name;
$projectImages->caption = $caption;
$projectImages->save();
}
}
$this->notification()->success(
$title = 'Success',
$description = 'Project Created Successfully'
);
$this->reset();
}
因此,您可以直觀地看到這里要執行的操作是表單的螢屏截圖

uj5u.com熱心網友回復:
我假設您只希望每個專案影像都有一個標題。如果是這種情況,那么嵌套的 foreach 回圈是不必要的,并且像這樣將其更改為一個 foreach 應該可以解決問題。
foreach ($this->projectImages as $key => $value) {
$image_name = $value->getClientOriginalName();
$value->storeAs('public/photos', $image_name);
$projectImages = new ProjectImages();
$projectImages->project_id = $project->id;
$projectImages->images = $image_name;
$projectImages->caption = $this->captions[$key];
$projectImages->save();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/490373.html
標籤:拉拉维尔 图片 嵌套循环 laravel-livewire 标题
