phplaravel

Mutator is saving slug to DB but not the actual title - Laravel


I am on Laravel 7.24 and right now I have a blogs table, model and controller. I am making a call to /slug and am trying to save a blog post as a test using a mutator and the eloquent create(). The odd thing is with my mutator it saves the slug to the DB but the title cell in the DB saves as an empty string. If I make the mutator work for the title then it saves the title but the slug saves as an empty string. Why is it only saving one or the other?

Blogs migration:

public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('post');
            $table->string('postExcerpt');
            $table->string('slug')->unique();
            $table->string('user_id');
            $table->string('featuredImage');
            $table->string('metaDescription');
            $table->integer('views')->default(0);
            $table->timestamps();
        });
    }

Controller method

 public function slug()
    {
        return Blog::create([
            'title' => 'This is a nice title',
            'post' => 'some post',
            'postExcerpt' => 'some post here',
            'user_id' => 11,
            'metaDescription' => 'some meta info here',
        ]);
    }

Blog model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Blog extends Model
{
    protected $fillable = [
        'title',
        'post',
        'postExcerpt',
        'slug',
        'user_id',
        'featuredImage',
        'metaDescription',
        'views'
    ];

    public function setSlugAttribute($title){
        $this->attributes['slug'] = $this->uniqueSlug($title);
    }

    private function uniqueSlug($title)
    {
        $slug = Str::slug($title, '-');
        $count = Blog::where('slug', 'LIKE', "{$slug}%")->count();
        $newCount = $count > 0 ? ++$count : '';

        return $newCount > 0 ? "$slug-$newCount" : $slug;
    }
}

UPDATE: I reverted the table back to the original migration but all other code is the same.

and this is the error I am getting

SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value (SQL: insert into `blogs` (`title`, `post`, `postExcerpt`, `user_id`, `metaDescription`, `updated_at`, `created_at`) values (This is a nice title, some post, some post here, 11, some meta info here, 2020-10-24 22:28:07, 2020-10-24 22:28:07))

Solution

  • You're not sending any data for the mutator to work with inside your controller. You need to do something like 'slug' => $post->title (or 'slug' => 'This is a nice title', as per your example) in order for the mutator to be called correctly and then do what you're telling it to. You're getting the default value error because it's skipping the column population due to not being hydrated with any data.