For starters, look at $build->update(['image' => $profileImage]); The image attribute is in the BuildImage model, not the Build model. You'll have to either create a build image instance with that inage, or find an existing one and update it. Also, what library are you using for resizing and saving the image? That's the important part in storing the images and you haven't provided what you are using for that
Jan 18, 2023
10
Level 6
update form one to many data
Hello / Each post has several images and these images are connected in another table with a one-to-many relationship. I upload the image with this form. It has two problems: -If we select a number of images, half of them will be uploaded.
- Uploaded but not saved. please check it.
models
class Build extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'title',
'desc',
'wall',
'sailing',
'adres',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function buildimages()
{
return $this->hasMany(Buildimages::class);
}
and
class Buildimages extends Model
{
use HasFactory;
protected $fillable = [
'build_id',
'image'
];
public function build()
{
return $this->belongsTo(Build::class);
}
}
Edit Form
<form action="{{ route('housebuild.update',$items->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<input type="text" name="title" class="form-control" value="{{ $items->title }}">
<input type="text" name="wall" class="form-control" value="{{ $items->wall }}">
<input type="text" name="sailing" class="form-control" value="{{ $items->sailing }}">
<input type="text" name="adres" class="form-control" value="{{ $items->adres }}">
<textarea id="editor" type="text" name="desc" class="form-control" rows="10">
{{ $items->desc }}
</textarea>
<input type="file" name="images[{{ $items->build_id }}]" id="inputImage" multiple
class="form-control @error('images') is-invalid @enderror">
<x-BtnSend/>
</form>
update controller
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'title' => 'required',
'desc' => 'required',
'wall' => 'required',
'sailing' => 'required',
'adres' => 'required',
'images' => 'array',
'images.*' => ['nullable', 'image']
]);
$build = Build::findOrFail($id);
foreach ($request->file('images') as $imageId => $image) {
$destinationPath = 'image/house/build/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$resize = Image::make($image);
$resize->resize(500, 300);
$resize->save($destinationPath . $profileImage);
$build->update(['image' => $profileImage]);
}
$build->update(Arr::except($validatedData, 'images'));
return redirect()->route('housebuild.index');
}
Level 104
@lovertohelp really?
$build->update(['image' => $profileImage]);
How many times have I said that the image is not on Build model; it is on the related BuildImage model:
foreach ($request->file('images') as $imageId => $image) {
$destinationPath = 'image/house/build/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$resize = Image::make($image);
$resize->resize(500, 300);
$resize->save($destinationPath . $profileImage);
// find the BuildImage instance (assuming $imageId is the ID of the BuildImage)
$buildImage = BuildImage::find($imageId);
$buildImage->update(['image' => $profileImage]);
}
1 like
Please or to participate in this conversation.