1

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');
        });
schel4ok
  • 634
  • 1
  • 11
  • 33
  • Gotta look in the network tab on devtools – Vinay Apr 25 '20 at 15:58
  • Cannot find anything useful in chrome devtools. `models: {data: {App\Models\NewsPost: 1, Encore\Admin\Auth\Database\Administrator: 1}, count: 2} count: 2 data: {App\Models\NewsPost: 1, Encore\Admin\Auth\Database\Administrator: 1} App\Models\NewsPost: 1 Encore\Admin\Auth\Database\Administrator: 1` – schel4ok Apr 25 '20 at 16:39

2 Answers2

1

Model::replicate returns the new instance you are creating (replicating the old one). You are not doing anything with this new model; you have not assigned it to a variable. You are updating the slug field of the existing model instance ($model) and trying to save the existing model instance, not the new one.

Also the new model instance won't have an id until after it is saved, not before.

If this is how you generate slugs everywhere then you have nothing to update on the existing model and the slug field is not considered dirty since it is the same as it was before you set it; so no save actually happens (because there is nothing to update).

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • If all that you mentioned is true, then how you can explain the fact that if I don't have unique fields in table then simple `$model->replicate()->save();` gives me new row in database table? – schel4ok Apr 25 '20 at 17:39
  • because `$model->replicate()` returns a new model instance and you are then calling `save` on that new model instance (saving a new record because it is an non existing model) ... though not sure what it is saving for the slug field ... i would probably have that field nullable so i could set the slug after it has been created – lagbox Apr 25 '20 at 17:41
  • slug cannot be nullable and should be unique, because it is part of URI – schel4ok Apr 25 '20 at 19:15
  • if `id` is not existing before new model instance is saved, then I expect we can use different text for the slug. For example `$model->replicate(['slug']); $model->slug = Str::slug($model->title, '-') . '-copy'; $model->save();` But it is also not working – schel4ok Apr 26 '20 at 07:42
  • because `$model` is the old model instance not the new replicated model ... again – lagbox Apr 26 '20 at 14:13
0

This is working.

        $new = $model->replicate();
        $new->slug = $new->slug . time();
        $new->save();
schel4ok
  • 634
  • 1
  • 11
  • 33