T
T
truevolkolak2019-07-31 18:23:25
Laravel
truevolkolak, 2019-07-31 18:23:25

Laravel 5.8. Why doesn't the mutator work?

Hello!
I have a database table with a "slug" field. In migration: Since the field must be unique and generated automatically when a new record is added, I use a mutator in the model:
$table->string('slug');

public function setSlugAttribute($value) {
        $this->attributes['slug'] = Str::slug(mb_substr($this->manufacture, 0, 60) . "-", "-");
    }

The store method in the corresponding controller:
public function store(Request $request)
    {
        $manufacture = Manufacture::create($request->all());
        
        return redirect()->route('admin.manufactures.index');
    }

When the form is submitted, the 'slug' field is sent empty and should, in theory, be modified by this mutator before the new record is saved. But that doesn't happen. The following error appears in the browser:
SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value 
(SQL: insert into `manufactures` 
(`manufacture`, `description`, `meta_description`, `meta_keywords`, `country`) 
values 
(Производитель 2, описание, meta description, meta keywords, Россия))

Moreover, as you can see from the error, during bulk filling there is not even an attempt to save the value of the 'slug' field, although it is declared in the model in the $fillable array:
protected $fillable = [
        'manufacture',
        'slug',
        'country',
        'image',
        'description',
        'meta_description',
        'meta_keywords',
    ];

PS In another project on Laravel 5.7 exactly the same mutator works fine.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JhaoDa, 2019-07-31
@truevolkolak

Why should he work? The mutator mutates the value when it is set, either explicitly or implicitly.

there is not even an attempt to store the value of the 'slug' field, although it is declared in the model in the $fillable array
I recommend reading the documentation for what this field was invented for. Certainly not for extracting data from the request...

E
Eugene, 2019-07-31
@Nc_Soft

https://laravel.com/docs/5.8/eloquent#events

G
Grigory Vasilkov, 2019-08-01
@gzhegow

Either you didn't pass $_POST['slug'] / $_GET['slug']
Then you pass $value to slug, and then you don't use it - you take $this->manufacture. It is most likely empty, because you are creating a new model
1) that is, it will try SELECT * FROM manufacturers WHERE ... (an empty query to the database if the database is at 300 ping in the cloud, + 350ms to create the page)
2) even if it is will be done on an existing model - of course it will do a SELECT * and pull out all the fields for you. To do this, be sure to use with();
Stop what? you are in manufacture.php, there you have setSlug() and inside $this->manufacture.... Do you have a parent/child relationship inside this table? Where did you decide to get $this->manufacture from?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question