Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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
Php 带有注释错误的Laravel帖子_Php_Laravel - Fatal编程技术网

Php 带有注释错误的Laravel帖子

Php 带有注释错误的Laravel帖子,php,laravel,Php,Laravel,因此,我创建了一个应用程序,其中有登录用户,用户可以发布,现在发布工作正常,但是当我放置一个注释函数时,我得到一个错误“为foreach()提供的参数无效” 我有这个在我的后模型 <?php class Post extends Eloquent{ protected $fillable = array('email', 'password','title','content'); protected $table = 'posts';

因此,我创建了一个应用程序,其中有登录用户,用户可以发布,现在发布工作正常,但是当我放置一个注释函数时,我得到一个错误“为foreach()提供的参数无效”

我有这个在我的后模型

<?php

    class Post extends Eloquent{

        protected $fillable = array('email', 'password','title','content');

        protected $table = 'posts';

        public function User()
        {
            return $this->belongsTo('User');
        }

        public function Comment()
        {
            return $this->hasMany('Comment', 'post_id');
        }
    }
在我看来

<section class="comments">  
            @foreach($posts->comments as $comment)
            <blockquote>{{$comment->content}}</blockquote>
            @endforeach
        </section>

@foreach($posts->comments as$comment)
{{$comment->content}
@endforeach
现在,当我尝试运行dd($posts->comment)时


它返回null,因为注释表为空。现在我想知道的是为什么我会犯这个错误?谢谢你的帮助,我只是好奇为什么会返回这个错误,我想解决这个问题。谢谢你的帖子模型中的函数名而不是注释,注释方法在hasMany方法调用下与错误的名称模型建立关系

public function Comment()
        {
            return $this->hasMany('Comments', 'post_id');
        }
尝试使用

@foreach($posts->Comment as$Comment)

如果仍然无法访问,则尝试立即加载。 就像你的控制器一样


$post=post::with(“comment”)->find($id)

我对拉威尔有点陌生,但是
Post::find
是一件事吗?你在哪里找到的?看看laravel docsI尝试了你的方法,但我返回了这个错误。类“Comment”未找到您使用的第一个或第二个方法?第一个,先生,您检查过我的关系方法吗?它正确吗?我猜它工作了,但它返回另一个错误:调用未定义的方法Comments::newQuery(),可以粘贴获取错误吗?为了更好的理解。因为你的场景对我来说很好用laravel 4.2。
<section class="comments">  
            @foreach($posts->comments as $comment)
            <blockquote>{{$comment->content}}</blockquote>
            @endforeach
        </section>
public function Comment()
        {
            return $this->hasMany('Comments', 'post_id');
        }