Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 此集合实例上不存在属性[comment]_Laravel - Fatal编程技术网

Laravel 此集合实例上不存在属性[comment]

Laravel 此集合实例上不存在属性[comment],laravel,Laravel,我试图通过slug列通过story表从table comment的列中获取数据。我已经设法和作者一起做了,但我想我一定是错过了什么或者忘记了什么,因为它已经不起作用了。我得到这个错误 Collection.php第1498行出现异常:此集合实例上不存在属性[comment] 我的控制器功能 public function slug($slug){ $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();

我试图通过slug列通过story表从table comment的列中获取数据。我已经设法和作者一起做了,但我想我一定是错过了什么或者忘记了什么,因为它已经不起作用了。我得到这个错误

Collection.php第1498行出现异常:此集合实例上不存在属性[comment]

我的控制器功能

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');
    }
}
尝试更改此选项:

//here you get a collection of stories 
$comment = Story::with('comment')->where('slug', $slug)->get();
为此:

//here you get a single story
$comment = Story::with('comment')->where('slug', $slug)->first();
尝试更改此选项:

//here you get a collection of stories 
$comment = Story::with('comment')->where('slug', $slug)->get();
为此:

//here you get a single story
$comment = Story::with('comment')->where('slug', $slug)->first();

我给你一个不同的方法来尝试

$comments = Comment::with(['story' => function ($query) {
    $query->where('slug', '=', $slug);
}])->get();

如果这对你想要实现的目标有效,那就试试吧。

我给你一种不同的尝试方法

$comments = Comment::with(['story' => function ($query) {
    $query->where('slug', '=', $slug);
}])->get();

如果这对你想要实现的目标有效,那就试试吧。

在dparoli的帮助下,我成功地做到了。我真不敢相信我这么久才弄明白

我变了

$comment = Story::with('comment')->where('slug', $slug)->get();
$test = $comment->comment->first()->name;

我把测试添加到

return view('open::public.single-story', compact('menus_child', 'stories', 'slug', 'test'));
然后加上

@foreach($test as $t)
  <h1>{!! $t['name'] !!}</h1>
  <p>{!! $t['comment'] !!}
@endforeach
@foreach($t作为测试)
{!!$t['name']!!}
{!!$t['comment']!!}
@endforeach

在我看来

在dparoli的帮助下,我成功地做到了这一点。我真不敢相信我这么久才弄明白

我变了

$comment = Story::with('comment')->where('slug', $slug)->get();
$test = $comment->comment->first()->name;

我把测试添加到

return view('open::public.single-story', compact('menus_child', 'stories', 'slug', 'test'));
然后加上

@foreach($test as $t)
  <h1>{!! $t['name'] !!}</h1>
  <p>{!! $t['comment'] !!}
@endforeach
@foreach($t作为测试)
{!!$t['name']!!}
{!!$t['comment']!!}
@endforeach

在我看来

那么如何显示与该故事相关的所有评论?也许@dparoli是对的。首先尝试他的解决方案,然后检查
comment
是否返回对象或集合。比如
dd($comment)
。原因是
first()
链接到的是故事,而不是注释。
$comment=Story::with('comment')->where('slug',$slug)->first()显示故事。但我似乎无法让所有的评论都显示出你的代码不仅仅是一个问题,试着一个一个地解决,并观察你在每一步上都得到了什么,就像@EddyTheDove建议的那样,这更容易。那么我如何显示与该故事相关的所有评论呢?也许@dparoli是正确的。首先尝试他的解决方案,然后检查
comment
是否返回对象或集合。比如
dd($comment)
。原因是
first()
链接到的是故事,而不是注释。
$comment=Story::with('comment')->where('slug',$slug)->first()显示故事。但是我似乎无法得到所有的评论来表明你的代码不仅仅是一个问题,试着一个一个地解决,并观察你在每一步上都得到了什么,就像@EddyTheDove建议的那样,这更容易。很高兴知道你自己找到了一个令人满意的解决方案,干得好。很高兴知道你自己找到了一个令人满意的解决方案,干得好。你们的关系正确吗?它们都设置为
belongToMany()
。我希望一个故事有一个作者(
belongsTo()
)和许多评论(
hasMany()
),相反,一个评论有一个故事(
belongsTo()
)。你们的关系正确吗?它们都设置为
belongToMany()
。我希望一个故事有一个作者(
belongsTo()
)和许多评论(
hasMany()
),相反,一个评论有一个故事(
belongsTo()
)。