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

kazzuya's avatar

Second where not working

Hello, Why the second where not working ??

    $products = Product::all();
    $carts    = Cart::where('opened' , '1')->get();
    $payment  = Payment::where('status_id' , '1')->get();

    return view('admin_layouts.orders.new', compact('products', 'carts', 'payment'));
0 likes
27 replies
kazzuya's avatar

@Sinnbeck I have a table status contain 2 fields, and this status_id is foreign_key inside Payment Table

Sinnbeck's avatar

@kazzuya what does this give you

dd(Payment::where('status_id' , '1')->count());
Sinnbeck's avatar

@kazzuya are you know for a fact that you have one or more rows in the database with status_id 1?

kazzuya's avatar

@Sinnbeck number "1" , Tried also dd(Payment::where('status_id' , '2')->count()); and it gives 1, That's correct but when i use this $payment = Payment::where('status_id' , '1')->get(); , It brings them all

Sinnbeck's avatar

@kazzuya you first said it didn't bring any result and now you say it brings them all? How are you using it in blade?

kazzuya's avatar

@Sinnbeck yes no result I mean even if i make it like this $payment = Payment::where('status_id' , '1')->get(); or like this $payment = Payment::where('status_id' , '2)->get(); or even a number not in database nothing happen also no errors

Sinnbeck's avatar

@kazzuya yeah that won't throw an error if nothing is found. You just get an empty collection

Sinnbeck's avatar

@kazzuya sorry I might be confused. Is the problem that it gets the wrong data or no data or that it does not give an error?

kazzuya's avatar

@Sinnbeck i have the data because of this $carts = Cart::where('opened' , '1')->get(); It's like the first where when user has paid and automatically the status_id in other table will be 1 Then the admin can update the status_id to 2 that mean it's delivered , In database everything is working well even in index i can see the status_id first one ('Paid', and it has the id '1') and the second one ('Delivered' and it has the id '2'),

kazzuya's avatar

@Sinnbeck

     @foreach ($carts as $cart)
              <tr>
					<td class="text-success fw-bold">{{ $cart->payment->status->status_name }}</td>
		      </tr>
      @endforeach
Sinnbeck's avatar

@kazzuya ah. So you never use $payment

$payment is not the same as $cart->payment

The first is a seperate variable and the second is a relationship

Sinnbeck's avatar

My guess is that this is what you actually want

$carts  = Cart::where('opened' , '1')
->with(['payment' => function ($query) {
    $query->where('status_id' , '1');
}])->get();

Delete the $payment line as it's never used

kazzuya's avatar

@Sinnbeck i got this Attempt to read property "country" on null

                                <td>{{ $cart->payment->address }}</td>

                                <td>{{ $cart->payment->street }}</td>

                                <td>{{ $cart->payment->email }}</td>

same for all

Sinnbeck's avatar

@kazzuya yeah. There is no guarantee that there is a payment for each. But you can do a fallback

{{ $cart->payment->address ?? 'no payment for '. $cart->id}} 
kazzuya's avatar

@Sinnbeck it works but still showing the results with id '2' with word 'no payment for ', Is that correct ? was trying to hide them and getting only with Id '1'

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@kazzuya it's hard to say as I don't know what's in your database. But if your relationships are correct then it should be correct. Do you perhaps want to only get carts that has payment with status 1?

$carts  = Cart::where('opened' , '1')
->with(['payment' => function ($query) {
    $query->where('status_id' , '1');
}])
->whereHas('payment', function ($query) {
    $query->where('status_id' , '1');
})->get(); 
1 like

Please or to participate in this conversation.