Eric
Eric

Reputation: 35

laravel database pass data blade

I'm new to Laravel and trying to figure out why one of my pages is not pulling data from the database. Running locally using a setup based on the project in Laracasts. I'm on Laravel 5.4 running PHP 7 and MySQL on OS X Sierra.

I'm getting this error: Undefined Variable: hashtags (in my hashtags.blade.php file)

I have this code hashtags.blade.php, designed to pull entries from a database:

<h2>Your Hashtags</h2>

    <table>
        <tr>
            <th>Hashtag</th>
            <th>List Name</th>
            <th>List Members Added</th>
        </tr>

    @foreach($hashtags as $hashtag)

        <tr>
            <td>{{ $hashtag -> tagname }}</td>
            <td>{{ $hashtag -> listname }}</td>
            <td>Count</td>
        </tr>

    @endforeach

    </table>

In my web.php routes file:

Route::get('/hashtags', function () {

    $hashtags = DB::table('hashtags')->get();
    return view('hashtags');
});

I have the database connected through the .env file and the overall connection in Laravel seems to be working, just not on this page. I was under the impression that the code in web.php defines the variable $hashtags and passes it to hashtags.blade.php. Apparently I'm missing something here.

I searched and found some other questions about passing data to the blade files, but nothing quite like this issue. Thanks!

Upvotes: 0

Views: 1150

Answers (4)

Adam
Adam

Reputation: 1309

Seems like you are not passing $hashtags to your view. I would suggest using compact in your Routes file. When you are moving to a controller, compact will work just fine there as well.

<h2>Your Hashtags</h2>
<table>
    <tr>
        <th>Hashtag</th>
        <th>List Name</th>
        <th>List Members Added</th>
    </tr>
@foreach($hashtags as $hashtag)
    <tr>
        <td>{{ $hashtag-> tagname }}</td>
        <td>{{ $hashtag-> listname }}</td>
        <td>Count</td>
    </tr>
@endforeach
</table>

And your Routes file:

Route::get('/hashtags', function () {
    $hashtags = DB::table('hashtags')->get();
    return view('hashtags', compact('hashtags'));
});

If you have a Hashtag model, you could do like this: (notice how you use the Hashtag model, rather then DB)

use App\Hashtag; // Add this at the routes files top

Route::get('/hashtags', function () {
    $hashtags = Hashtag::all();
    return view('hashtags', compact('hashtags'));
});

Upvotes: 1

Vishal Varshney
Vishal Varshney

Reputation: 905

You need to Passed the variable in your web.php route file

Try this

Route::get('/hashtags', function () {
    $hashtags = DB::table('hashtags')->get();
    return view('hashtags',compact('hashtags'));
});

Upvotes: 1

Farhad Hossen
Farhad Hossen

Reputation: 273

You can pass data in blade in 3 ways

1.  return view('hashtags', ['hashtags' => $hashtags]);
2   return view('hashtags')->with('hashtags', $hashtags);
3.  return view('hashtags', compact('hashtags'));

Upvotes: 1

Andrii Lutskevych
Andrii Lutskevych

Reputation: 1389

You need pass variable to template. Try:

return view('hashtags', [
    'hashtags' => $hashtags
]);

or

return view('hashtags', compact('hashtags'));

Upvotes: 2

Related Questions