skip to Main Content

I want to save images in db like:

{
    "1":"accounts/29cc60f2-c018-403c-80a9-94cda509e0f7.jpeg",
    "2":"accounts/c2658592-526a-426e-9834-084e0252c4c5.jpeg",
    "3":"accounts/3db1d928-e32f-4f3e-b41e-c99ffefec952.jpeg",
    "4":"accounts/4d421a36-dc7e-4252-b806-17467a515aa7.jpeg"
}

but in response just recieve one data in output in db like :
["property/March2023/535856080.png"]

in my code :
in model :

    protected $fillable
        = [
            'images',
        ];
    protected $casts
        = [
            'images' => 'array',
        ];

in controller :

        $image_exists = $request->file('images');

        if ($image_exists !== Null) {
                    $images = array();
            $date = now()->format('F') . now()->format('Y');
            if ($files = $request->file('images')) {
                foreach ($files as $file) {
                    $name = rand() . '.' . $file->getClientOriginalExtension();
                    $file->store('image', 'public');
                    $images[] = 'property/' . $date . '/' . $name;
                }
            }
            dd($images);
            $property = DB::table('re_properties')->orderBy('id', 'DESC')->first();

            DB::table('re_properties')->where('id', $property->id)->update([

                'images' => $images,
            ]);
        } else {

        }

2

Answers


  1. you must first use implode() to save the images and save like in

    "property/March2023/2061010485.png,property/March2023/1949282764.png,property/March2023/1065750317.png"
    

    Try this:

    DB::table('re_properties')->where('id', $property->id)->update([
            'images' => implode(",", $images),
        ]);
    

    and for get images use explode() and get data like in

    [
      "property/March2023/2061010485.png",
      "property/March2023/1949282764.png",
      "property/March2023/1065750317.png"
    ]
    

    Try this:

    $images = explode(",", DB::table('re_properties')->where('id', $property->id)->first()->images);
    
    Login or Signup to reply.
  2. When you save data, you can use serialize() to convert array to string & also use base64_encode() to encode data.

    $data = [
        "accounts/29cc60f2-c018-403c-80a9-94cda509e0f7.jpeg",
        "accounts/c2658592-526a-426e-9834-084e0252c4c5.jpeg",
        "accounts/3db1d928-e32f-4f3e-b41e-c99ffefec952.jpeg",
        "accounts/4d421a36-dc7e-4252-b806-17467a515aa7.jpeg"
    ];
    
    $images = base64_encode(serialize($data));
    Re_properties::create([
      'images' => $images,
    ]);
    
    
    /**
     * FOR CORE PHP
     * ____________
     * $string = base64_encode(serialize($data));
     * $sql = "INSERT INTO tb1 (col1) VALUES ('".$string."')";
    */
    
    

    When you fetch data, you can us unserialize() & base64_decode() to decode data & convert string to array.

    
    $re_property = Re_properties::find(1);
    $images = unserialize(base64_decode($re_property->images));
    echo $images[0]; // output : accounts/29cc60f2-c018-403c-80a9-94cda509e0f7.jpeg
    
    
    /**
     * FOR CORE PHP
     * ____________
     * $sql = "SELECT col1 FROM tb1 WHERE id=1";
     * $result = $conn->query($sql);
    
     * if ($result->num_rows > 0) {
     *   while($row = $result->fetch_assoc()) {
     *     $data = unserialize(base64_decode($row['col1']));
     *     print_r($data);
     *   }
     * }
     * /
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search