【问题标题】:BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::messages does not exist.'带有消息“方法 Illuminate\Database\Query\Builder::messages 不存在”的 BadMethodCallException。
【发布时间】:2018-09-20 12:20:38
【问题描述】:

我正在尝试使用 Laravel 和 Vue.js 制作一个聊天框。我在网上关注这个tutorial。我几乎每一步都走到了发球台,我不知道为什么我没有得到想要的结果。这是我迄今为止所做的:

我创建了一个用户模型和一个消息模型,其中包含正确的表列和迁移。在 User 模型中,我与 Message 模型建立了 hasMany 关系。在Message模型中,我与User建立了belongsTo关系。

当我进入修补程序时,我可以:

factory(App\User::class)->create() 

很好,就像教程中的人一样。但是,当我尝试这样做时: App\User::find(4)->messages()->created(['message'=> "Hello from Sharon"])

我得到这个错误:

BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::messages does not exist.'

这是我的代码:

用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','api_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function messages()
    {
        return $this->hasMany(Message::class);
    }
}

消息模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
    protected $fillable = ['message'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
 }

消息迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMessagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->text('message');
            $table->integer('user_id')->unsigned();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('messages');
    }
}

如果您能告诉我我做错了什么,我将不胜感激。谢谢。

【问题讨论】:

    标签: laravel vue.js migration faker tinker


    【解决方案1】:

    您似乎收到此错误:

    BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::created does not exist.'
    

    要将模型保存到关系中,请使用create 方法,而不是created 方法,例如:

    App\User::find(4)->messages()->create(['message'=>'Hello from Sharon']);
    

    【讨论】:

      【解决方案2】:

      而不是App\User::find(4)-&gt;messages()-&gt;created(['message'=&gt; "Hello from Sharon"])

      尝试使用

      App\User::find(4)->messages()->create(['message'=> "Hello from Sharon"])
      

      或

      App\User::find(4)->messages()->save(['message'=> "Hello from Sharon"])
      

      【讨论】:

        【解决方案3】:

        重新启动 php artisan tinker 并重新运行您的代码。它有效:)

        【讨论】:

          猜你喜欢
          • 2020-02-24
          • 1970-01-01
          • 2016-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-24
          • 2019-01-24
          • 2019-10-04
          相关资源
          最近更新 更多