PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to retrive single field array data from database and separate them- using laravel, PHP

 November 15, 2022     arrays, laravel, laravel-6, mysql, php     No comments   

Issue

I Trying to upload multiple image for my product, and store the names of that images in database as an array. My code works like this-

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

if (isset($images)) {
  foreach($images as $image){
    $imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
    if (!file_exists('uploads/product/images')) {
       mkdir('uploads/product/images', 0777, true);
    }
      $image->move('uploads/product/images', $imagename);
      $data[] = $imagename;
    }
            
  }else{
     $data[] = 'default.png';
  }

  $product = new Product();
  $product->images = json_encode($data);

And data stored inside the images field like-

["jeans-2020-08-13-5f352f18b30a4.jpg","jeans-2020-08-13-5f352f18b36a0.jpg","jeans-2020-08-13-5f352f18b3a2c.jpg"]

**And the problem is how can i separate this image name to show images in Laravel Blade? **
OR Suggest me, If there is another way to upload multiple image or multiple value in laravel-6


Solution

You need to deserialize the images names and then loop through them:

@foreach(json_decode($product->images) ?? [] as $image)
    <img src="uploads/product/images/{{ $image }}">
@endforeach


Answered By - Alberto Sinigaglia
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing