Php Laravel-使用链接按升序和降序排列图像
我的控制器中有一个create方法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('proper
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]);
}
这是我的刀片视图
<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按降序排列,有没有办法将其连接到我的控制器,或者有没有办法通过
链接按升序和降序排列图像?首先,foreach
在每次迭代中覆盖$id
,最后是$id的值e> 是$image
数组中最后一项的$property\u id
值
你需要定义关系
在PropertyUser
类中添加:
公共函数图像(){
返回$this->belongsTo(Image::class,'property_id');
}
然后,在控制器中的创建方法中:
公共函数创建(请求$Request){
//使用方向查询参数定义asc或desc排序
$order_direction=$request->query('direction');
$property\u user=PropertyUser::where('user\u id','=',Auth::user()->id)
->使用(['images'=>函数($query)使用($order\u direction){
$query->orderBy('id',$order\u direction)
})->get();
//您可以在此处访问具有关联图像的阵列:
$images=$property\u user->images;
返回视图('settings.photos',['image\u array'=>$images]);
}
指向已排序图像的链接应该是:
升序和降序是什么意思?我甚至没有看到表单的输入字段。它不一定是表单,它可以是链接,只要按钮/链接按特定顺序对图像排序