doumie7914 2014-05-29 11:57
浏览 23
已采纳

仅当Laravel中的Auth可用时才传递对象

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

  • 写回答

1条回答 默认 最新

  • doudu9094 2014-05-29 13:37
    关注

    Check if the user is logged in before setting the hero data:

    if (Auth::check())
    {
        View::composer(array('home'), function($view)
        {
            $view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?