Kiy
Kiy

Reputation: 31

Laravel Undefined offset error

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>

Upvotes: 3

Views: 2708

Answers (3)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

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!

Upvotes: 0

Ken
Ken

Reputation: 2734

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

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

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]);
}

Upvotes: 1

Related Questions