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

lifesound's avatar

PAss data from controller to view

invoiceController

	class InvoiceController extends Controller
	{
 	   public function step1(Request $request)
 	   {
 	       $request->validate([
	            'invoice' => 'required|numeric',
 	           'telNumber' => 'required|numeric',
 	       ]);

   	     // $invoice = Invoice::with('items')->where('number' ,$request->invoice)->where('tel' ,$request->telNumber)->first();
   	     $invoice = Invoice::where('number' ,$request->invoice)->where('tel' ,$request->telNumber)->first();
   	     if($invoice) {
    	        return redirect('/step2')->with('invoice_id', $invoice->id);
     	   }
	
    	    return back()->with('top','Incorrect invoice or / and telephone number');
  	  }
	
   	 public function step2(Request $request)
  	  {
   	     $invoice_id = session('invoice_id');
    	    $invoiceWithItems = Invoice::with('items')->find($invoice_id);
        	if($invoiceWithItems){
            	// return $invoiceWithItems;
          	  return view('pages.guest.step2page',compact('invoiceWithItems'));
        	}
        	return back()->with('top','Incorrect invoice data');
    	}

	}

step2page.blade.php

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

step2.blade.php

	@extends('pages.guest.step2page')
	@section('content')
	{{ $invoiceWithItems }}
	@endsection

there is 500 server error i do not know ehy ? I should mention that in step.blade.php if i return $invoiceWithItems it returned very good as a json data . but not pass to view .. i dunno why !

0 likes
29 replies
lifesound's avatar

@tykus [2022-01-18 23:19:09] local.ERROR: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) {"userId":5000,"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Allowed memory size of 134217728 bytes exhausted (tried to allocate 16384 bytes) at /lara5/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php:52) [stacktrace] #0 {main}

Snapey's avatar

@lifesound it means php ran out of memory. Most likely you have an infinite loop

1 like
lifesound's avatar

@Snapey mmm from code above .. is there any issue especially step2 function ?

lifesound's avatar

@Snapey if i did

				    public function step2(Request $request)
   		 {

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

if i return $invoiceWithItems as json it works

MichalOravec's avatar

Why did you create a second thread with the same problem?

Snapey's avatar

Show the view where you loop over invoice items

lifesound's avatar

@Snapey this is the json

{
"id": 1,
"number": 666,
"tel": "1002706658",
"invoiced_at": "2022-01-18T21:18:51.000000Z",
"created_at": "2022-01-18T21:18:51.000000Z",
"updated_at": "2022-01-18T21:18:51.000000Z",
"items": [
{
"id": 4000,
"sku": "1922",
"invoice_id": 1,
"description": "Qui quas ex quis.",
"amount": 2,
"reason": "Vitae est quo repudiandae nihil architecto voluptas dolor aut.",
"warranted_months": 10,
"unboxed": 1,
"shipped": 0,
"created_at": "2022-01-18T21:18:51.000000Z",
"updated_at": "2022-01-18T21:18:51.000000Z"
},
{
"id": 4001,
"sku": "430",
"invoice_id": 1,
"description": "Modi ipsam nam optio ipsa cupiditate.",
"amount": 3,
"reason": "Nostrum illum veritatis omnis maiores omnis dolore harum.",
"warranted_months": 10,
"unboxed": 1,
"shipped": 0,
"created_at": "2022-01-18T21:18:51.000000Z",
"updated_at": "2022-01-18T21:18:51.000000Z"
}
]
}```

// pages.guest.step2page

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

@endsection


```
@extends('pages.guest.step2page')
<!-- // -->
@section('content')
<!-- // -->
@foreach ($invoiceWithItems as $item)
<div>{{ $item }}</div>
@endforeach
<!-- // -->
@endsection
```
tykus's avatar

@lifesound the JSON works because you don't return the view (that includes the view)

return $invoiceWithItems; <=== works

1 like
tykus's avatar

@lifesound is this the same problem where you are returning a view that includes the very same view inside?!?!?!? You see how that might result in an issue, right....

lifesound's avatar

@tykus yes but i changed the components system to the old-blade system and by help here i watch the log and found a memory err

lifesound's avatar

@tykus

@extends('pages.guest.step2page')
<!-- // -->
@section('content')
<!-- // -->
@foreach ($invoiceWithItems as $item)
<div>{{ $item }}</div>
@endforeach
<!-- // -->
@endsection
Snapey's avatar

@lifesound

Step2page extends step2page. No wonder php dissappears up its own arse

@extends('pages.guest.step2page')
2 likes
tykus's avatar

@lifesound you are returning a view

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

that @extends the same view?

@extends('pages.guest.step2page')

Based on this and the last thread; I think you need to go over the docs and look over the Blade tutorials to get a better understanding of Blade, Blade Layouts and Blade components!

1 like
Snapey's avatar

@lifesound controller

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

then the view extends the same blade file

lifesound's avatar

@tykus NO this is step2page.blade.php

				@extends('layout.guest')
				<!-- // -->
				@section('content')
				<!-- // -->
				@include('layout.stepper',['step'=> '2'])
				<!-- // -->
				@include('layout.step2', ['invoiceWithItems' => $invoiceWithItems])
				<!-- // -->
				@endsection
tykus's avatar
tykus
Best Answer
Level 104

@lifesound but that's not what you posted earlier...

Here's some advice for you. Simplify. You're all over the place with your templates and layouts. Simplify the view you are returning by removing all @extends @includes directives. Make sure that the view will render with the data it needs; it will be ugly (and that's fine). Progressively reintroduce the other templates, layouts etc. and when an error occurs, diagnose that error

1 like
Snapey's avatar

and choose clearer naming of files

1 like

Please or to participate in this conversation.