Thursday, March 2, 2017

[SOLVED]Property [comment] does not exist on this collection instance

I'm trying to get data from a column from table comment through the story table via the slug column. I've managed to do it with authors but I think I must be missing something or forgetting something because it is no longer working. I get this error
Exception in Collection.php line 1498: Property [comment] does not exist on this collection instance.
My controller function
public function slug($slug){
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $stories = Story::where('slug', $slug)->get();

    $slug = $slug;

    // I used this to get the author's email going through the slug from
    // story table
    $story = Story::with('author')->where('slug', $slug)->first();
    $name = $story->author->first()->email;

    // I would like to get the name in the comment table by going through
    // the slug from the story table
    $comment = Story::with('comment')->where('slug', $slug)->get();
    $test = $comment->comment->first()->name;

    return view('open::public.single-story', compact('menus_child', 'stories', 'slug'));
}

my Comment.php
namespace App\Modules\Authors\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = array('name', 'comment');

    protected $guarded = array('id');

    protected $table = 'comments';

    //Validation rules
    public static $rules = array(
        'name' => '',
        'comment' => ''
    );

    public function story(){
        return $this->belongsToMany('App\Modules\Authors\Models\Story', 'comment_story', 'comment_id', 'story_id');
    }
}

my Story.php
class Story extends Model
{
    protected $fillable = array('title', 'content', 'type', 'slug');

    protected $guarded = array('id');

    protected $table = 'story';

    //Validation rules
    public static $rules = array(
        'title' => '',
        'content' => '',
        'type' => ''
    );

    public function slug($title){
        $this->attributes['title'] = $title;
        $this->attributes['slug'] = Str::slug($title);
    }

    public function author(){
        return $this->belongsToMany('App\Modules\Authors\Models\Author', 'author_story', 'story_id', 'author_id');
    }

    public function comment(){
        return $this->belongsToMany('App\Modules\Authors\Models\Comment', 'comment_story', 'story_id', 'comment_id');
    }
}




via Isis

Advertisement