skip to Main Content

I have used a seeder to fill up data on my database! However, it doesn’t show up on my blade file! and it just only the table. I tried various methods, including changing the route file and to add data directly from Phpmyadmin.

Blade

<table>
    <tr>
        <td>Id</td>
        <td>bedrijven_id</td>
        <td>Body</td>
        <td>Created at</td>
        <td>Updated at</td>
        <td><button type="button">Soliciteer</button></td>
    </tr>
    @if(isset($data))
        @foreach($vacatures as $vacature)
            <tr>
                <td>{{$vacature['id']}}</td>
                <td>{{$vacature['bedrijven_id']}}</td>
                <td>{{$vacature['body']}}</td>
                <td>{{$vacature['created_at']}}</td>
                <td>{{$vacature['updated_at']}}</td>
            </tr>
        @endforeach

Controller

class Vacatures extends Controller
{
    function viewLoad()
    {
        return view('vacatures');
    }

    function show() {
        $data = Vacature::all();
    
        return view('list', ['vacatures' => $data]);
    }
}

Route

Route::get('list',[Vacatures::class,'show']);

3

Answers


  1. The $data variable only exists within your controller, it is not passed through to the view.

    Change this:

    isset($data)
    

    To this:

    isset($vacatures)
    
    Login or Signup to reply.
  2. As of comments from @ths and @Omar, the $data is not passed to view.

    By additional, $vacatures is instance of Eloquent collection at all.So for readable, you can check $vacatures->isNotEmpty() instead of isset

    New laravel version also supports blade @forelse (documentation)

    Login or Signup to reply.
  3. Instead of isset($data) use isset($vacatures), because you passed vacatures to view file not data

    @if(isset($vacatures))
        @foreach($vacatures as $vacature)
            <tr>
                <td>{{$vacature['id']}}</td>
                <td>{{$vacature['bedrijven_id']}}</td>
                <td>{{$vacature['body']}}</td>
                <td>{{$vacature['created_at']}}</td>
                <td>{{$vacature['updated_at']}}</td>
            </tr>
        @endforeach
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search