Php Laravel-按升序和降序排列记录

Php Laravel-按升序和降序排列记录,php,laravel,laravel-5,Php,Laravel,Laravel 5,我的控制器中有一个create方法 public function create() { $image = PropertyUser::where('user_id', '=', Auth::user()->id)->get(); foreach($image as $property) { $id = $property->property_id; } $image_main = Image::where('property_id', $id)->get();

我的控制器中有一个create方法

public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
    $id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main]);
}
这是我的photos.blade文件

<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
    @csrf
    <input type="submit"  value="Ascending " class="settings-photos-header2 text-center"/>  |
</form><form name="dec" action="{{route("settings.photos")}}" method="post"  class="text-center">
    @csrf
    <input type="submit"  value= " Descending" class="settings-photos-header2 text-center"/>
</form>
<h2 class="settings-photos-header2 text-center">Photo Gallery</h2>
@foreach ($image_array as $images)
    <div class="image-warp"><img src="{{$images->filename}}"
                                 style="width:100px;height:100px;"><br/><span style="color: #1b1e21">{{$images->description}}</span>
    </form>
    </div>
    @endforeach

@csrf
|
@csrf
照片库
@foreach($image\u数组作为$images)
文件名}”
style=“宽度:100px;高度:100px;“>
{{$images->description}” @endforeach

问题-如何使asc按钮按升序排列图像,des按降序排列图像,是否有方法将其连接到我的控制器,或者是否有方法通过任何其他方式按升序和降序排列图像?

有许多方法可以解决排序问题,但在设置页面时,让我告诉你您可以选择对记录进行排序的方式。创建另一个指向您的
设置的操作。照片
路线

public function settings_photos($property_id) {

  $form = Input::get('submit');
  $orderBy = $form == "Ascending" ? "asc" : "desc";

  # Fetch Images in of Specific Property 
  $list_of_images = Image::where('property_id', $property_id) 
  # Order by Asc/Desc 
  ->sortBy('id', $orderBy)->get(); 

  return view('settings.photos', ['image_array' => $list_of_images]); } 
现在添加
Image->id
以按表单传递控制器操作

https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#

@csrf
|
@csrf

我想对所有图像进行排序,而不是一个,所有图像不是一个特定的图像,如果为每个图像输出2个或多个图像,则按钮应该对它们进行排序。您所说的“所有图像都不是一个”是什么意思?就像您提到的在特定属性中获取图像的查询一样,接下来的查询将在属性中返回已排序的图像。$image\main=Image::where('property_id',$Image_id)->sortBy('id',$orderBy)->get();其中$Image_id是property->id,根据您的代码示例..是的,一个属性我可以有两个或多个imagesO.K,我假设属性有图像,并且您希望以ASC/DESC顺序获取该属性以外的所有图像。然后执行以下查询公共函数设置($property_id){$form=Input::get('submit');$orderBy=$form==“升序”?“asc”:“desc”;#在特定属性中获取图像$list_of_Images=Image::where('property_id',$property_id)#按asc/desc sortBy('id',$orderBy)->get();返回视图('settings.photos',['Image_array'=>$list_图像])@nourman这很有帮助,但为了更清楚,请您将其添加到您上面给我的答案中,只需编辑并添加您的注释,谢谢只是一个旁注:您的
foreach(图像作为属性)看起来有问题..
选择最后一个
属性id
。这是故意的吗?登录属性id的最后一个用户不仅仅是最后一个属性id