Php 在Laravel中连接三个表的问题
我有一个在拉威尔的广告/物业项目。我有很多用户和属性、用户和类别之间的关系,也有很多属性和类别之间的关系。这是我的桌子Php 在Laravel中连接三个表的问题,php,laravel,Php,Laravel,我有一个在拉威尔的广告/物业项目。我有很多用户和属性、用户和类别之间的关系,也有很多属性和类别之间的关系。这是我的桌子 users (id, first_name, last_name, user_preferences) properties (id, user_id, location, price) categories (id, category) category_property (id, category_id, property_id) 作为登录用户,当我选中编辑配置文件
users (id, first_name, last_name, user_preferences)
properties (id, user_id, location, price)
categories (id, category)
category_property (id, category_id, property_id)
作为登录用户,当我选中编辑配置文件页面中的一个或多个复选框并提交表单时,我会在用户表的用户首选项列中插入json值,例如,[“house”,“flat”]。这些是“类别”表中“类别”列中的值。我希望登录的用户显示具有他在表单中签入的值的所有属性。我在控制器中编写查询时遇到问题,该查询将连接这些表并获取过滤的属性。使用当前代码,我可以获得所有属性。任何帮助都将不胜感激。这是我的密码
CategoryController.php
public function index(Category $category, Property $property, User $user)
{
preferedAdsResults = $property
->orderBy('page_views', 'desc')
->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>
<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])}}">
<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->location}}/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>
Property.php
public function category()
{
return $this->belongsToMany(Category::class);
}
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');
}
您可以向属性模型中添加一个范围,然后在存在相关用户的首选项时,调用该范围。根据首选项进行筛选的逻辑将位于范围中
Property::with(['category', 'user'])
->when(isset($user->user_preferences), function($q) use ($user) {
$q->preferences($user->user_preferences);
})
->orderBy('page_views', 'desc')
->paginate(5);
您可以向属性模型中添加一个范围,然后在存在相关用户的首选项时,调用该范围。根据首选项进行筛选的逻辑将位于范围中
Property::with(['category', 'user'])
->when(isset($user->user_preferences), function($q) use ($user) {
$q->preferences($user->user_preferences);
})
->orderBy('page_views', 'desc')
->paginate(5);
也许我做错了什么,这样我仍然可以得到所有的属性,而不是那些过滤器。你的属性需要有类型(例如房屋/公寓等)。我在您上面描述的表中没有看到这一点,但是,假设它确实存在并且您的用户首选项正确,它将起作用。公寓/房屋是用户表中“用户首选项”列下的值是的,我可以看到,但它们需要对应于物业类型,否则您将同时返回公寓和房屋。嗯,如果house和flat存储在user_preferences列中,像这样[“house”,“flat”]我想显示所有具有这两个值的属性。我很困惑,我应该在哪里添加属性类型以及为什么?也许我做错了什么,我仍然得到了所有属性,而不是那些过滤器。您的属性需要有类型(例如房屋/公寓等)。我在您上面描述的表中没有看到这一点,但是,假设它确实存在并且您的用户首选项正确,它将起作用。公寓/房屋是用户表中“用户首选项”列下的值是的,我可以看到,但它们需要对应于物业类型,否则您将同时返回公寓和房屋。嗯,如果house和flat存储在user_preferences列中,像这样[“house”,“flat”]我想显示所有具有这两个值的属性。我很困惑,我应该在哪里添加属性类型?在声明
preferedAdsResults=$property(…)
之后的preferedAdsResults=$property
中缺少$