0

I tried to work on my problem but i'm stuck here can't resolve why the variable is undefined in home.blade.php
This is my HomeController.php where i have $items variable which is causing problem

<?php
 use app\Item;
 namespace App\Http\Controllers;

class HomeController extends BaseController
{
    public function __construct(Item $items)
    {
        $this->items = $items;
    }

    public function getIndex()
    {
        $items = Auth::user()->items;

        return View::make('home', array(
        'items' => $items    
        ));
    }



    public function postIndex()
    {
        $id = Input::get('id');
        $useId = Auth::user()->id;

        $item = Item::findOrFail($id);

        if($item->owner_id == $userId)
        $item -> mark();

        return Redirect::route('home');
    }
}
?>

and this is Items class where i have extended it with eloquent

<?php
class Item extends Eloquent
{
    public function mark()
    {
        $this->done = $this->done?false:true;
        $this->save();
    }
}

while i have another function of items which i'm trying to use as a variable in view this is file of user.php and function is defined at the end

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function items()
    {
        return $this->hasMany('Item','owner_id');
    }
}

And this is the file from views home.blade.php where its giving error on foreach loop
Error: ErrorException in b5a9f5fc2ee329af8de0b5c94fd30f78 line 7: Undefined variable: items (View: C:\Users\Rehman_\Desktop\todo-application\resources\views\home.blade.php)

@extends('master')

@section('content')

    <h1>TO DO: Items</h1>
    <hr>

    <ul>
        @foreach ($items as $item)
        @endforeach
    </ul>

@stop

Update: Route.php file

<?php

Route::get('/',array('as'=>'home','uses'=>'PageController@getindex'))->before('auth');

Route::post('/',array('uses','HomeController@postIndex'))->before('csrf');

Route::get('/login',array('as'=>'login','uses' => 'Auth\AuthController@getLogin'))->before('guest');

Route::post('login',array('uses' => 'Auth\AuthController@postLogin'))->before('csrf');
19
  • In HomeController you could try return view('home',compact('items')) Commented Dec 18, 2015 at 15:31
  • i have already tried that @IE5Master :( i have even tried with view('home')->with('item',$item) but didn't help Commented Dec 18, 2015 at 15:33
  • everything seems fine. Are you sure your route is pointing to the correct controller and its method? Commented Dec 18, 2015 at 15:40
  • @Digitlimit i have updated the code with the route.php file can you have a look please ? Commented Dec 18, 2015 at 15:47
  • 1
    Try Route::get('/', ['middleware'=>'auth', 'as'=>'home','uses'=>'HomeController@getIndex'] ); Commented Dec 18, 2015 at 15:58

2 Answers 2

3

Try this:

return View('home', compact('items'));

Instead of this:

return View::make('home', array(
    'items' => $items    
));
Sign up to request clarification or add additional context in comments.

4 Comments

i have already tried this bro, however with the help of other guys i have changed the controller by changing it now says Class App\Http\Controllers\Item does not exist, now i guess it doesn't even find a class.
Did you add Item at the top of your controller? Ex: use App\Item;
after changing the name you will have to "composer dump-autoload", it will update the autoload_classmap.php file in vendor/composer with your new class name.
@smartrahat yes i have used it, now running composer dump
1

Your route is probably pointing to the wrong controller/method hence the variable is not been sent to the view.

Try:

 Route::get('/', [ 'as'=>'home','uses'=>'HomeController@getIndex'] );

7 Comments

why do we have to use 'middleware'=>'auth' here ? its sending me to localhost:8000/auth/login
Added it based on your code,. Have removed it. If you must use Auth::user()->items then you need to include it
Which URL is your request going to
when i use your method and run localhost:8000 it automatically re directs me to localhost:8000/auth/login
Remove the middleware see my updated answer. Hope you know what you are doing. I can see you are using $items = Auth::user()->items; Which means only authenticated user can get related lists
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.