php - Laravel Eloquent create returning null id


I have an Eloquent modelFoo, with a publicbar array:

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

In thestore method of my controller I want to access these variables, and create a newFoo object:

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

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


    return $foo->id;

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




You can re-assign the$foo variable because thecreate method will return the newly created instance.

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

People are also looking for solutions to the problem: How to only echo a value from MySQL once with PHP loop


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.