简体   繁体   中英

Blade won't show data from PHPMyAdmin

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'\]);

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

Change this:

isset($data)

To this:

isset($vacatures)

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM