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

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