试图获得非物体的财产 - 拉拉维尔?

时间:2016-03-09 10:26:44

标签: php multithreading laravel

我正在做一个博客来学习如何使用laravel。博客大部分都已完成,但我仍然在代码中发现了一些错误。他们中的大多数已经解决,但仍然有一个我无法解决的错误。

这是关于用户的“我的主题”。如果用户使用了新帐户,他还会获得“我的主题”页面。如果他在页面上点击,他将获得所有线程的视图。我现在的问题是,如果他没有任何线程,他会得到一个:

尝试获取非对象异常的属性。我当然不希望他看到这个。

所以这是我的控制器中的“我的线程”页面中的函数:

public function startpage($id)
    {
//        $userthreads = Thread::query()->where('user_id', $id)->get();
//        return $userthreads;
        try {
            return view('test.startpage', [
                'userthreads' => Thread::query()->where('user_id', $id)->get(),
                'user' => User::query()->findOrFail($id)
            ]);
        } catch (ModelNotFoundException $e) {
            return view('test.notfound');
        }
    } 

使用前两行(未注释),我检查了数组中是否有某些内容。我刚刚得到了

'[]'

作为输出。这意味着数组是空的。我将$ userthreads变量返回到视图中,在我的视图中,我这样做是为了避免这个问题:

            @if(empty($userthreads))
                <a href="{{ action('Test\\TestController@add') }}">Add Thread </a>
            @else
            @foreach($userthreads as $uthread)
                    <ul>
                        <a href="{{ action('Test\\TestController@show', [$uthread->id]) }}">{{$uthread->thread}}</a>
                    </ul>
            @endforeach
            @endif

这对我没有用,我不明白为什么。也许你们中的某个人可以帮助我。

感谢您的帮助!

Alexey Mezenin的当前代码回答:

public function startpage($id)
    {
        try {
            $arr = [];
            $arr['userthreads'] = Thread::where('user_id', $id)->get();
            $arr['user'] = User::findOrFail($id);

            return view('test.startpage', $arr);

        } catch (ModelNotFoundException $e) {
            return view('test.notfound');
        }
    }

HTML:

@if(count($userthreads) > 0)
            <a href="{{ action('Test\\TestController@add') }}">Thread hinzufügen</a>
            @else
            @foreach($userthreads as $uthread)
                    <ul>
                        <a href="{{ action('Test\\TestController@show', [$uthread->id]) }}">{{$uthread->thread}}</a>
                    </ul>
            @endforeach
            @endif

错误:

Trying to get property of non-object (View: /var/www/laravel/logs/resources/views/test/startpage.blade.php)

3 个答案:

答案 0 :(得分:2)

好的,我们在聊天中讨论了它并发现了一个问题:

@if(Auth::user()->id == $userthreads->first()->user_id) 

答案 1 :(得分:0)

错误发生在你的startpage.blade.php

@if( Auth::user()->id == $userthreads->first()->user_id) 

并按以下方式更改

@if( isset($userthreads) && count($userthreads) > 0 && Auth::user()->id == $userthreads->first()->user_id) 

就是这样

@if(isset($userthreads) && count($userthreads) > 0)

  @foreach($userthreads as $uthread)
   <ul>
      <a href="{{ action('Test\\TestController@show', [$uthread->id]) }}">{{$uthread->thread}}</a>
   </ul>
  @endforeach

@else
  <a href="{{ action('Test\\TestController@add') }}">Thread hinzufügen</a>
@endif

答案 2 :(得分:0)

@ ItzMe488 我想替换以下代码:

@if( Auth::user()->id == $userthreads->first()->user_id)

@if( Auth::user()->id == $user->first()->user_id)

应该没有任何错误和错误。

<强>解释

在我们的聊天中@AlexeyMezenin发现该错误是因为$userthreads为空,$userthreads->first()因此错误。我认为既然您将$user传递给模板,使用它我们可以为当前用户进行身份验证,这可以防止用户B在用户A的页面中创建线程。