Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rakic's avatar
Level 9

Problem with writing query between four tables in Laravel

I have project for ads/properties in Laravel. I am trying on my user profile page to select checkboxes (house, flat, room, etc.) and then to display ads that have those selected values. I have four tables.

users (id, first_name, user_preferences)

properties (id, user_id, location, price)

categories (id, category)

category_property (id, category_id, property_id)

In categories table under category column are values house, flat, room, and those values are connected through pivot table with properties. And in users table under user_preferences column are values that are checked in my user profile form. I need help writing query that will insert in user_preferences column values, for example room and flat, and then I want to be able to display on some page all ads/properties that have room and flat. Any help is appreciated. Here is my code.

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>
0 likes
0 replies

Please or to participate in this conversation.