Lara_Love's avatar

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');
    }
0 likes
10 replies
litvinjuan's avatar

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

1 like
tykus's avatar
tykus
Best Answer
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
Lara_Love's avatar

hello Mr @tykus

Call to a member function update() on null

in this

            $buildImage->update(['image' => $profileImage]);
Snapey's avatar

look at how you name the images. You can only store one image per second. a second image saved in the same second will overwrite the first?

1 like
Lara_Love's avatar

in create is correct form create


<form action="{{ route('housebuild.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
  
                <input type="text" name="title" >
            
                <input type="text" name="wall" >
           
                <input type="text" name="sailing" >
           
                <input type="file" name="images[]" id="inputImage" multiple class="form-control @error('images') is-invalid @enderror">
             
                <input type="text" name="adres" >
            
                <textarea id="editor"  name="desc"></textarea>
            
        <x-BtnSend/>
</form>

controller create post

public function store(Request $request)
    {
        $request->validate([
            'title'=> 'required',
            'desc'=> 'required',
            'wall'=> 'required',
            'sailing'=> 'required',
            'adres'=> 'required',
        ]);
        $input = $request->all();
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image)
            {
                $destinationPath = 'image/house/build/';
                $profileImage = time().rand(1,99).'.'.$image->extension();
                $resize = \Intervention\Image\ImageManagerStatic::make($image);
                $resize->resize(500, 300);
                $resize->save($destinationPath . $profileImage);
                $images[]= $profileImage;
            }}
        $input['user_id'] = Auth::id();
        $builder = Build::create($input);
        foreach ($images as $path) {
            $builder->buildimages()->create(['image' => $path] );
        }
        return redirect('housebuild');

    }

but edit not work / Can't you write the update like the store controller? it now upload image but not store in database

 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);
            $images[]= $profileImage;
//            $build->update(['image' => $profileImage]);
        }
        $build->update(Arr::except($validatedData, 'images'));
        return redirect()->route('housebuild.index');
    }
Lara_Love's avatar

Hello Mr @tykus . I was without internet πŸ˜” . Now designing the image ☺️ . What are you doing? I hope you are well and cheerful 🀎

Lara_Love's avatar

@tykus Sorry, I did not remember. happy New Year . I hope it will be a year full of money with happiness and health for you and your family

Please or to participate in this conversation.