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

Veltix's avatar

Show only current user branches

Im trying to create new query with join statement.

My current query:

BookingMaster::with('user:id,name')->where('branch_id', $branch->id)->orderBy('id', 'desc')->get();

Here is my branch query with join but I dont know how to use that with('user:id,name')

DB::table('branch')
                ->join('business', 'branch.business_id', '=', 'business.id')
                ->join('branch_managers', 'branch.id', '=', 'branch_id')
                ->join('booking_master', 'branch.id', '=', 'branch_id')
                ->select('branch.id', 'branch.name', 'branch.address', 'branch.for_who', 'branch.start_time', 'branch.end_time', 'branch.icon', 'branch.is_featured', 'branch.status', 'booking_master.')
                ->where('branch_managers.user_id', Auth::id())
                ->get();

If I did like this in admin panel it showed all data what I need:

 public function show(Branch $branch)
            $booking = BookingMaster::with('user:id,name')->where('branch_id', $branch->id)->orderBy('id', 'desc')->get();
            $review = Review::with('user:id,name')->where('branch_id', $branch->id)->orderBy('id', 'desc')->get();
            $service = Category::with('service')->whereIn('id', $branch->category)->get();
            $employee = EmployeeInfo::with('user:id,name,email')->whereIn('emp_id', $branch->employee)->get();
            return view('salon.branch.show', compact('branch', 'booking', 'review', 'service', 'employee'));

    }

But now for business panel where is Different roles like owner and manager. For owner I show all his branches and manager all assigned branches. at the moment if I change url ID it shows different business branches..

At the moment I think this show branches views all branches even these what is not assigned or created by user: public function show(Branch $branch)

0 likes
9 replies
MichalOravec's avatar

Here is an example how to connect eager loading with joins

$users = User::with('contacts')->select('users.*')
    ->join('contacts', 'users.id', '=', 'contacts.user_id')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->where('orders.something', 'XYZ')
    ->get();

So apply that logic to your code.

1 like
Veltix's avatar

This returns array and stdclass but how I can get only stdclass?

$branch = DB::table('branch')
                ->join('business', 'branch.business_id', '=', 'business.id')
                ->select('branch.id', 'branch.name', 'branch.address', 'branch.for_who', 'branch.start_time', 'branch.end_time', 'branch.icon', 'branch.is_featured', 'branch.status')
                ->where('business.owner_id', Auth::id())
                ->get();

So I would use $branch->id but at the moment I have to use $branch[0]->id..

There is many branch ID's

in blade I use already foreach, but says stdclass ID doesnt exist.

Veltix's avatar

Okay, so using first, if I dd($branch); I get this stdclass

{#1808 ▼
  +"id": 2
  +"name": "Barbarella Salon"
  +"address": "rajkot  gujrat"
  +"for_who": 2
  +"start_time": "09:00:00"
  +"end_time": "19:00:00"
  +"icon": "5ed79560a64a0.png"
  +"is_featured": 1
  +"status": 1
  +"category_id": 1
  +"user_id": 9
}

but if I remove dd($branch); then I get error

Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, int given, called in 
Veltix's avatar
$branch = DB::table('branch')
                ->join('business', 'branch.business_id', '=', 'business.id')
                ->join('branch_categories', 'branch.id', '=', 'branch_categories.branch_id')
                ->join('branch_employees', 'branch.id', '=', 'branch_employees.branch_id')
                ->select('branch.id', 'branch.name', 'branch.address', 'branch.for_who', 'branch.start_time', 'branch.end_time', 'branch.icon', 'branch.is_featured', 'branch.status', 'branch_categories.category_id', 'branch_employees.user_id')
                ->where('business.owner_id', Auth::id())
                ->first();
MichalOravec's avatar

Still is not responsible code.

The problem will be with whereIn somewhere in your code. You have to pass there an array not int.

Veltix's avatar

These both have

$service = Category::with('service')->whereIn('iconv(in_charset, out_charset, str)d', $branch->category_id)->get();
$employee = EmployeeInfo::with('user:id,name,email')->whereIn('emp_id', $branch->user_id)->get();
MichalOravec's avatar

Use a classic where instead.

And what is this?

->whereIn('iconv(in_charset, out_charset, str)d', $branch->category_id)

Please or to participate in this conversation.