Search code examples
laravelreplicatelaravel-admin

cannot replicate model with unique slug field


I cannot understand how to replicate model with unique slug field. I see success message, but this code doesn't create additional row into DB table. And no messages or exceptions in debugbar.


    public function handle(Model $model)
    {
        $model->replicate(['slug']);
        $model->slug = Str::slug($model->title, '-') . $model->id;
        $model->save();
        return $this->response()->success('Скопировано!')->refresh();
    }

And if I add dd($model) somewhere in the middle it doesn't help me, because I don't see anything except Oops message. enter image description here Here is migration file

        Schema::create('news_posts', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->string('title', 255)->nullable();
            $table->string('slug', 255)->unique();
            $table->text('fulltext')->nullable();
            $table->string('image', 255)->nullable();
            $table->boolean('is_published')->default(false);
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('category_id')->references('id')->on('news_categories');
            $table->index('is_published');
        });

Solution

  • This is working.

            $new = $model->replicate();
            $new->slug = $new->slug . time();
            $new->save();