我一直收到此错误SQLSTATE [23000]:完整性约束违规:1062重复条目''为关键'videos_slug_unique'我该怎么做才能解决它

时间:2023-01-31 07:38:41

I have a site I created that has a blog section and a video section. I had a config file for blog that I tried to share with the video section but it wasn't working properly so I created a separate config for video. and modified my column names for video from

我有一个我创建的网站,其中包含博客部分和视频部分。我有一个博客的配置文件,我试图与视频部分共享,但它没有正常工作,所以我创建了一个单独的视频配置。并修改了我的视频列名

    $title and $subtitle 

to

    $v_title and $v_subtitle. 

but when I do I get the following error

但是当我这样做时,我得到以下错误

    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'videos_slug_unique'

for some reason a slug is not being produced and this causes the issue. The slug is unique in my table. If I change the variables to how they were

由于某种原因,没有产生slu and,这就引起了这个问题。 slu is在我的餐桌上是独一无二的。如果我将变量更改为它们的方式

    $title and $subtitle 

it works, why is that?

它有效,为什么?

this is my Admin videoController

这是我的Admin videoController

    <?php

        namespace App\Http\Controllers\Admin;

        use App\Jobs\VideoFormFields;
        use App\Http\Requests;
        use App\Http\Requests\VideoCreateRequest;
        use App\Http\Requests\VideoUpdateRequest;
        use App\Http\Controllers\Controller;
        use App\Video;

        class VideoController extends Controller
        {
            /**
             * Display a listing of the posts.
             */
            public function index()
            {
                return view('admin.video.index')
                    ->withVideos(Video::all());
            }

            /**
             * Show the new video form
             */
            public function create()
            {
                $data = $this->dispatch(new VideoFormFields());

                return view('admin.video.create', $data);
            }

            /**
             * Store a newly created Video
             *
             * @param VideoCreateRequest $request
             */
            public function store(VideoCreateRequest $request)
            {
                $video = Video::create($request->videoFillData());
                $video->syncTags($request->get('tags', []));

                return redirect()
                    ->route('admin.video.index')
                    ->withSuccess('New Video Successfully Created.');
            }

            /**
             * Show the video edit form
             *
             * @param  int  $id
             * @return Response
             */
            public function edit($id)
            {
                $data = $this->dispatch(new VideoFormFields($id));

                return view('admin.video.edit', $data);
            }

            /**
             * Update the Video
             *
             * @param VideoUpdateRequest $request
             * @param int  $id
             */
            public function update(VideoUpdateRequest $request, $id)
            {
                $video = Video::findOrFail($id);
                $video->fill($request->videoFillData());
                $video->save();
                $video->syncTags($request->get('tags', []));

                if ($request->action === 'continue') {
                    return redirect()
                        ->back()
                        ->withSuccess('Video saved.');
                }

                return redirect()
                    ->route('admin.video.index')
                    ->withSuccess('Video saved.');
            }

            /**
             * Remove the specified resource from storage.
             *
             * @param  int  $id
             * @return Response
             */
            public function destroy($id)
            {
                $video = Video::findOrFail($id);
                $video->tags()->detach();
                $video->delete();

                return redirect()
                    ->route('admin.video.index')
                    ->withSuccess('Video deleted.');
            }
        }

this is my videoFormFields

这是我的videoFormFields

    <?php

            namespace App\Jobs;

            use App\Video;
            use App\Tag;
            use Carbon\Carbon;
            use Illuminate\Contracts\Bus\SelfHandling;

            class VideoFormFields extends Job implements SelfHandling
            {
                /**
                 * The id (if any) of the Post row
                 *
                 * @var integer
                 */
                protected $id;

                /**
                 * List of fields and default value for each field
                 *
                 * @var array
                 */
                protected $fieldList = [
                    'v_title' => '',
                    'v_subtitle' => '',
                    'page_image' => '',
                    'content' => '',
                    'meta_description' => '',
                    'is_draft' => "0",
                    'publish_date' => '',
                    'publish_time' => '',
                    'layout' => 'video.layouts.v_post',
                    'tags' => [],
                ];

                /**
                 * Create a new command instance.
                 *
                 * @param integer $id
                 */
                public function __construct($id = null)
                {
                    $this->id = $id;
                }

                /**
                 * Execute the command.
                 *
                 * @return array of fieldnames => values
                 */
                public function handle()
                {
                    $fields = $this->fieldList;

                    if ($this->id) {
                        $fields = $this->fieldsFromModel($this->id, $fields);
                    } else {
                        $when = Carbon::now()->addHour();
                        $fields['publish_date'] = $when->format('M-j-Y');
                        $fields['publish_time'] = $when->format('g:i A');
                    }

                    foreach ($fields as $fieldName => $fieldValue) {
                        $fields[$fieldName] = old($fieldName, $fieldValue);
                    }

                    return array_merge(
                        $fields,
                        ['allTags' => Tag::lists('tag')->all()]
                    );
                }

                /**
                 * Return the field values from the model
                 *
                 * @param integer $id
                 * @param array $fields
                 * @return array
                 */
                protected function fieldsFromModel($id, array $fields)
                {
                    $video = Video::findOrFail($id);

                    $fieldNames = array_keys(array_except($fields, ['tags']));

                    $fields = ['id' => $id];
                    foreach ($fieldNames as $field) {
                        $fields[$field] = $video->{$field};
                    }

                    $fields['tags'] = $video->tags()->lists('tag')->all();

                    return $fields;
                }
            }

