简体   繁体   中英

Laravel Eloquent create returning null id

I have an Eloquent model Foo , with a public bar array:

class Foo extends Model
{
    public $bar = ['a', 'b', 'c'];
}

In the store method of my controller I want to access these variables, and create a new Foo object:

public function store(Request $request)
{
    $foo = new Foo;

    foreach ($foo->bar as $field) {
        $data[$field] = $request->{$field};
    }

    $foo->create($data);

    return $foo->id;
}

The problem is that $foo->id is null , although the object is created successfully.

您可以重新分配$foo变量,因为create方法将返回新创建的实例。

$foo = $foo->create($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM