在Laravel中联接三个表的问题

时间:2019-07-29 10:22:54

标签: php laravel

我在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)

作为登录用户,当我在编辑个人资料页面中选中一个或多个复选框并提交表单时,将json值插入到用户表的user_preferences列中,例如[“ 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>

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])}}">
                        <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>

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);
}

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');
}

2 个答案:

答案 0 :(得分:1)

您可以在属性模型中添加一个Query Scope,然后在存在相关用户的首选项时调用范围。您根据偏好进行过滤的逻辑将位于范围内。

        Property::with(['category', 'user'])
        ->when(isset($user->user_preferences), function($q) use ($user) {
            $q->preferences($user->user_preferences);
        })
        ->orderBy('page_views', 'desc')
        ->paginate(5);

答案 1 :(得分:1)

$member = Member::select("members.*","products.id as productId","company.id as companyId")
            ->join("products","products.member_id","=","members.id")
            ->join("company",function($join){
                $join->on("company.member_id","=","members.id")
                    ->on("company.product_id","=","products.id");
            })
            ->get();
print_r($member);

How to Join Multiple Table in Laravel 5.8

Join multiple tables