imoh's avatar
Level 1

display image encoded in my database

i want to display multiple images stored in my database, I created a form with input type of file with name file[] so as to allow admin user upload multiple images with an article, with the controller method to store the form like this

public function store(Request $request)

{

    $this->validate($request, [
        //this method method validates the file

            'filename' => 'required',
            'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'

    ]);
    
    if($request->hasfile('filename'))
     {

        foreach($request->file('filename') as $image)
        {
            $name=$image->getClientOriginalName();
            $image->move(public_path().'/images/', $name);  
            $data[] = $name;  
        }
     }

     $form= new Form();
     $form->filename=json_encode($data);
     
    
    $form->save();


   return back()->with('success', 'Your images has been successfully');
}

this works very fine and stores my image in the database correctly in an array, but the problem is that when i want to display the images for this article i use the index method in the controller

public function index() { $form = Form::all();

return view('show')->withForm($form);

}

then in my show.blade.php i tried to loop through, so as to display my images like this

@foreach(json_decode($form->, true) as $images)

<a href="{{ url('images/'.$images->name)}}"
   class="portfolio-box">
    <img src="{{ url('images/'.$images->name)}}"
         class="img-responsive" alt="--">
    <div class="portfolio-box-caption">
        <div class="portfolio-box-caption-content">
        <span class="glyphicon glyphicon-zoom-in"
              style="font-size: 80px"></span>
        </div>
    </div>
</a>

but it keeps showing me error, please how can i get through this

0 likes
8 replies
rawilk's avatar

Unless you have a typo in this post, your error is coming from this line:


@foreach (json_decode($form->, true) as $images)

It also looks like you're storing it as filename, but you're referencing it as name, which won't cause an error, but will result in a null value being returned.

Do this instead:


@foreach (json_decode($form, true) as $image)
    <a href="{{ url('images/' . $image->filename) }}" class="portfolio-box">
        <img src="{{ url('images/' . $image->filename) }}" class="img-responsive" alt="--">
        <!-- rest of your markup -->
    </a>
@endforeach

One thing I'd recommend doing is making the path the image's location an accessor on your model to make it easier to reference:


// In your model
public function getImagePathAttribute()
{
    return url('images/' . $this->filename);
}

// Usage
$image->image_path;

1 like
imoh's avatar
Level 1

hello @wilk_randall i am still having the same issue., when i restart my server i get the whoops page(trying to get the property of non-object)

imoh's avatar
Level 1

here is my code pls help @wilk_randall here is my updated code bcos i want to save the article with images

class FormController extends Controller { public function index() { $form = Form::all();

return view('show')->withForm($form);

}

public function store(Request $request)

{

    $this->validate($request, [
        //this method method validates the file


             'title'    =>  'required',
             'news'     =>  'required' , 
             'filename' =>  'required',
            'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'

    ]);
    
    if($request->hasfile('filename'))
     {

        foreach($request->file('filename') as $image)
        {
            $name=$image->getClientOriginalName();
            $image->move(public_path().'/images/', $name);  
            $data[] = $name;  
        }
     }

     $form= new Form();
     $form->filename=json_encode($data);
     $form->title = $request->title;
     $form->news = $request->news;
    
     
    
    $form->save();


   return back()->with('success', 'Your news content has been successfully updated');
}

in my view i have

@foreach(json_decode($form, true) as $image)

{{ $image->title }}

{{ $image->news }}

<a href="{{ url('images/'.$image->filename)   }}"
   class="portfolio-box">
    <img src="{{ url('images/'  . $image->filename) }}"
         class="img-responsive" alt="--">
    <div class="portfolio-box-caption">
        <div class="portfolio-box-caption-content">
        <span class="glyphicon glyphicon-zoom-in"
              style="font-size: 80px"></span>
        </div>
    </div>
</a>
@endforeach

when i do this i get the trying to get property of non-object please help

rawilk's avatar

You're trying to json_decode your model, when it looks like all you need to do is decode the column filename. It also won't be an object when you pass true in json_decode, it will be an array.

I think something like this is what you need:

@foreach (json_decode($form->filename, true) as $image)
    <a href="{{ url('images/' . $image['filename']) }}" class="portfolio-box">

    <!-- rest of your markup -->
@endforeach
1 like
imoh's avatar
Level 1

Thank you very much @wilk_randall I keep getting this error

"Property [filename] does not exist on this collection instance."

rexsteroxy's avatar
if($request->hasfile(‘filename’))
{

foreach($request->file(‘filename’) as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().’/properties/’, $name);
$url= URL::to(‘/’) . ‘/properties/’ . $image->getClientOriginalName();
$data[] = $url;
}
}
$property= new Property;
$property->image=json_encode($data);
$property->save();

Am storing the image location url to my database column call images. and the are in this form… [“http://kizflex.local/properties/10.jpg”,”http://kizflex.local/properties/11.jpg”]

So how can i iterate through this particular column so as to get each address and display it in my view? Please am stuck i need help.

Please or to participate in this conversation.