【问题标题】:Mutator is saving slug to DB but not the actual title - LaravelMutator 将 slug 保存到 DB 但不是实际的标题 - Laravel
【发布时间】:2020-10-24 22:09:00
【问题描述】:

我在 Laravel 7.24 上,现在我有一个 blogs 表、模型和控制器。我正在拨打/slug 的电话,并尝试使用mutator 和雄辩的create() 将博客文章保存为测试。奇怪的是,我的 mutator 将 slug 保存到 DB,但 DB 中的标题单元格保存为空字符串。如果我让 mutator 为标题工作,那么它会保存标题,但 slug 会保存为空字符串。为什么它只保存一个或另一个?

博客迁移:

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();
        });
    }

控制器方法

 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',
        ]);
    }

博客模型:

<?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;
    }
}

更新:我将表恢复为原始迁移,但所有其他代码都是相同的。

这是我得到的错误

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))

【问题讨论】:

  • 正确的 uniqueSlug 方法,不起作用
  • 您没有发送任何数据供 mutator 使用。你需要做类似'slug' =&gt; $post-&gt;title

标签: php laravel


【解决方案1】:

您不会发送任何数据供 mutator 在控制器内部使用。您需要执行类似'slug' =&gt; $post-&gt;title (或'slug' =&gt; 'This is a nice title', 根据您的示例)之类的操作,以便正确调用mutator,然后执行您告诉它的操作。您会收到 default value 错误,因为它由于未包含任何数据而跳过列填充。

【讨论】:

    【解决方案2】:

    这是因为 Laravel elequent 不知道您打算将 slog 列与其他列一起保存,因为您已将其设置为可为空。修改你的slog方法如下。

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

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 2017-05-15
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2020-05-17
      • 2018-07-23
      • 2015-08-15
      相关资源
      最近更新 更多