i was trying to display news with its comments, but one of the headlines is giving me an error,but others are working, i have a table of news and i have a table of news_comments, some of the news are being retrieved perfectly with their comments but this one is giving me an error.
@extends('layouts.app')
@section('content')
<div class="container">
{{$news->title}}<br/>
@foreach($news->news_pictures as $news_picture)
<img src="{{asset($news_picture->pictures)}}" width="200"><br/>
@endforeach
{!! $news->body !!} <br/>
<small>written on{{$news->created_at}} by {{$news->user->name}} </small>
</div>
@foreach($news->news_comments as $news_comment)
@if(!Auth::guest())
<div class="well">
<a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
@else
<div class="well">
{{$news_comment->commentor}}<br/>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
@endif
@endforeach
@if(!Auth::guest())
<form action="{{action('NewsController@AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
@else
<form action="{{action('NewsController@AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<input type="text" class="form-control" placeholder="your name" name="commentor">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
@endif
@endsection
This error occur when the property you want to access that doesn't exist so you should check property isset
or not
Here is the example
@if (!is_null($news_comment->user))
<div class="well">
<a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
@endif
OR
@if (isset($news_comment->user->id) && isset($news_comment->user->name))
<div class="well">
<a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
@endif
Try to use: with ()
on user
<a href="{{action('ProfileController@show', [$news_comment->user()->id, $news_comment->user()->name])}}">{!! $news_comment->user()->name!!}</a>
Instead of:
<a href="{{action('ProfileController@show', [$news_comment->user->id, $news_comment->user->name])}}">{!! $news_comment->user->name!!}</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.