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());
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)