this is my videoCreateRequest

这是我的videoCreateRequest

    <?php

            namespace App\Http\Requests;

            use Carbon\Carbon;

            class VideoCreateRequest extends Request
            {
                /**
                 * Determine if the user is authorized to make this request.
                 */
                public function authorize()
                {
                    return true;
                }

                /**
                 * Get the validation rules that apply to the request.
                 *
                 * @return array
                 */
                public function rules()
                {
                    return [
                        'v_title' => 'required',
                        'v_subtitle' => 'required',
                        'content_raw' => 'required', //                                         HERE CHANGED
                        'publish_date' => 'required',
                        'publish_time' => 'required',
                        'layout' => 'required',
                    ];
                }

                /**
                 * Return the fields and values to create a new VIDEO post from
                 */
                public function videoFillData()
                {
                    $published_at = new Carbon(
                        $this->publish_date.' '.$this->publish_time
                    );
                    return [
                        'v_title' => $this->v_title,
                        'v_subtitle' => $this->v_subtitle,
                        'page_image' => $this->page_image,
                        'content_raw' => $this->content_raw, //                                  HERE CHANGED
                        'meta_description' => $this->meta_description,
                        'is_draft' => (bool)$this->is_draft,
                        'published_at' => $published_at,
                        'layout' => $this->layout,
                    ];
                }
            }

this is my videoUpdateRequest

这是我的videoUpdateRequest

            class VideoUpdateRequest extends VideoCreateRequest
            {
                //
            }

this is my video model

这是我的视频模型

    <?php

            namespace App;

            use App\Services\Markdowner;
            use Illuminate\Database\Eloquent\Model;
            use Carbon\Carbon;
            use Sofa\Eloquence\Eloquence;
            class Video extends Model
            {
                use Eloquence;

                protected $dates = ['published_at'];

                protected $fillable = [
                    'v_title', 'v_subtitle', 'content_raw', 'page_image', 'meta_description',
                    'layout', 'is_draft', 'published_at',
                ];

                /**
                 * The many-to-many relationship between posts and tags.
                 *
                 * @return BelongsToMany
                 */
                public function tags()
                {
                    return $this->morphToMany('App\Tag', 'taggable');
                }

                /**
                 * Set the title attribute and automatically the slug
                 *
                 * @param string $value
                 */
                public function setTitleAttribute($value)
                {
                    $this->attributes['title'] = $value;

                    if (! $this->exists) {
                        $this->setUniqueSlug($value, '');
                    }
                }

                /**
                 * Recursive routine to set a unique slug
                 *
                 * @param string $title
                 * @param mixed $extra
                 */
                protected function setUniqueSlug($title, $extra)
                {
                    $slug = str_slug($title.'-'.$extra);

                    if (static::whereSlug($slug)->exists()) {
                        $this->setUniqueSlug($title, $extra + 1);
                        return;
                    }

                    $this->attributes['slug'] = $slug;
                }

                /**
                 * Set the HTML content automatically when the raw content is set
                 *
                 * @param string $value
                 */
                public function setContentRawAttribute($value)
                {
                    $markdown = new Markdowner();

                    $this->attributes['content_raw'] = $value;
                    $this->attributes['content_html'] = $markdown->toHTML($value);
                }

                /**
                 * Sync tag relation adding new tags as needed
                 *
                 * @param array $tags
                 */
                public function syncTags(array $tags)
                {
                    Tag::addNeededTags($tags);

                    if (count($tags)) {
                        $this->tags()->sync(
                            Tag::whereIn('tag', $tags)->lists('id')->all()
                        );
                        return;
                    }

                    $this->tags()->detach();
                }

                /**
                 * Return the date portion of published_at
                 */
                public function getPublishDateAttribute($value)
                {
                    return $this->published_at->format('M-j-Y');
                }

                /**
                 * Return the time portion of published_at
                 */
                public function getPublishTimeAttribute($value)
                {
                    return $this->published_at->format('g:i A');
                }

                /**
                 * Alias for content_raw
                 */
                public function getContentAttribute($value)
                {
                    return $this->content_raw;
                }

                /**
                 * Return URL to post
                 *
                 * @param Tag $tag
                 * @return string
                 */
                public function url(Tag $tag = null)
                {
                    $url = url('video/'.$this->slug);  // this fixed my problem it was 'blog/'
                    if ($tag) {
                        $url .= '?tag='.urlencode($tag->tag);
                    }

                    return $url;
                }

                /**
                 * Return array of tag links
                 *
                 * @param string $base
                 * @return array
                 */
                public function tagLinks($base = '/video?tag=%TAG%') // this fixed my problem it was 'blog?tag=%TAG%/'
                {
                    $tags = $this->tags()->lists('tag');
                    $return = [];
                    foreach ($tags as $tag) {
                        $url = str_replace('%TAG%', urlencode($tag), $base);
                        $return[] = '<a href="'.$url.'">'.e($tag).'</a>';
                    }
                    return $return;
                }

                /**
                 * Return next post after this one or null
                 *
                 * @param Tag $tag
                 * @return Post
                 */
                public function newerPost(Tag $tag = null)  //                 //here newVideo v_index & v_post
                {
                    $query =
                        static::where('published_at', '>', $this->published_at)
                            ->where('published_at', '<=', Carbon::now())
                            ->where('is_draft', 0)
                            ->orderBy('published_at', 'asc');
                    if ($tag) {
                        $query = $query->whereHas('tags', function ($q) use ($tag) {
                            $q->where('tag', '=', $tag->tag);
                        });
                    }

                    return $query->first();
                }

                /**
                 * Return older post before this one or null
                 *
                 * @param Tag $tag
                 * @return Post
                 */
                public function olderPost(Tag $tag = null) //                 //here olderVideo v_index & v_post
                {
                    $query =
                        static::where('published_at', '<', $this->published_at)
                            ->where('is_draft', 0)
                            ->orderBy('published_at', 'desc');
                    if ($tag) {
                        $query = $query->whereHas('tags', function ($q) use ($tag) {
                            $q->where('tag', '=', $tag->tag);
                        });
                    }

                    return $query->first();
                }
            }

