php Laravel - Model::create 或 Model->save() 上的数组到字符串转换错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27524908/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:24:41  来源:igfitidea点击:

Laravel - Array to string conversion error on Model::create or Model->save()

phplaraveleloquent

提问by haakym

I'm performing a simple insert which I've done many times without any issues and for some odd reason it's not working and I get this error message:

我正在执行一个简单的插入,我已经做了很多次没有任何问题,但由于某些奇怪的原因它不起作用,我收到以下错误消息:

error: {type: "ErrorException", message: "Array to string conversion",…}
file: "C:\wamp\www\studentreg2\vendor\laravel\framework\src\Illuminate\Database\Grammar.php"
  line: 33
  message: "Array to string conversion"
  type: "ErrorException"

Here's my code:

这是我的代码:

$advisorCheck = AdvisorCheck::create([
            'status'         => Input::get('status'),
            'application_id' => Input::get('id'),
            'user_id'        => Auth::id()
        ]);

The migration for the advisor_check table which AdvisorCheck model uses seems fine, all foreign keys are unsigned and show the relations correctly in phpmyadmin, all values from the Input::get are strings, the model has the correct fields set as fillable (status, application_id, user_id).

AdvisorCheck 模型使用的 advisor_check 表的迁移似乎很好,所有外键都未签名并在 phpmyadmin 中正确显示关系,来自 Input::get 的所有值都是字符串,模型将正确的字段设置为可填充(状态、应用程序 ID) , 用户身份)。

I've even tried doing this in php artisan tinkerlike this:

我什至尝试过这样做php artisan tinker

AdvisorCheck::create([ 'status' => 'returned', 'application_id' => '3', 'user_id' => '4']);

and I get this response: Array to string conversion

我得到了这个回应:数组到字符串的转换

I've also tried this method and get the same error:

我也试过这个方法并得到同样的错误:

$advisorCheck                 = new AdvisorCheck;
$advisorCheck->status         = Input::get('status');
$advisorCheck->application_id = Input::get('id');
$advisorCheck->user_id        = Auth::id();
$advisorCheck->save();

Model code:

型号代码:

<?php

class AdvisorCheck extends \Eloquent {

    protected $fillable = ['status', 'application_id', 'user_id'];

    protected $table = ['advisor_check'];
}

If you need to see more code please ask.

如果您需要查看更多代码,请询问。

Many thanks to anyone who can help!

非常感谢任何可以提供帮助的人!

回答by lukasgeiter

As you can see in the example that's shown in the Laravel docsthe table property is a stringand not an array

正如您在Laravel 文档中显示的示例中所见, table 属性是一个字符串而不是数组

protected $table = 'advisor_check';

Which makes total sense if you think about it, since Eloquent models don't support multiple tables natively.

如果您考虑一下,这完全有意义,因为 Eloquent 模型本身不支持多个表。

回答by nilobarp

$tableshould be a string not array

$table应该是字符串而不是数组

<?php

   class AdvisorCheck extends \Eloquent {

   protected $fillable = ['status', 'application_id', 'user_id'];

   protected $table = 'advisor_check';
}