Reputation: 1362
I want to get the the id of the last inserted row in a database so I created a static field in my model:
public static $lastid;
and I try to override the create method:
public static function create($data){
parent::create($data);
$lastid = DB::getPdo()->lastInsertId();
}
now I have an Error exception saying:
Declaration of Actor::create() should be compatible with Illuminate\Database\Eloquent\Model::create(array $attributes)
how can I make this work?
Upvotes: 0
Views: 2930
Reputation: 18665
When you use the Model::create
method you can grab the ID straight from the result.
$actor = Actor::create(array('name' => 'Jason'));
dd($actor->id);
You shouldn't need to use the lastInsertId
method from the PDO object.
Upvotes: 1
Reputation: 1151
Here, the keyword array before $data
will print a fatal error if the data are not an array.
Like is said in the error, your class extends Illuminate\Database\Eloquent\Model
and should be compatible with his parent.
public static function create(array $data = array()){
parent::create($data);
$lastid = DB::getPdo()->lastInsertId();
}
Upvotes: 2