mardi 17 avril 2018

How do I order my images DESC or ASC compared to created_at row?

Hello I am learning Laravel. I have an image gallery with pagination and I want to add a dropdown button from where I can choose how to order my images. This is my photos table which I want to be able to order them from a click.

public function up()
{
    Schema::create('photos', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedinteger('user_id')->index();
        $table->string('title');
        $table->string('image');
        $table->timestamps();
    });
}

This is my Photos model:

protected $fillable = [
    'user_id','title','image'
];

public function user()
{
    return $this->belongsTo(User::class,'user_id');
}
public function comments()
{
    return $this->hasMany(Comment::class,'comments');
}
public function favorited()
{
    return $this->hasMany(Favorites::class);
}

This is my pagination image gallery:

    @foreach($images as $image) 
<div class='col-sm-6'>
    <div class="pagination">
        <a class="thumbnail fancybox detail-box" rel="ligthbox" href="/images/" data-toggle="modal" data-target="#detail-mode">
            <img class="img-fluid" alt="" src="/images/" />
            <small class='text-muted'></small>
            <small class='text-muted'> </small>   
        </a>
        <a class="btn btn-info btn-xs btn-add add" href="">  Edit </a>
        <a class="btn btn-success btn-xs btn-add add" href="">Details </a>
        @if(auth()->check())
        <meta name="csrf-token" content="">
        <form action="" data-id="" method="POST" name="delete" class="formDeleteImage" >
            
              
            <div class="form-group" >
                <button class="btn btn-danger btn-xs btn-delete deleteImage"  type="submit" >Delete</button>
            </div>
        </form>
        @endif  
    </div>                  
</div> <!-- col-6 / end -->
@endforeach

Dropdown button from where I want to choose how to order the images:

            <div class="dropdown">
                <a class="btn btn-secondary dropdown-toggle" href="https://example.com" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                 OrderBy</a>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="/gallery?popular=1">Popular</a>
                    <a class="dropdown-item" href="/gallery?popular=1">Not Popular</a>
                    <a class="dropdown-item" href="/gallery?recent=1">Recent</a>
                    <a class="dropdown-item" href="/gallery?older=1">Older</a>
                </div>
            </div>  

And this is my controller from where I show the images:

public function show(Request $request)
    {
     // $user=User::all();    
     $images = DB::table('photos')->paginate(2);
     $featured_images=$this->featured();
     $comments = Comment::all();
     return view::make('gallery',['images'=>$images,'featured_images'=> $featured_images,'comments'=>$comments]);
    }

So what do I have to do on controller or database level to order my images how I want? Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire