Php 尝试获取非对象的属性-Laravel?

Php 尝试获取非对象的属性-Laravel?,php,multithreading,laravel,Php,Multithreading,Laravel,我正在写一个博客来学习如何使用laravel。博客基本上已经完成了,但我仍然在代码中发现了一些错误。大多数问题都已解决,但仍有一个错误我无法修复 这是关于用户的“我的线程”。如果用户创建了一个新帐户,他也会得到一个“我的线程”页面。如果他在这一页上闲逛,他会看到他所有的帖子。我现在的问题是,如果他没有任何线程,他将得到: 正在尝试获取非对象异常的属性。我当然不想让他看到这个 这就是我的控制器中的函数到他的“我的线程”页面: public function startpage($id) {

我正在写一个博客来学习如何使用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
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

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

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

错误在您的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
@if(isset($userthreads)&count($userthreads)>0)
@foreach($userthreads作为$uthread)
@endforeach @否则 @恩迪夫
@ItzMe488 我想替换以下代码:

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

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

应该工作没有任何错误和错误

说明:


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

您是否尝试过将if条件@if(isset($userthreads))改为空或通过count@if(count($userthreads)>0进行更改我两个都试过了,但似乎都没用。两次尝试都给了我相同的错误消息..用else更改(交换)if bloack的内容,用if更改(交换)else block的内容。我尝试了conld方法和catch逻辑,但都不起作用:/controller函数不会有问题:/if用户有线程,一切都会起作用。。这就是为什么我不认为这是一个错误为什么你这么肯定?看看你的错误,我认为错误在
op()
action.mhh。。我删除了->query()部分,但不管你是从我的代码还是你的代码中删除了它,都没有用?相同的错误还是不同的错误?
@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