Amy B
Amy B

Reputation: 21

Eloquent Model, Illegal offset type error on non-date field

When I try to create a UserFailedLogin object I get an illegal offset type error. When I look closer the it appears to be trying to cast the ip address column to a date. The column is not a date in the DB nor is it in the $date of the model.

Here is my Model:

class UserFailedLogin extends Model {

public $timestamps = false;
protected $table = 'user_failed_login';
protected $fillable = ['ip','email'];
protected $primaryKey = ['invalid_login_id'];

Here is my create statement:

UserFailedLogin::create(['ip' => $request->ip(), 'email'=>$email,]);

My Error:

ErrorException in Model.php line 2759:

Illegal offset type

  1. in Model.php lne 2759
  2. at HandleExceptions->handleError('2', 'Illegal offset type', '.../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php', '2759', array()) in Model.php line 2759
  3. at Model->getCasts() in Model.php line 2743
  4. at Model->hasCast('ip', array('date', 'datetime')) in Model.php line 2774
  5. at Model->isDateCastable('ip') in Model.php line 2863

Upvotes: 2

Views: 7338

Answers (3)

Ahmed Samir
Ahmed Samir

Reputation: 150

This problem occurred because the eloquent thinks your primary key is a composite primary key, cause you added it to array, in your case just make the primary key a string, in the really composite primary keys you should use "Mass Updates". I hope this helps you.

Upvotes: 2

joncampbell
joncampbell

Reputation: 624

I think the issue is that your primary key is an array instead of just a string.

Try changing that to be: protected $primaryKey = 'invalid_login_id';

The getCasts is looking for a string and not an array or object.

Upvotes: 5

Ohgodwhy
Ohgodwhy

Reputation: 50787

->ip() is not a function, is it a member accessor, it should be ->ip or ->get('ip'). That is to say, unless you've create a custom Request class which implements the IP function, although I do not see that being the case given your stack trace.

Upvotes: 1

Related Questions