virtual's avatar

Display single and multiple image from database at the same time

Hi, i am new in laravel. So i am insert multi image url in a column but i also have single image url in the same table and i want to display all image in blade page. But i get an error when fetch multi image from a column of a table. The error is - Invalid argument supplied for foreach()

Here my blade page

{{$images}}
      @foreach($images as $imag)
      
      <div class="col-lg-3 col-md-6 mb-3">
          <div class="blog-post">
              <a  style="margin-left: 60px" href="{{ $imag->image_name }}">{{ $imag->image_name }}</a>
              
                @if($imag->multi==0)
                 <div class="blog-thumb">

                  <a href="#"><img src="{{asset($imag->images) }}" alt="" /></a>
              </div>
               @else
               <?php
               $vv=json_decode($imag->images, true);
               
                ?>
                 @foreach($vv as $imag)
                 {{$img}}
                  @endforeach
                
                 <div class="blog-thumb">

                  <a href="#"><img src="" alt="" /></a>
              </div>
            
             @endif
          </div>

          <div style="margin-top: 3px; margin-left: 60px">

            <a href="{{ route('imag-detail',['id'=>$imag->id]) }}"></a>
            <button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#show_modal_{{$imag->id}}">
               Images Detail
           </button>
       </div>

       <!-- Modal -->

   </div>
   
   @endforeach

Here my controller query function

public function store(Request $request) { $category = new Category(); $category->category_name = $request->category_name; $category->category_description = $request->category_description; $category->save(); return redirect('/add-category'); }

Please help me ,how can i fixed it. Thanks in advanced

0 likes
5 replies
jasonlimantoro's avatar

Can you share your Controller's method responsible for rendering that view? In particular what is the shape of $images?

The error Invalid argument supplied for foreach() simply implies that the $images variable is not traversable, i.e. not an array or Collection.

So there's a good chance that your Eloquent query is wrong.

1 like
virtual's avatar

@JASONLIMANTORO - Sir , here is my vie method of controller;

    public function show($id)
{
    $images = Image::where('category_id',$id)->get();
    $categories = Category::all();
    return view('front.imag.category-imag',[

        'images'=>$images,
        'categories'=>$categories,
        
    ]);
}

Here is my image upload method;

 public function store(Request $request)
 {
   if($request->hasfile('images'))
     {

        foreach($request->file('images') as $image)
        {
            $imageName=$image->getClientOriginalName();
            $directory = 'images/imag1/';
            $imgeUrl = $directory.$imageName;
           $image->move($directory, $imageName); 
            $data[] = $imgeUrl;  
        }
     }
     //$dd=json_encode($data);
  // dd(json_decode($dd));
   $Images = new Image();
   $Images->image_name = $request->image_name;
   $Images->category_id = $request->category_id;
   $Images->images = implode(',',$data);
   $Images->email_address = $request->email_address;
   $Images->password = $request->password;
   $Images->image_descriptions = $request->image_descriptions;
   $Images->save();
   $data = $Images->toArray();
    $data['pass'] = $request->password;
    Mail::send('front.mails.confirmation-mail', $data, function ($message) use ($data){
        $message->to($data['email_address']);
        $message->subject('Congratulation Mail');
    });
   return redirect('/imag-upload');
}

Here is my data table. how can i show single and retrieve and show multiple image from a column.

INSERT INTO images (id, image_name, category_id, images, email_address, password, image_descriptions, multi, created_at, updated_at) VALUES (1, 'gnfhfgh', '1', 'images/imag/Nature_-Fairy_Falls_1(1).jpg', '[email protected]', 'cvbcvbcvbcvb', 'cvbcvbvcbcv', 0, '2018-11-11 01:13:29', '2018-11-11 01:13:29'), (5, 'images 4', '3', 'images/imag/s34weissrosa.gif', 'rakibfdgabdur4@gmail.com', 'gdfgdfgdfg', 'dfgdfgdfgdgf', 0, '2018-11-14 01:49:43', '2018-11-14 01:49:43'), (6, 'test upload', '3', 'images/imag1/s27gelborange.gif', '[email protected]', '123456', 'test description', 0, '2018-11-14 03:15:44', '2018-11-14 03:15:44'), (13, 'sss', '1', 'images/imag1/f2.jpg,images/imag1/f3.jpg,images/imag1/f4.jpg,images/imag1/f5.jpg,images/imag1/f6.jpg,images/imag1/family-2923690_1280.jpg,images/imag1/img.jpg,images/imag1/kid.jpg', 'rakibabdur4@gmail.com', '1234', 'adad', 1, '2018-11-25 19:02:46', '2018-11-25 19:02:46');

virtual's avatar

@MUNAZZIL - sir i am trying this process but failed. which is being mentioned above.

{{ $imag->image_name }}

                @if($imag->multi==0)
                 <div class="blog-thumb">

                  <a href="#"><img src="{{asset($imag->images) }}" alt="" /></a>
              </div>
               @else
               <?php
               $vv=json_decode($imag->images, true);
               
                ?>
                 @foreach($vv as $imag)
                 {{$img}}
                  @endforeach
                
                 <div class="blog-thumb">

                  <a href="#"><img src="" alt="" /></a>
              </div>
            
             @endif

please review again and give better suggestion. Thanks

munazzil's avatar

You want to add some codes as like below because error occur in this @foreach($vv as $imag) .

    <?php
           $vv=json_decode($imag->images, true);
           
            ?>
             @foreach($vv as $img)
             {{$img}}
              @endforeach

Please or to participate in this conversation.