12823

Laravel Blade @foreach not working

Question:

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)

User: {{ $user->username }}

@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.

Answer1:

You should use a template/layout (but you didn't use according to your <a href="https://github.com/OmarGRocha/laratest/blob/master/views/users/index.blade.php" rel="nofollow">view on Github</a>) 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)

User: {{ $user->username }}

@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.

Recommend

  • Low impact encryption formula for ROBLOX Lua
  • Prime numbers c++
  • C99′s Fixed-Width Integer Types
  • How to use grep to output unique lines of code from a file?
  • spring security manual login best practice
  • How to delete first 7 characters of folder name by using batch script?
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • Is possible having two COM STA instances of the same component?
  • 'float' object cannot be interpreted as an integer
  • how to add dashed border on highcharts “area” graph for every point
  • Generate and export point cloud from Project Tango
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Year over Year Stats from a Crossfilter Dataset
  • How to define something in JavaScript [closed]
  • Floating parent div grows to hypothetical width of floating child div
  • C++ STL stack pop operation giving segmentation fault
  • JQuery: Infinite input select
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • DataTables move rows between tables
  • Cloud Code: Creating a Parse.File from URL
  • iphone image to video issue in video speed
  • reshape/remould data frame to create normalized bar chart and pie chart
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to decleare char *const argv[] in swift [duplicate]
  • read part of h5 dataset python
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • Computing the discrete fourier transform of audio data with FFTW
  • Grails - How to implement a foreign key relationship not using an id column?
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • convert json to excel in java
  • ReferenceError: TextEncoder is not defined
  • Create/delete users from text file using Bash script
  • How to check if object is null in Java?