Listing images

In this section, we are going to create an 'all images' section in our system, which will have a page navigation (pagination) system. There are a few steps to be followed as shown:

  1. First, we need to define its URL from our route.php file. For this, open app/http/routes.php and add the following lines:
    //This route is to show all images.
    Route::get('all',array('as'=>'all_images','uses'=>'[email protected]'));
  2. Now, we need a method named getAll() (there is a get method at the start because it will be a RESTful controller) to get values and load the view. To do this, open yourapp/controllers/imageController.php and add these codes before the last curly bracket (}):
    public function getAll(){
      //Let's first take all images with a pagination feature
      $all_images = DB::table('images')->orderBy('id','desc')->paginate(6);
      //Then let's load the view with found data and pass thevariable to the view
      return View::make('tpl.all_images')->with('images',$all_images);

    Here, we first got all the images from the database using the paginate() method, which will allow us to get the pagination links easily. After that, we loaded the view for the user with the images data with pagination.

  3. To view this properly, we need a view file. Save the following code in a file namedall_image.blade.php in the resources/views/tpl/ directory:
        @foreach($images as $each)
            <a href="{{URL::to('snatch/'$each->id)}}">{{HTML::image(Config::get('image.thumb_folder')'/'.$each->image)}}</a>
      {{--If no images are found on the database, we will showa no image found error message--}}
      <p>No images uploaded yet, {{HTML::link('/','care to upload one?')}}</p>

    We first extend the frontend_master.blade.php file with our content section. As for the content section, we first check whether any rows are returned. If so, then we loop them all in list item tags (<li>) with their permalinks. The links() method that came with the paginate class will create the pagination for us.


    You can switch the pagination template from config/view.php.

    If no rows have returned, that means there are no images (yet), so we show a warning message with a link to the new upload page (which is the index page in our case).

What if a person uploads an image that is not allowed or not safe for work? You would not like to have them on your website, right? So there should be an image deleting feature on your website.