I try to show variable from the controller to blade in laravel but the result is "Undefined Variable $bedrijven , i tried to not use as bedrijf but with no avail
The controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Bedrijf;
class Bedrijven extends Controller
{
//
function viewLoad()
{
return Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
}
//
{
//
function account (Request $req)
{
return $req->input();
}
}
The blade file
<html>
<table>
<tr>
<td>Id</td>
<td>bedrijven_id</td>
<td>Body</td>
</tr>
@foreach($bedrijven as $bedrijf)
<tr>
<td>{{$bedrijf['id']}}</td>
<td>{{$bedrijf['bedrijven_id']}}</td>
<td>{{$bedrijf['body']}}</td>
</tr>
@endforeach
</table>
</body>
</html>
</div>
</body>
</html>
the route
Route::get('list', [Bedrijven::class, 'show']);
CodePudding user response:
in your controller there is not a $data
variable that's why it shows a error
to fix it try this:
function viewLoad()
{
$data = Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
instead of :
function viewLoad()
{
return Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
CodePudding user response:
Minor spelling mistake and the methods controller does not match the route definition. It is quite easy to use compact, to create the array for the view data.
public function show() {
$bedrijven = Bedrijf::all();
return view ('list', compact('bedrijven'));
}
Now the blade code will match the compacted data.
@foreach($bedrijven as $bedrijf)