W
W
WebDev2018-09-28 17:17:11
Laravel
WebDev, 2018-09-28 17:17:11

Strange laravel error?

The model has a non-existent comma_tags property. These are tags separated by commas. It looks like this:

class Article  extends Model
{
protected $appends = ['comma_tags'];

public function tags()
    {
        return $this->belongsToMany('App\Models\Tag');
    }

    public function getCommaTagsAttribute()
    {
        return $this->tags->implode('name', ', ');
    }
}

That is, for each instance of the Article class, I can get a list of tags as a string.
Everything is fine, but now when I create a new article, I get a strange error
Column not found: 1054 Unknown column 'comma_tags' in 'field list'

I'm trying to create an article like this
Article::create([
    'name' => $name,
    'text' => $text
]);

That is, I don't even pass any comma_tags there. This is strange because it's not the only such "fake" property. But there were no such problems with others.
What can be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danny Chase, 2018-10-01
@Dase23

This is not a laravel error, but a SQL one.
Do It

dd(Article::create([
    'name' => $name,
    'text' => $text
]));

and see what ORM creates a query to the database. You’ll immediately see what’s the matter;)
I don’t understand - why do you need to create a non-existent property in the model) tell me?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question