this is my video table

这是我的视频表

    <?php

        use Illuminate\Database\Schema\Blueprint;
        use Illuminate\Database\Migrations\Migration;

        class CreateVideosTable extends Migration
        {
            /**
             * Run the migrations.
             *
             * @return void
             */
            public function up()
            {
                Schema::create('videos', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('slug')->unique();
                    $table->string('v_title');
                    $table->string('v_subtitle');
                    $table->text('content_raw');
                    $table->text('content_html');
                    $table->string('page_image');
                    $table->string('meta_description');
                    $table->boolean('is_draft');
                    $table->string('layout')
                        ->default('blog.layouts.post');
                    $table->timestamps();
                    $table->timestamp('published_at')->index();
                });
            }

            /**
             * Reverse the migrations.
             */
            public function down()
            {
                Schema::drop('videos');
            }
        }

2 个解决方案

#1


0  

it seems you did not refactor title to v_title in the whole class. For example, you should change

看来你没有在全班重构v_title的标题。例如,你应该改变

public function setTitleAttribute($value)
                {
                    $this->attributes['title'] = $value;

                    if (! $this->exists) {
                        $this->setUniqueSlug($value, '');
                    }
                }

to

public function setVTitleAttribute($value)
                {
                    $this->attributes['v_title'] = $value;

                    if (! $this->exists) {
                        $this->setUniqueSlug($value, '');
                    }
                }

#2


1  

After you changed the column names, your dynamic setters setTitleAttribute and setSubtitleAttribute are not called and the slug does not get updated. You need to change the names of setter methods as well.

更改列名后,不会调用动态setter setTitleAttribute和setSubtitleAttribute,并且不会更新slug。您还需要更改setter方法的名称。

public function setVTitleAttribute($value) {
  ...
}

public function setVSubtitleAttribute($value) {
  ...
}

#1


0  

it seems you did not refactor title to v_title in the whole class. For example, you should change

看来你没有在全班重构v_title的标题。例如,你应该改变

public function setTitleAttribute($value)
                {
                    $this->attributes['title'] = $value;

                    if (! $this->exists) {
                        $this->setUniqueSlug($value, '');
                    }
                }

to

public function setVTitleAttribute($value)
                {
                    $this->attributes['v_title'] = $value;

                    if (! $this->exists) {
                        $this->setUniqueSlug($value, '');
                    }
                }

#2


1  

After you changed the column names, your dynamic setters setTitleAttribute and setSubtitleAttribute are not called and the slug does not get updated. You need to change the names of setter methods as well.

更改列名后,不会调用动态setter setTitleAttribute和setSubtitleAttribute,并且不会更新slug。您还需要更改setter方法的名称。

public function setVTitleAttribute($value) {
  ...
}

public function setVSubtitleAttribute($value) {
  ...
}