BadMethodCallException使用children_comments()

埃米尔·盖尔·维尔姆森

我目前正在尝试发表评论回复(多语),但我不断收到错误消息:

BadMethodCallException调用未定义的方法Illuminate \ Database \ Query \ Builder :: children_comments()

这是我的方法:

public function commentsReply(Requests\CreateCommentsRequest $request, $comment)
{
$comments = Comments::whereId($comment)->first();

    $comment = new ChildrenComment(Request::all());
    $comment->pubslished_at = Carbon::now();
    $comment->user()->associate(Auth::user());
    $comment->children_comments()->associate($comments);
    $comment->save();

    return Redirect::back();
}

这是我的模型:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class ChildrenComment extends Model {
protected $fillable = [
        'id',
        'user_id',
        'post_id',
        'parent_id',
        'comment'
    ];

    public function setPublishedAtAttribute($date)
    {
        $this->attributes['pubslished_at'] = Carbon::parse($date);
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
    public function comments()
    {
        return $this->belongsTo('App\Comments');
    }

}

如果需要,这是我的迁移方案:

public function up()
{
    Schema::create('children_comments', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('post_id')->unsigned();
        $table->integer('parent_id')->unsigned();
        $table->text('comment');
        $table->string('fileToUpload')->default("uploads/images/comments/NFF4D00-0.png");
        $table->timestamps();;
        $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');
        $table->foreign('post_id')
                ->references('id')
                    ->on('posts')
                    ->onDelete('cascade');
        $table->foreign('parent_id')
                    ->references('id')
                    ->on('comments')
                    ->onDelete('cascade');
    });
}
莫波

associate以错误的方式调用该方法。children_comments()您在此处调用的方法在任何地方都不存在:

$comment->children_comments()->associate($comments);

要将aComments与a关联ChildrenComment您应该这样做:

$comment->comments()->associate($comments);

最后一点:我发现您命名变量的方式非常令人困惑(特别是因为您对具有单个值的变量使用复数形式)。我会这样:

//is't only one comment so 'comment' not 'comments'
$comment = Comments::whereId($comment)->first();

//$childComment is another object, so give it another name
$childComment = new ChildrenComment(Request::all());
$childComment->pubslished_at = Carbon::now();
$childComment->user()->associate(Auth::user());

//this statement represent a relation with one element, so name it 'comment()', not 'comments()'
$childComment->comment()->associate( $comment );
$childComment->save();

我认为这将使它更具可读性

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

与with()一起使用时的Laravel 5 BadMethodCallException

来自分类Dev

BadMethodCallException Laravel

来自分类Dev

BadMethodCallException 解释?

来自分类Dev

Flutter:使用“ children:<Widget>”还是“ children:”?

来自分类Dev

使用 TypeScript 反应 children[] 问题

来自分类Dev

Laravel Artisan迁移[BadMethodCallException]

来自分类Dev

Laravel Artisan迁移[BadMethodCallException]

来自分类Dev

当使用:first时children()和find()的行为

来自分类Dev

ReactJS:为什么使用this.props.children?

来自分类Dev

在jQuery中使用.children和.find

来自分类Dev

我可以在匹配的 Children 中使用 jQuery

来自分类Dev

BadMethodCallException 属于多个 Laravel 5.4

来自分类Dev

Laravel dusk 宏抛出的 BadMethodCallException

来自分类Dev

如果还使用children参数,请使用redirectTo,这可能吗?

来自分类Dev

react-router 1.0:使用this.props.children代替RouteHandler

来自分类Dev

应该如何使用OneDrive [email protected]参数

来自分类Dev

jQuery:如何在表中使用“>”和children()

来自分类Dev

使用 JQuery $.children() 和 $.each() 遍历 data-* 元素?

来自分类Dev

如何在 {this.props.children} 中使用 React-router

来自分类Dev

Laravel:BadMethodCallException方法[查找]不存在

来自分类Dev

Laravel:BadMethodCallException方法[存储]不存在

来自分类Dev

BadMethodCallException 方法 [插入] 不存在。在 Laravel

来自分类Dev

努力使django_comments与Django REST Framework一起使用

来自分类Dev

路由错误没有路由匹配[POST]“ / comments / new”-使用范围

来自分类Dev

“ comments_path”中使用“ Commentable” form_for多态关联的未定义方法

来自分类Dev

jQuery-.children()。children()与.find()

来自分类Dev

BadMethodCallException方法App \ Http \ Controllers \ TaskController :: destory不存在

来自分类Dev

我通过MongoDB从Laravel护照获取BadMethodCallException“ Client :: confidential()”

来自分类Dev

BadMethodCallException调用未定义的方法App \ User :: map()

Related 相关文章

  1. 1

    与with()一起使用时的Laravel 5 BadMethodCallException

  2. 2

    BadMethodCallException Laravel

  3. 3

    BadMethodCallException 解释?

  4. 4

    Flutter:使用“ children:<Widget>”还是“ children:”?

  5. 5

    使用 TypeScript 反应 children[] 问题

  6. 6

    Laravel Artisan迁移[BadMethodCallException]

  7. 7

    Laravel Artisan迁移[BadMethodCallException]

  8. 8

    当使用:first时children()和find()的行为

  9. 9

    ReactJS:为什么使用this.props.children?

  10. 10

    在jQuery中使用.children和.find

  11. 11

    我可以在匹配的 Children 中使用 jQuery

  12. 12

    BadMethodCallException 属于多个 Laravel 5.4

  13. 13

    Laravel dusk 宏抛出的 BadMethodCallException

  14. 14

    如果还使用children参数,请使用redirectTo,这可能吗?

  15. 15

    react-router 1.0:使用this.props.children代替RouteHandler

  16. 16

    应该如何使用OneDrive [email protected]参数

  17. 17

    jQuery:如何在表中使用“>”和children()

  18. 18

    使用 JQuery $.children() 和 $.each() 遍历 data-* 元素?

  19. 19

    如何在 {this.props.children} 中使用 React-router

  20. 20

    Laravel:BadMethodCallException方法[查找]不存在

  21. 21

    Laravel:BadMethodCallException方法[存储]不存在

  22. 22

    BadMethodCallException 方法 [插入] 不存在。在 Laravel

  23. 23

    努力使django_comments与Django REST Framework一起使用

  24. 24

    路由错误没有路由匹配[POST]“ / comments / new”-使用范围

  25. 25

    “ comments_path”中使用“ Commentable” form_for多态关联的未定义方法

  26. 26

    jQuery-.children()。children()与.find()

  27. 27

    BadMethodCallException方法App \ Http \ Controllers \ TaskController :: destory不存在

  28. 28

    我通过MongoDB从Laravel护照获取BadMethodCallException“ Client :: confidential()”

  29. 29

    BadMethodCallException调用未定义的方法App \ User :: map()

热门标签

归档