【发布时间】:2019-11-30 08:30:12
【问题描述】:
我在 Laravel 中构建了一个广告/属性应用程序。我有一个带有复选框的过滤器的搜索表单。当我从同一请求中选择两个或多个选项时遇到问题,例如房屋、公寓、房间,它们是 PropertyType 我收到错误
未找到列:1054 'where 子句'中的未知列'category'(SQL:select count(*) as aggregate from
propertieswherecategoryin (house, flat, room))
我有三张桌子。
属性(id、位置、价格)
properties_categories(id、properties_id、category_id)
类别(id、类别)
house、flat、room 是类别表中类别列中的值。我想当我单击多个复选框以从数据库中获取正确的结果时。任何帮助表示赞赏。这是我的代码:
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Property;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class CategoryController extends Controller
{
public function search(Request $request, Property $property)
{
$category = $property->category;
$query = Property::query();
//PROPERTY TYPE- HERE IS PROBLEM!!!
if ($request->has('propertyType')) {
$request->get('propertyType');
}
$propertyType = $request->input('propertyType');
if (!empty($propertyType)) {
$query->whereIn('category', $propertyType);
}
$results = $query->paginate(6);
return view('startpage', compact('category', 'results', 'request', 'user.photo', 'photos', 'propertyBidAsk', 'propertyPayment', 'propertyType'));
}
}
startpage.blade.php
<form id="searchForm" method="GET" action="/search">
<div class="col-md-2 mb-6">
<h5>Property type</h4>
<div class="d-block my-3 ">
<div class="custom-control custom-checkbox">
<input id="house" name="propertyType[]" value="house" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('house', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="house">house</label>
</div>
<div class="custom-control custom-checkbox">
<input id="flat" name="propertyType[]" value="flat" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('flat', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="flat">flat</label>
</div>
<div class="custom-control custom-checkbox">
<input id="room" name="propertyType[]" value="room" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('room', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="room">room</label>
</div>
</div>
</div>
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit">Search</button>
</form>
属性.php
public function category()
{
return $this->belongsToMany(Category::class, 'properties_categories')->orderBy('priority', 'asc');
}
Category.php
public function property()
{
return $this->belongsToMany(Property::class);
}
【问题讨论】:
标签: php laravel search-form