我在Laravel中有一个广告/媒体资源项目。我正在尝试在用户个人资料页面上选中复选框(房屋,公寓,房间等),然后显示具有所选值的广告。我有四个桌子。
users (id, first_name, user_preferences)
properties (id, user_id, location, price)
categories (id, category)
category_property (id, category_id, property_id)
在“类别”列下的“类别”表中,有“房屋”,“公寓”,“房间”值,这些值通过数据透视表与属性连接。在user_preferences列下的users表中,是在我的用户个人资料表单中检查的值。我需要编写查询的帮助,该查询将插入到user_preferences列值(例如room和flat)中,然后我希望能够在某些页面上显示所有具有room和flat的广告/属性。任何帮助表示赞赏。这是我的代码。
UserController.php
public function update(StoreUserInfo $request, User $user)
{
if ( !($user->id == Auth::user()->id)) {
abort(404);
}
$request->validated();
if ($request->has('user_preferences')) {
$request->get('user_preferences');
}
$user_preferences = $request->input('user_preferences');
//BELLOW IS QUERY THAT DOESN'T WORK!!!
if (!empty($user_preferences)) {
$user->whereHas('categories', function ($user) use ($user_preferences) {
$user->whereIn('categories', $user_preferences);
})->where('id', $user->id)->update(
[
'user_preferences' => json_encode($request->user_preferences)
]
);
}
$user->where('id', $user->id)->update(
[
'first_name' => $request->first_name
]
);
return redirect()->back()->with('message', 'User information updated');
}
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']);
}
Property.php
public function category()
{
return $this->belongsToMany(Category::class)->orderBy('priority', 'asc');
}
public function user()
{
return $this->belongsTo(User::class);
}
Category.php
public function property()
{
return $this->belongsToMany(Property::class);
}
public function users()
{
return $this->belongsTo(User::class);
}
edit.blade.php
<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>