0

I am programing a blog using Laravel 5 and bootstrap. The @if control structure seem to not be working written this way in my blade.php:

@section('title')
@if($post)
    <p>  {{ $post->title}} </p>
    @if(!Auth::guest() && ($post->name == Auth::user()->id || Auth::user()->is_admin()))
        <button type="button" class="btn btn-default"><a href="{{ url('news.edit')}}">Editer</a></button>
    @endif
@endif
@endsection

Here is my function in PostController that calls the blade :

 public function index()
{
    $post = Post::get();

    return view('posts.index', compact('post'));
}

I encounter this error : Undefined property: Illuminate\Database\Eloquent\Collection::$title

I'm new to PHP, I can't seem to see where is the problem. If you have any clue, please help.

1

1 Answer 1

0

You need to attach a "post" variable when making a call to this view. In one of your controller methods. In your edited question, "title" is missing. Pass a "title" variable along with post. Basically any variable you want to use in views that are not defined in view (except global variables), need to be passed to it explicitly from the controller.

Edit for collections error.

Your post variable is actually an array, loop through it and access title or any property of a post for each member of the post array

Sign up to request clarification or add additional context in comments.

2 Comments

@Inas edited answer for collections error. I suggest you follow the introductory tutorial thoroughly.
just vardump() the variable. If its an array, which it mostly could be, then you need to use a foreach loop instead of a if loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.