mercredi 31 mars 2021

Unable To Add Photos in Table By Using Morphmany relation Laravel any help would be apricated

this Is the Model which I am using for inserting photos in Photos Table by use of MorphMany Realtions in laravel in Object Model

public function photos(){ return $this->morphMany('App\Photo', 'photoable');}

photable function in Photo model

 public function photoable(){

return $this->morphTo();

}

form for adding photos.

<input type="file" name="objectPictures[]" id="objectPictures" multiple><br>

here I am inserting the Object in objects table along with Photos in Photos table. Object inserted successfully but photos are not inserting.

public function saveObjectby(Request $request)

{

     $inputs=request()->validate([

       'name'=> 'required|min:8|max:255',
        'description'=> 'required',
      ]);

      $object = new TouristObject();

                          $object->user_id = $request->user()->id;
                          $object->name = $request->input('name');
                          $object->package_id = $request->input('package_id');
                          $object->description = $request->input('description');
                          $object->save($inputs);
                          

                       if ($request->hasFile('objectPictures'))
                       {

                  $this->validate($request, \App\Photo::imageRules($request,'objectPictures')); 
                         foreach($request->file('objectPictures') as $picture)
                          {

                            $path = $picture->store('objects', 'public');

                                    $photo = new Photo;
                                    $photo->path = $path;
                                    $object->photos()->save($photo);

                               }

                             }

                        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire