Skip to Content
PHPCODE
Passing Data to View in Laravel
laravel code / August 23, 2018

Passing Data to View

Routes in define function view load

<?php

Route::get(‘/’,function(){
return view(‘test-data’);
});
passing data to View

<?php

Route::get(‘/’,function(){
$data=[
‘rahul’,’amit’,’Gaurav’,’Abhishek’,’Imran’
];
return view(‘test-data’,compact(‘data’));
});

or

<?php

Route::get(‘/’,function(){
$data=[
‘rahul’,’amit’,’Gaurav’,’Abhishek’,’Imran’
];

return view(‘test-data’)->with(‘data’,$data);
});

or

<?php

Route::get(‘/’,function(){
$data=[
‘rahul’,’amit’,’Gaurav’,’Abhishek’,’Imran’
];

return view(‘test-data’)->withNames($data);
});
test-data file created resources and view folder and created test-data.blade.php

<h1>Hello</h1>

or

passing varible

<h1>
Hello
</h1>

<?php print_r($data); ?>

PHPCODE © 2024