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

lifesound's avatar

check for successul elequent query

i do not know why if conditional always yields an error

    		public function step2(Request $request)
    		{

       		 $invoice_id = session('invoice_id');
       		 $invoiceWithItems = Invoice::with('items')->find($invoice_id);
     		   if($invoiceWithItems){
      		      return view('components.pages.guest.step2page', ['invoiceWithItems' => $invoiceWithItems]);
      		  }
     		   back()->with('top','Incorrect invoice data');
    		}
Undefined variable $invoiceWithItems (View: /resources/views/components/pages/guest/step2page.blade.php)

when I pass the $invoiceWithItems to the view, it is undefined, whereas when returning it directly as JSON .. it works Do I have to check for a successful query? and what is the proper way to do that?

0 likes
17 replies
Nakov's avatar

It seems like you are checking correctly. But make sure you don't have any other place from where you are returning that view and you forgot to pass the invoiceWithItems.

Nakov's avatar

@lifesound you should have a blade view returned, like for example:

<x-app>
 <x-pages.guest.step2page :invoices="$invoiceWithItems" />
</x-app>

in your component:

@props('invoices')

// use $invoices here

and from the controller just return the blade view, not the component.

tykus's avatar

Show how you are actually returning the view and passing data to the view, e.g.

return view('components.pages.guest.step2page', compact('invoiceWithItems'));

or

<x-pages.guest.step2page :invoiceWithItems="$invoiceWithItems" />

EDIT this is a Blade component??? How are you actually using the step2Page???

tykus's avatar

@lifesound so you have <x-pages.guest.step2page /> somewhere in another template?

Your anonymous Blade component can accept data using data properties. You would not actually be returning the view directly!

<x-pages.guest.step2page :invoice-with-items="$invoiceWithItems" />
1 like
lifesound's avatar

@tykus I changed it to the old-fashion

		@extends('components.layout.guest') 
		@include('components.layout.stepper',['step'=> '2']) 
		@include('components.layout.step2', ['invoiceWithItems' =>$invoiceWithItems])

So why i have the same error ?

		Undefined variable $invoiceWithItems (View: /resources/views/components/pages/guest/step2page.blade.php)```
tykus's avatar

@lifesound and where does $invoiceWithItems come from for this old-fashioned view?

lifesound's avatar

@tykus from invoiceController

	    public function step2(Request $request)
    {

        	$invoice_id = session('invoice_id');
      	  $invoiceWithItems = Invoice::with('items')->find($invoice_id);
     	   if($invoiceWithItems){
     	       return view('components.pages.guest.step2page',compact('invoiceWithItems'));
     	   }
  	 	     back()->with('top','Incorrect invoice data');
  	  }
tykus's avatar

@lifesound so, the view you are returning is the same Blade template as the Blade component/Blade template partial that you @include in that Blade template???

lifesound's avatar
				// step2page.blade
				<x-layout.guest>
    								<x-layout.stepper :step="2"/>
    								<x-layout.step2  :invoiceWithItems="$invoiceWithItems"/>
				</x-layout.guest>
Nakov's avatar

@lifesound you should not return a component from your controller but a VIEW that contains those components.

1 like
lifesound's avatar

@Nakov I have changed all the website to the old-fashion blade and remove all the views from /components dir and still have the same err

Please or to participate in this conversation.