传递给 HasOneOrMany::save() 的 Laravel 参数 1 必须是给定的模型数组的实例

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

Laravel Argument 1 passed to HasOneOrMany::save() must be an instance of Model array given

phplaravelmodel

提问by David

I'm new at Laraveland I have a problem when I try to save array of data to database. Here's the error I get

我是Laravel 的新手,当我尝试将数据数组保存到数据库时遇到问题。这是我得到的错误

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given, called in S:\Documents\samdyk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php on line 267 and defined

传递给 Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() 的参数 1 必须是 Illuminate\Database\Eloquent\Model 的实例,给定数组,在 S:\Documents\samdyk\vendor\laravel\framework\src 中调用\Illuminate\Database\Eloquent\Relations\HasOneOrMany.php 在第 267 行并定义

So here's my code

所以这是我的代码

class Skill extends Model
{
    protected $fillable = ['skill_title', 'knowledge_level'];
}

class User extends Authenticatable
{
    use Notifiable;

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

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

    public function skills() {
        return $this->hasMany(Skill::class);
    }
}

And here's the controller function

这是控制器功能

public function editMyProfile(Request $request) {
        $user = Auth::user();

        dump($request->get('skills'));
       // dump($request->get('skills')[0]);
        dump($user->skills()->saveMany($request->get('skills')));
       return 1;
}

So here's $request->get('skills')data

所以这里的$request->get('skills')数据

array:5 [
  0 => array:2 [
    "skill_title" => "fghjghj"
    "knowledge_level" => "20"
  ]
  1 => array:2 [
    "skill_title" => "gjghjhgj"
    "knowledge_level" => "50"
  ]
  2 => array:2 [
    "skill_title" => "ghjhgjgfjh"
    "knowledge_level" => "80"
  ]
  3 => array:2 [
    "skill_title" => "hjkhgkkkhgjkjhkhjgk"
    "knowledge_level" => "53"
  ]
  4 => array:2 [
    "skill_title" => "jghjhgjhgj"
    "knowledge_level" => "57"
  ]
]

So as you can see I try to save an array (that's obvious). However even on laravel documentation I see this $post = App\Post::find(1);

如您所见,我尝试保存一个数组(这很明显)。然而,即使在 Laravel 文档中,我也看到了这个 $post = App\Post::find(1);

$post->comments()->saveMany([
    new App\Comment(['message' => 'A new comment.']),
    new App\Comment(['message' => 'Another comment.']),
]);

So why my code is wrong?

那么为什么我的代码是错误的呢?

回答by Alexey Mezenin

You need to pass an array of Skillobjects to the saveMany()method:

您需要将一组Skill对象传递给该saveMany()方法:

$skillModels = [];
foreach ($request->skills as $skill) {
    $skillsModels[] = new Skill($skill);
}

$user->skills()->saveMany($skillModels);

And you're passing a simple array.

你正在传递一个简单的数组。

回答by Elbo S.P.

Just try createMany instead :)

只需尝试 createMany :)

$post->comments()->createMany([
    ['message' => 'A new comment.'],
    ['message' => 'Another comment.'],
]);

See the doc: https://laravel.com/docs/7.x/eloquent-relationships#the-create-method

请参阅文档:https: //laravel.com/docs/7.x/eloquent-relationships#the-create-method