Photos are not being saved when i hit the POST route - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, August 3, 2023

Photos are not being saved when i hit the POST route

The issue i am having is that when i hit save all the data goes through besides the photos, i see the photos in the payload but they dont get saved, i have that same exact function materialSplit as a store method and the photos get saved fine but just in that materialSplit function only the photos arent being stored.

I tried changing the laravel code but no luck, also tried to append the photos to formData but no luck

I posted the laravel code below and Vue.js code under that .

Thank you

 public function materialSplit(Request $request){

        $validator = Validator::make($request->all(), [
            'newWeight' => 'required|gt:0',
            'go_to' => 'sometimes',
            'comment' => 'sometimes',
            'content' => 'sometimes',  
        ]);

        if ($validator->fails()) {
            return Response::json([
                'errors' => $validator->errors()
            ], 400);
        }
        $photos_data = [];
        $photos_names = '';
        $dt = Carbon::now();
        $inboundJob = InboundJob::find($request->job_id);
        $container = 0;
        if ($request->container_id) {
            $container = Container::find($request->input('container_id'));
        }
        $content = Material::find($request->input('content_id'));
        $name = Material::find($request->input('content_name'));
        
        $outbound = $inboundJob->outbound_materials()->create([
             // 'content_id' => $request->input('material'),
             'content' => $content->name,
             'content_id' => $content->id,
             'container_id' => ($container) ? $container->id : null,
             'job' => $inboundJob->jobname,
             'weight' => $request->input('newWeight'),
             'color' => $request->input('color'),
             'price' => $request->input('price'),
             'process' => $request->input('process'),
             'grossWeight' => $request->input('newWeight'),
             'photos' => $photos_names,
             'ToStock' => $request->boolean('stock'),
             'bin' => $request->input('bin') ? $request->input('bin') : '',
             'comment' => $request->input('commentSplit'),
             'date_entered' => $request->input('date_entered'),
             'updated_at'=> $request->input('updated_at'),
             'active' => true,
             'billed' => $request->boolean('billed'),
             'username' => $request->user()->username,
        ]);

        
        if ($request->hasFile('photos')) :
            foreach ($request->file('photos') as $key => $file) {

                $name = Carbon::now()->timestamp . $key. "_". $file->getClientOriginalName();               
                
                $encoded_name = rawurlencode($name);
                Image::make($file)->orientate()->resize(1333, 1000)->save(public_path('/images/outboundMaterials/'.$encoded_name));
               
        
                $photos_data[] = $name;
                if (!$key) {
                    $photos_names .= $name;
                } else {
                    $photos_names .= ';' . $name;
                }
                $outbound->images()->create([
                    'name' => $name,
                    'path' => "/images/outboundMaterials/{$encoded_name}",
                ]);
            }
            $outbound->photos = $photos_names;
            $outbound->save();
          
        endif;

        $outbound->load(['inboundJob']);
        return (new OutboundResource($outbound))->response()->setStatusCode(201);
    }```







async save() { this.errors = {};

     console.log( this.editedItem.images, this.editedItem.photos)
     //Add this new variables to to split them to a new record
    Object.assign(this.editedItem,
     {newWeight: Number(this.editedItemSplit.splitWeight)},
     {commentSplit: this.editedItemSplit.go_to},
    );

      try {
        let formData = new FormData();
       

        for (var key in this.editedItem) {
          
          if (this.editedItem[key] !== null) {
            formData.append(key, this.editedItem[key]);
          }

        }

           await this.$axios.$post(`/api/materialSplit`, formData).then((res) => {
           this.editedItem.photos = this.editedItem.photos
            if (res.data) 

            { console.log(res.data)
                this.loadingToApi = true;          
            }
          });
        
          
       await this.$axios.$post(`/api/materialUpdateSplit?weight=${this.editedItemSplit.weightLeft}&id=${this.material.id}`).then((res) => {
            if (res.data) {
              console.log(res.data)
             this.loadingToApi = false;
             this.editedItemSplit = Object.assign({}, this.defaultItemSplit);
             this.$emit("form-submit");
            }
          });   
   
      } catch (err) {
        console.log(err.response);
        this.errors = err.response.data.errors;
      }
      
       
    },


source https://stackoverflow.com/questions/76822617/photos-are-not-being-saved-when-i-hit-the-post-route

No comments:

Post a Comment