Paul
Paul

Reputation: 283

Laravel - How can i display what has been sorted

This is my controller

if ($request->input('sort')) {
    $sort = $request->input('sort');
}

if ($sortBy == "asc") {
    $image_ = Image::where('property_id', $id)
              ->orderBy($sort,'asc')
              ->get();
 }

return view('settings.photos', ['image_' => $image_]);

This is my blade file

@if( isset($image_))
    @foreach ($image_ as $images)
        <div class="image-warp">
            <img src="{{$images->filename}}" 
                 style="width:100px;height:100px;">
            <br/>
            <span style="color: #1b1e21">{{$images->description}}</span>
        </div>
    @endforeach
@endif

It doesn't display the $images_ variable, how can i display it?

Upvotes: 1

Views: 1098

Answers (1)

Saurabh Mistry
Saurabh Mistry

Reputation: 13689

You have to pass two variables to your controller

1) SortBy : the column by which , you want to do sorting

2) SortOrder : sort order ascending or descending

You can pass these two variables to request query string parameters :

like : http://yourdomain.com/products?sortBy=name&sortOrder=-1

get these query parameters in your controller

 public function sort_products(Request $request){
    $id, $sortBy , $sortOrder ;

    $id=$request->input('id');

    // check request has sortBy 

    if($request->has('sortBy')){
      $sortBy=$request->query('sortBy');
    }else{
       //request has no sort by  , set default sort by 
        $sortBy='name';
     }

     // check request has sortOrder 

    if($request->has('sortOrder')){
       $sortOrder=$request->query('sortOrder');
    }else{
       //request has no sort order  , set default sort order         
       $sortOrder='asc';
    }

    $images = Image::where('property_id',$id)->orderBy($sortBy,$sortOrder)->get();

    return view('yourview',compact('images','sortBy','sortOrder'));
 }

in your blade view use font aweasome icons to display up and down arrow for sort order :

http://fontawesome.io/icons/

@if( count($images)>0 )
        <table>
          <thead>
              <tr>
                 <th>FileName 
                     @if($sortBy=='name')
                         @if($sortOrder=='asc')
                          <span><i class="fas fa-sort-up"></i></span>
                         @else
                          <span><i class="fas fa-sort-down"></i></span>
                         @endif
                     @endif
                  </th>
                 <th>Description
                   @if($sortBy=='description')
                         @if($sortOrder=='asc')
                          <span><i class="fas fa-sort-up"></i></span>
                         @else
                          <span><i class="fas fa-sort-down"></i></span>
                         @endif
                     @endif
                 </th>
              </tr>
          </thead>
          <tbody>
        @foreach ($images as $image)
             <tr>
                <td>
                   <img src="{{$image->filename}}" style="width:100px;height:100px;">
                </td>
                <td>
                     <span style="color: #1b1e21">{{$image->description}}</span>
                </td>
             </tr>
        @endforeach
        </tbody>
        </table>
@else
 <p>NO IMAGES FOUND.</p>
@endif

for more info : https://laravel.com/docs/5.6/requests

Upvotes: 1

Related Questions