Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mmstaniewski@gmail.com's avatar

Can't pass data into view

Hello,

Don't know if writing in proper channel, but i've a trouble with passing data into view.

I've tried in few way:

  1.  $articles = Article::all();
     View::share('articles', $articles);
     return View::make('dashboard');
    
  2.  $articles = Article::all();
     return View::make('dashboard')->with('articles', $articles);
    
  3.  $articles = Article::all();
     return View::make('dashboard', array('articles' => $articles));
    

All these codes are of course in the index method of DashboardController.

Always i get: Undefined variable: articles (View: /Users/Ultearie/Sites/Czysta/Czysta/resources/views/dashboard.blade.php)

All layouts looks like: app.blade.php

@yield('content')

dashboard.blade.php @extends('app')

@section('content') @foreach ($articles as $article)

    <tr>
        <td><input type="checkbox" name="delete" value="{{ $article->id }}"/></td>
        <td>{{ $article->title }}</td>
    </tr>

@endforeach

Sorry if wrote in wrong section but i really searched through several sites, tutorials etc and nothing helped

0 likes
19 replies
bobbybouwmann's avatar

Give this a try

Laravel 4:

public function index()
{
    $articles = Article::all();

    return View::make('dashboard', compact('articles'));
}

Laravel 5:

public function index()
{
    $articles = Article::all();

    return view('dashboard', compact('articles'));
}
mmstaniewski@gmail.com's avatar

Still no effect :c

Undefined variable: articles (View: /Users/Ultearie/Sites/Czysta/Czysta/resources/views/dashboard.blade.php)

bobbybouwmann's avatar

Your code looks fine! Have you tried passing another variable in your view to check if that worked?

mmstaniewski@gmail.com's avatar

I've tried with User::all() and with value set by me like

$articles = Article::all(); return View::make('dashboard')->with('test', 'test variable');

None worked

bobbybouwmann's avatar

You are sure you have data? Can you give this a try:

public function index()
{
    $articles = Article::all();

    dd($articles);
    
    return View::make('dashboard', compact('articles'));
}
bobbybouwmann's avatar

it does a var_dump on the variable and kills the script. If this doesn't give you anything then you're probably not calling this function correctly! Can you show me your routes.php?

mmstaniewski@gmail.com's avatar

Yeah,

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::get('login', 'auth@index');

Route::get('admin', 'DashboardController@index');

Route::controllers([ 'auth' => 'Auth\AuthController', 'password' => 'Auth\PasswordController', ]);

Its an admin route, quite fresh laravel instance

mmstaniewski@gmail.com's avatar

public function index() {

    $articles = Article::all();

    dd($articles);

    return view('dashboard', compact(articles));

}

use Illuminate\Database\Eloquent\Model;

class Article extends Model {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'articles';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['title', 'data', 'place', 'article'];

}

{{ $articles }}

mmstaniewski@gmail.com's avatar

Now i can't get my models to work...

FatalErrorException in DashboardController.php line 33: Class 'App\Http\Controllers\User' not found

with code:

public function index(){ $articles = User::all(); return view('welcome')->with('test', $articles); }

I'm new to laravel, sorry for such a dumb questions, but have i to use "use" of my Model namespace? Show me example please

RomainLanz's avatar

@Ultearie You need to import User class.

<?php namespace Cpnv\Http\Controllers;

use App\User;

...

Look at some video about Laravel 4/5 fundamentals and OOP, it'll help you. :)

1 like

Please or to participate in this conversation.