samedi 23 mai 2020

Laravel: Trying to push url strings to array in database

I have a field in my AppSetup table called reference_images that is added as follows in the migration: $table->json('reference_images')->nullable(); and casted as an array in the AppSetup model. I have a method that adds image urls to the database. If it's for the reference_image field, I am trying to push it to the array but I keep getting an "Array to String conversion" error in Laravel.

   public function addImagesToAppSetup($imageType=null, $image=null)
    {
        $appSetup = AppSetup::where('store', '=', id())->first();

        if ($image) {
            if ($imageType == "reference_images") {
                $originalArray = $appSetup->$imageType;
                $originalArray[] = $image;
                $appSetup->$imageType = array_values(array_unique($originalArray));
            } else {
                $appSetup->$imageType = $image;
            }
        }

        $appSetup->save();

        return response()->json(['status' => 1, 'data' => $appSetup]);
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire