Reputation: 7715
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.
Upvotes: 1
Views: 1420
Reputation: 14921
You can re-assign the $foo
variable because the create
method will return the newly created instance.
$foo = $foo->create($data);
Upvotes: 3