A
A
alex--n2017-03-22 23:25:04
Laravel
alex--n, 2017-03-22 23:25:04

Why does Laravel throw an error on assignment?

Good evening. I am not new to Laravel, after using 5.2 for a long time I decided to try 5.3. Decided to make a simple application. Created a migration

Schema::create('news', function (Blueprint $table) {
            $table->increments('id');
            $table->string('content');
            $table->timestamps();
        });

Described the model
class News extends Model
{
    protected $primaryKey = ['id'];
    protected $fillable = [
        'content', 'created_at', 'updated_at',
    ];
}

And when I try to assign like this:
$news = new News();
$news->content = $request->input('content');

Or even so
$news = new News();
$news->content = "test";

I get
Illegal offset type
at Model->getCasts() in Model.php line 2764
at Model->hasCast('content', array('date', 'datetime')) in Model.php line 2795
at Model->isDateCastable( 'content') in Model.php line 2884
at Model->setAttribute('content', 'test') in Model.php line 3478
at Model->__set('content', 'test') in AdminController.php line 29
In the database, this is a string, as elsewhere. But, it seems to me, for unknown reasons, he takes it for datetime. In general, what's wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alex--n, 2017-03-23
@alex--n

Problem solved.
protected $primaryKey = 'id';
not
protected $primaryKey = ['id'];
Noticed it right away)

A
Alexander Aksentiev, 2017-03-22
@Sanasol

decided to try 5.3

already 5.4 long ago
Well, try to register your castes
https://laravel.com/docs/5.4/eloquent-mutators#att...
Somewhere they did something wrong, since it takes a field for a date

R
Roman Igoshin, 2017-03-23
@MasterRO

By the way, created_at and updated_at can be omitted from fillable. And the error is clearly somewhere in the code, but from what is presented it is not visible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question