laravel BadMethodCallException 带有消息“方法 Illuminate\Database\Query\Builder::messages 不存在”。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49761104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::messages does not exist.'
提问by Toti Cuervo
I am trying to make a chatbox using Laravel and Vue.js. I am following this online tutorial. I have followed almost every step to a tee and I cannot tell why I am not getting the desired result. Here is what I have done thus far:
我正在尝试使用 Laravel 和 Vue.js 制作一个聊天框。我正在关注这个在线教程。我几乎每一步都走到了发球台,但我不知道为什么我没有得到想要的结果。这是我迄今为止所做的:
I have created a User model and a Message model with the correct table columns and migrations. In the User model I established a hasMany relationship with the Message model. In the Message model, I established a belongsTo relationship with User.
我创建了一个 User 模型和一个 Message 模型,其中包含正确的表列和迁移。在 User 模型中,我与 Message 模型建立了 hasMany 关系。在Message模型中,我和User建立了belongsTo关系。
When I go into tinker, I can do:
当我进入修补程序时,我可以这样做:
factory(App\User::class)->create()
just fine, like the guy in the tutorial can do. However when I try to do:
App\User::find(4)->messages()->created(['message'=> "Hello from Sharon"])
就好了,就像教程中的那个人一样。但是,当我尝试这样做时:
App\User::find(4)->messages()->created(['message'=> "Hello from Sharon"])
I get this error:
我收到此错误:
BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::messages does not exist.'
Here is my code:
这是我的代码:
User Model:
用户模型:
<?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);
}
}
Message Model:
消息模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $fillable = ['message'];
public function user()
{
return $this->belongsTo(User::class);
}
}
Message Migration:
消息迁移:
<?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');
}
}
If you could let me know what I am doing wrong, I would really appreciate it. Thanks.
如果您能让我知道我做错了什么,我将不胜感激。谢谢。
回答by Lohith
Restart the php artisan tinker and re-run your code. It works:)
重新启动 php artisan tinker 并重新运行您的代码。有用:)
回答by Ben
Seems you are getting this error:
似乎您收到此错误:
BadMethodCallException with message 'Method Illuminate\Database\Query\Builder::created does not exist.'
To save models to relationships, use create
method, but not created
method, for example:
要将模型保存到关系,请使用create
方法,而不是created
方法,例如:
App\User::find(4)->messages()->create(['message'=>'Hello from Sharon']);
回答by Lavkush Gupta
instead of App\User::find(4)->messages()->created(['message'=> "Hello from Sharon"])
代替 App\User::find(4)->messages()->created(['message'=> "Hello from Sharon"])
try to use
尝试使用
App\User::find(4)->messages()->create(['message'=> "Hello from Sharon"])
or
或者
App\User::find(4)->messages()->save(['message'=> "Hello from Sharon"])