Maison > Article > développement back-end > Laravel点击页面链接按某一条件排序,这个页面的更新链接应该如何写?
如题。。一个图片的展示页面,如果我想根据某一条件排序,例如根据ID倒序排序,那么这个在blade.php的条件更新链接应该怎样写?
<code>public function index() { $images = Image::paginate(10); return view('images-list',compact('images')); }</code>
<code>//想根据ID倒序,(请问这样的情况需要另外新开一个controller吗) public function idsort() { $images = Image::paginate(10); $idsort = $images->sortByDesc('id'); return view('test',compact('images','idsort')); }</code>
<code><!-- blade.php片段 --> @forelse($images as $image) <div class="col-md-3 col-xs-6 col-sm-6"> <div class="thumbnail"> <img src="%7B%7Basset(%24image->file)%7D%7D" alt="Laravel点击页面链接按某一条件排序,这个页面的更新链接应该如何写?" > <div class="caption"> <h5>{{$image->caption}}</h5> <p>{!! substr($image->description, 0,100) !!}</p> <p> </p> <div class="row text-center" style="padding-left:1em;"> <a href="%7B%7B%20url('/image/'.%24image->id.'/edit')%20%7D%7D" class="btn btn-warning pull-left">Edit</a> <span class="pull-left"> </span> {!! Form::open(['url'=>'/image/'.$image->id, 'class'=>'pull-left']) !!} {!! Form::hidden('_method', 'DELETE') !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger', 'onclick'=>'return confirm(\'Are you sure?\')']) !!} {!! Form::close() !!} </div> </div> </div> </div> @empty <p>No images yet, <a href="%7B%7B%20url('/image/create')%20%7D%7D">add a new one</a>?</p> @endforelse</code>
如题。。一个图片的展示页面,如果我想根据某一条件排序,例如根据ID倒序排序,那么这个在blade.php的条件更新链接应该怎样写?
<code>public function index() { $images = Image::paginate(10); return view('images-list',compact('images')); }</code>
<code>//想根据ID倒序,(请问这样的情况需要另外新开一个controller吗) public function idsort() { $images = Image::paginate(10); $idsort = $images->sortByDesc('id'); return view('test',compact('images','idsort')); }</code>
<code><!-- blade.php片段 --> @forelse($images as $image) <div class="col-md-3 col-xs-6 col-sm-6"> <div class="thumbnail"> <img src="%7B%7Basset(%24image->file)%7D%7D" alt="Laravel点击页面链接按某一条件排序,这个页面的更新链接应该如何写?" > <div class="caption"> <h5>{{$image->caption}}</h5> <p>{!! substr($image->description, 0,100) !!}</p> <p> </p> <div class="row text-center" style="padding-left:1em;"> <a href="%7B%7B%20url('/image/'.%24image->id.'/edit')%20%7D%7D" class="btn btn-warning pull-left">Edit</a> <span class="pull-left"> </span> {!! Form::open(['url'=>'/image/'.$image->id, 'class'=>'pull-left']) !!} {!! Form::hidden('_method', 'DELETE') !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger', 'onclick'=>'return confirm(\'Are you sure?\')']) !!} {!! Form::close() !!} </div> </div> </div> </div> @empty <p>No images yet, <a href="%7B%7B%20url('/image/create')%20%7D%7D">add a new one</a>?</p> @endforelse</code>
已经看到这个问题多次了,这不叫更新,你只需要对查询时排序即可。
Eloquent ORM文档
<code>$users = User::popular()->women()->orderBy('created_at')->get();</code>