I'm learning Laravel 4, so far so good. But for some weird reason blade's @foreach doesn't seem to work for a simple query. My code is:
Route:
Route::get('/users', function(){
$users = User::all();
return View::make('users/index')->with('users',$users);
});
Now in index.blade.php my code is:
@foreach ($users as $user)
<p>User: {{ $user->username }}</p>
@endforeach
The weird thing is that when I dump the object in the view, it does work:
{{ dd($users->toArray())}}
The DB data is displayed raw as an array.
I'm not really sure what am I doing wrong here, this is pretty much code from the beginners tutorial.
You should use a template/layout
(but you didn't use according to your view on Github ) and child views should extend it, for example, your index.blade.php
view should be look something like this:
// index.blade.php
@extends('layouts.master')
@section('content')
@foreach ($users as $user)
<p>User: {{ $user->username }}</p>
@endforeach
@stop
Now make sure that, in your app/views/layouts
folder you have a master.blade.php
layout and it contains something like this:
// master.blade.php
<!doctype html>
<html class="no-js" lang="">
<head>
<style></style>
</head>
<body>
<div class='content'>
@yield('content') {{-- This will show the rendered view data --}}
</div>
</body>
</html>
Also dd($users->toArray())
works because it dumps the $user->toArray()
using var_dump
and exits the script using die
function, the dd
means dump and die
.
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.