Laravel,视图中未定义的变量

Laravel, Undefined Variable in Views

我试图解决我的问题,但我被困在这里无法解决为什么变量在 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');
    }
}
?>

这是项目 class,我用 eloquent

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

虽然我有 items 的另一个功能,但我试图将其用作变量,因为这是 user.php[= 的文件35=] 函数定义在末尾

<?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: 未定义的变量:项目(视图: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');

您的路线可能指向错误 controller/method 因此变量未发送到视图。

尝试:

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

试试这个:

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

而不是这个:

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