[英]Problem with passing values from checkboxes and displaying them in another page in Laravel
I am building an application for ads/properties in Laravel.我正在为 Laravel 中的广告/属性构建应用程序。 I have profile page with users info that can be updated.我有个人资料页面,其中包含可以更新的用户信息。 In there I have user preference section where I can check one or more checkboxes (house, flat) and based on what I checked after submission of form I want to show properties/ads that have those values on start page.在那里,我有用户首选项部分,我可以在其中选中一个或多个复选框(房屋、公寓),并根据我在提交表单后检查的内容,我想在起始页上显示具有这些值的属性/广告。 Currently when I submit form I get success message, but no results are displayed on startpage.目前,当我提交表单时,我收到成功消息,但起始页上没有显示任何结果。 Any help is greatly appreciated.任何帮助是极大的赞赏。 Here are my tables and code.这是我的表格和代码。
users (id, first_name)
properties (id, user_id, title, location, price, etc...)
properties_categories (id, property_id, category_id)
categories (id, category)
In category column in categories table are values house, flat, apartment, room.在类别表中的类别列中,价值房子、公寓、公寓、房间。
UserController.php用户控制器.php
public function update(StoreUserInfo $request, User $user)
{
if ( !($user->id == Auth::user()->id)) {
abort(404);
}
$request->validated();
$user->where('id', $user->id)->update(
[
'first_name' => $request->first_name,
]
);
$query = Property::query();
if ($request->has('propertyType1')) {
$request->get('propertyType1');
}
$propertyType1 = $request->input('propertyType1');
if (!empty($propertyType1)) {
$query->whereHas('category', function ($query) use ($propertyType1) {
$query->whereIn('category', $propertyType1);
});
}
$resultsFiltered = $query->where('active', 'Q')
->orWhere('active', 'A')
->orderBy('page_views', 'desc')
->with('user.photo', 'photos', 'category')->paginate(5);
return redirect()->back()->with('message', 'User information updated')->with(compact('resultsFiltered'));
}
edit.blade.php编辑刀片.php
<form id="form" method="POST" action="{{ route('user.update',['id'=>$user->id]) }}">
@method('PUT')
@csrf
<div class="form-group row">
<label for="first_name" class="col-md-4 col-form-label text-md-right">{{ __('First Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('first_name') is-invalid @enderror" name="first_name" value="{{ old('first_name',$user->first_name) }}" required autocomplete="first_name" autofocus>
@error('first_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row page-hero d-flex align-items-center justify-content-center">
<label for="preferences" class="text-center">Select your preferences</label>
</div>
<div class="row">
<div class="col-md-1" style="margin-right:15px; margin-left:60px;">
<div class="custom-control custom-checkbox">
<input id="house" name="propertyType1[]" value="house" type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="house">house</label>
</div>
</div>
<div class="col-md-1" style="margin-right:15px;">
<div class="custom-control custom-checkbox">
<input id="flat" name="propertyType1[]" value="flat" type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="flat">flat</label>
</div>
</div>
<div class="col-md-1" style="margin-right:50px;">
<div class="custom-control custom-checkbox">
<input id="apartment" name="propertyType1[]" value="apartment" type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="apartment">apartment</label>
</div>
</div>
<div class="col-md-1" style="margin-right:15px;">
<div class="custom-control custom-checkbox">
<input id="room" name="propertyType1[]" value="room" type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="room">room</label>
</div>
</div>
</div>
<div class="col-md-6 offset-md-4">
<button form="form" type="submit" class="btn btn-primary">
Save changes
</button>
</div>
</form>
startpage.blade.php起始页.blade.php
<div class="col-2">
<h1>Prefered ads</h1>
@if (isset($resultsFiltered))
@foreach ($resultsFiltered as $resultFiltered)
<div class="row">
<a href="{{route('property.show',['id'=>$resultFiltered->id])}}">
@if ($resultFiltered->active == 'A')
<button class="btn btn-success" disabled="dissabled">Verified</button>
@endif
<img class="img-fluid" src="/storage/{{$resultFiltered->main_photo['original_src']}}/{{$resultFiltered->main_photo['filename']}}" alt="Card image cap">
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$resultFiltered->price}} eur</button>
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$resultFiltered->quadrature}} m<sup>2</sup></button>
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px; margin-bottom:10px;">{{$resultFiltered->city}}</button>
<hr>
</a>
</div>
@endforeach
<div class="row">
<div class="col"></div>
<div class="col">{{ $resultsFiltered->links() }}</div>
<div class="col"></div>
</div>
@endif
</div>
web.php网页.php
Route::put('/user/{user}', 'UserController@update')->name('user.update');
Property.php属性.php
public function category()
{
return $this->belongsToMany(Category::class, 'properties_categories')->orderBy('priority', 'asc');
}
public function user()
{
return $this->belongsTo(User::class);
}
Category.php分类.php
public function property()
{
return $this->belongsToMany(Property::class);
}
public function users()
{
return $this->belongsToMany(User::class);
}
User.php用户名
public function categories()
{
return $this->hasMany(Category::class);
}
public function property()
{
return $this->hasMany('App\Property', 'user_id', 'id')->where('active', 'Q')->orWhere('active', 'A');
}
I would think your problem is here, as the $query
is on the property model and not the category model, so you would need to define both table and column in the whereIn.我认为你的问题就在这里,因为$query
在属性模型上而不是在类别模型上,所以你需要在 whereIn 中定义表和列。
if (!empty($propertyType1)) {
$query->whereHas('category', function ($query) use ($propertyType1) {
$query->whereIn('category', $propertyType1);
});
}
You would need to do this你需要这样做
if (!empty($propertyType1)) {
$query->whereHas('category', function ($query) use ($propertyType1) {
$query->whereIn('properties_categories.category', $propertyType1);
});
}
EDIT: Although there is a relationship set in your models you will still need to provide the table in your statement as your query is in the property model and not the category.编辑:尽管在您的模型中设置了关系,您仍然需要在语句中提供表,因为您的查询在属性模型中而不是在类别中。
$query = Property::query();
As it stands you are looking for property.category
, which doesn't exist.就目前而言,您正在寻找不存在的property.category
。 What you need is properties_categories.category
你需要的是properties_categories.category
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.