3

I just finishing my blog, using laravel, and I want to add a comment feature on it, but i got few errors like this, can anyone help me, please??, sorry for my English, English is not my native language, Thank you :)

(1/1) ErrorException
Undefined offset: 1

Here is my AdminBlog.php Model

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class AdminBlog extends Model
{
    protected $table = 'admin';
    protected $fillable = ['title','text','images','slug'];

    public function comment(){
        return $this->hasMany('App\Comment');
    }
}

Comment.php Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $table = 'comment';
    protected $fillable = ['name','email','text','post_id'];

    public function post(){
        return $this->belongsTo('App\AdminBlog');
    }
}

BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\AdminBlog;
use App\Comment;

class BlogController extends Controller
{
    //Index
    public function index(){
        $post   = AdminBlog::all();
        return view('blog/index', compact('post'));
    }

    //Show
    public function show($id){
        $post       = AdminBlog::findOrFail($id);
        $comment    = Comment::all();
        //dd($comment);
        return view('blog/show', ['post'    => $post,
                                  'comment' => $comment]);
    }
}

show.blade.php

<div class="col-md-12 post-comment-show">
        @foreach($post->comment() as $list)
            <p>{{ $list->text }}</p>
        @foreach
</div>

3 Answers 3

1

You should use:

<div class="col-md-12 post-comment-show">
    @foreach($post->comment as $list)
        <p>{{ $list->text }}</p>
    @foreach
</div>

Note that, you have used comment(). Also, you should use a plural name for a hasMany relationship, so comment should be comments.

Also, in your show method, you should use something like the following:

public function show($id)
{
    $post = AdminBlog::with('comment')->findOrFail($id);

    return view('blog/show', ['post' => $post]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try the following

$post->comment()->get()

or

$post->comment

when calling a relationship with ()it returns a instance of the query builder and allows you to filter the object. If you remove the call it without () it returns you a collection which is array accessible

Another suggestion if you have many comments you should name your relationship plural.

in this case:

  public function comments(){
      return $this->hasMany('App\Comment');
  }

  // Use it like this
  $post->comments

Comments

0

You need to change your show function:

public function show($id){
  $post       = AdminBlog::findOrFail($id);
  $comment    = Comment::all();
  //dd($comment);
  return view('blog/show', ['post'    => $post,
                                  'comment' => $comment]);
}

TO

public function show($id){
  $post = AdminBlog::with('comment')->where('admin.id',$id)->get();

  return view('blog/show', compact('post'));
}

Hope this work for you!

Comments

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.