Reputation: 618
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');
Upvotes: 0
Views: 6617
Reputation: 5649
Try this:
return View('home', compact('items'));
Instead of this:
return View::make('home', array(
'items' => $items
));
Upvotes: 3
Reputation: 17553
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'] );
Upvotes: 1