Laravel,视图中未定义的变量

时间:2015-12-18 15:18:21

标签: laravel-5 undefined

我试图处理我的问题,但我被困在这里无法解决为什么变量未定义 home.blade.php
这是我的HomeController .php我有 $ items 变量导致问题

<?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');
    }
}
?>

这是Items类,我用eloquent

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

虽然我有的其他功能,我试图将其用作视图中的变量,这是 user.php 的文件,并且函数定义于结束

<?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');
    }
}

这是来自 home.blade.php 的视图中的文件,其中它在foreach循环上给出错误
错误:b5a9f5fc2ee329af8de0b5c94fd30f78第7行中的ErrorException: 未定义的变量:items(查看: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

更新: Route.php 文件

<?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');

2 个答案:

答案 0 :(得分:2)

试试这个:

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

而不是:

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

答案 1 :(得分:1)

您的路线可能指向错误的控制器/方法,因此该变量未发送到视图。

尝试:

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