我在Laravel中有一个广告/媒体资源项目。我在用户和属性之间以及用户和类别之间有很多关系,在属性和类别之间也有很多关系。这是我的桌子。
users (id, first_name, user_preferences)
properties (id, user_id, location, price)
categories (id, category)
category_property (id, category_id, property_id)
作为登录用户,当我在编辑配置文件页面中选中一个或多个复选框时,我将json值插入到用户表的user_preferences列中,例如[“ house”,“ flat”]。我希望该登录用户在起始页上显示具有他在表单中签入的值的所有属性。使用当前代码,我会收到此错误
未找到列:1054“ where子句”中的未知列“ user_preferences”(SQL:从
properties
中选择count(*)作为聚合,其中active
= Q或active
= A和user_preferences
=车库)
我需要帮助以在控制器中编写正确的查询。
CategoryController.php
public function index(Category $category, Property $property, User $user)
{
$preferedAdsResults = $property->where('active', 'Q')
->orWhere('active', 'A')
->orderBy('page_views', 'desc')
->with('category', 'user')
->where('user_preferences', 'garage')
->paginate(5, ['*'], 'preferedAdsResults');
return view('startpage', compact('preferedAdsResults'));
}
edit.blade.php
<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="user_preferences[]" value="house" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('house', old('user_preferences', $user->user_preferences))) checked @endif>
<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="user_preferences[]" value="flat" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('flat', old('user_preferences', $user->user_preferences))) checked @endif>
<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="room" name="user_preferences[]" value="room" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('room', old('user_preferences', $user->user_preferences))) checked @endif>
<label class="custom-control-label" for="room">room</label>
</div>
</div>
</div>
startpage.blade.php
<div class="col-2">
<h1>Prefered ads</h1>
@if (isset($preferedAdsResults))
@foreach ($preferedAdsResults as $preferedAdsResult)
<div class="row">
<a href="{{route('property.show',['id'=>$preferedAdsResult->id])}}">
@if ($preferedAdsResult->active == 'A')
<button class="btn btn-success" disabled="dissabled">Verified</button>
@endif
<img class="img-fluid" src="/storage/{{$preferedAdsResult->main_photo['original_src']}}/{{$preferedAdsResult->main_photo['filename']}}" alt="Card image cap">
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$preferedAdsResult->price}} eur</button>
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$preferedAdsResult->quadrature}} m<sup>2</sup></button>
<button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px; margin-bottom:10px;">{{$preferedAdsResult->city}}</button>
<hr>
</a>
</div>
@endforeach
<div class="row">
<div class="col"></div>
<div class="col">{{ $preferedAdsResults->links() }}</div>
<div class="col"></div>
</div>
@endif
</div>
Category.php
public function property()
{
return $this->belongsToMany(Property::class);
}
public function users()
{
return $this->belongsTo(User::class);
}
Property.php
public function category()
{
return $this->belongsToMany(Category::class)->orderBy('priority', 'asc');
}
public function user()
{
return $this->belongsTo(User::class);
}
User.php
public function categories()
{
return $this->hasMany(Category::class);
}
public function property()
{
return $this->hasMany('App\Property', 'user_id', 'id')->whereIn('active', ['Q','A']);
}
答案 0 :(得分:0)
如果想进行雄辩的加入,请在何处使用
public function index(Category $category, Property $property, User $user)
{
$preferedAdsResults = $property->where('active', 'Q')
->orWhere('active', 'A')
->whereHas('user_preferences', function($q)use($variable){
$q->where('column',$variable)})->paginate(5);
return view('startpage', compact('preferedAdsResults'));
}
有关laravel在哪里的文档
,并且如果您动态地在哪里有,您需要使用我的示例中的使用方法来传递它