Okay so I've got a view composer; that basically pulls a row from my table 'heros'. This then allow's me to obviously use the object $hero>whatever; on a page. The problem is this obviously only works for someone who is logged in.
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
As its using the
Auth::user()->
to find the correct row in the 'heros' table, once the user is no longer logged in it cannot access this. As no one is set to Auth.
So the error when I logout (or anyone no logged in) is this:
ErrorException
Trying to get property of non-object
));
//Authenticated group
Route::group(array('before' => 'auth'), function() {
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
How does one go about this?
Note: Still learning, I DID try and search but I found nothing probably because Im not sure how to put my issue into the correct sentance..
EDIT: Included home.blade.php
@extends('layout.main')
@section('content')
@if(Auth::check())
<p>Hello, {{{ Auth::user()->username }}}.</p>
@if($herodata)
Your hero:<br>
<b>{{ $herodata->hero; }}</b><br>
<b>Stats:</b>
LvL: {{ $herodata->level }}
Exp: {{ $herodata->exp }}
HP: {{ $herodata->hp }}/{{ $herodata->max_hp }}
Str: {{ $herodata->str }}
Atk: {{ $herodata->atk }}
Def: {{ $herodata->def }}
Int: {{ $herodata->int }}
Blk: {{ $herodata->blk }}
<form action="{{ URL::route('hero-delete') }}" method="POST">
<input type="submit" value="Delete"><br>
</form>
<form action="{{ URL::route('rest') }}" method="POST">
<input type="submit" value="Rest">
</form>
<form action="{{ URL::route('attack') }}" method="POST">
<input type="submit" value="Random attack">
</form>
<form action="{{ URL::route('hero-died') }}" method="POST">
<input type="submit" value="Died">
</form>
@else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
@if($errors->has('hero'))
{{ $errors->first('hero')}}
@endif
{{ Form::token()}}
</form>
@endif
@else
<p>You are not signed in.</p>
@endif
@stop