【问题标题】:Laravel- arranging records in ascending and descending orderLaravel - 按升序和降序排列记录
【发布时间】:2018-05-20 20:55:43
【问题描述】:

我的控制器中有一个创建方法

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

问题-我怎样才能让 asc 按钮按升序和 des 降序对图像进行排序,有没有办法将它连接到我的控制器,或者有没有办法通过任何方式按升序和降序对它们进行排序其他方式?

【问题讨论】:

  • 只是旁注:您的foreach(image as property).... 最后一个property_id 被选中,这看起来有些不对劲。这是故意的吗?
  • 最后一个用户登录的属性ID不仅仅是最后一个属性ID

标签: php laravel laravel-5


【解决方案1】:

有很多方法可以解决排序问题,但是当您设置好页面后,让我告诉您排序记录的方法。创建另一个指向您的 settings.photos 路由的操作

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-&gt;id 以通过表单传入您的控制器操作

https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">

  @csrf
    
  <input type="image_id" value="{{$id}}" />
  <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="image_id" value="{{$id}}" />
  <input type="submit" value="Descending" class="settings-photos-header2 text-center"/>
</form>

现在对您之前的控制器代码进行一些更改

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, 'id' => $id]);
}

正如我告诉您的,即使通过 Ajax 也有多种方法可以对记录进行排序,但我建议您使用您的代码示例。我也没有优化代码。

如果您有任何问题,请告诉我。 谢谢

【讨论】:

  • 我想对所有图像进行排序而不是一个,所有图像不是一个特定图像,如果每个输出2个或更多图像,按钮应该对它们进行排序
  • 所有图像都不是一个是什么意思?就像您提到在特定属性中获取图像的查询一样,以下查询将返回属性中的排序图像。 $image_main = Image::where('property_id', $image_id)->sortBy('id', $orderBy)->get();根据您的代码示例,其中 $image_id 是 Property->id ..
  • 是的,我可以拥有 2 张或更多图片
  • 好的,我假设属性有图像,并且您想按 ASC/DESC 顺序获取属性以外的所有图像。然后执行以下查询 public function settings_photos($property_id) { $form = Input::get('submit'); $orderBy = $form == "升序" ? “升序”:“降序”; # 获取特定属性中的图像 $list_of_images = Image::where('property_id', $property_id) # 按 Asc/Desc 排序 ->sortBy('id', $orderBy)->get();返回视图('settings.photos', ['image_array' => $list_of_images]); }
  • @nourman 这很有帮助,但请您将其添加到您在上面给我的答案中以便更清楚,只需编辑并添加您的评论,谢谢
猜你喜欢
  • 2011-08-25
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 2020-02-08
  • 2012-11-08
  • 2023-03-07
  • 2016-10-18
相关资源
最近更新 更多