我创建了一个网站,它有一个博客部分和一个视频部分。我有一个博客的配置文件,我试图与视频部分共享,但它不能正常工作,所以我为视频创建了一个单独的配置。并修改了视频
的列名 $title and $subtitle
$v_title and $v_subtitle.
但是当我这样做时,我得到以下错误
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'videos_slug_unique'
由于某种原因没有产生一个段塞,这就导致了这个问题。这种蛞蝓在我的桌子上是独一无二的。如果我把变量改成原来的样子
$title and $subtitle
它工作,为什么呢?
这是我的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.');
}
}
这是我的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;
}
}
这是我的videocreaterrequest
<?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,
];
}
}
这是我的videoUpdateRequest class VideoUpdateRequest extends VideoCreateRequest
{
//
}
这是我的视频模型
<?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();
}
}
这是我的视频表
<?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');
}
}
在更改列名之后,动态设置器setTitleAttribute和setSubtitleAttribute不会被调用,并且段代码不会得到更新。您还需要更改setter方法的名称。
public function setVTitleAttribute($value) {
...
}
public function setVSubtitleAttribute($value) {
...
}
似乎你没有重构title
到v_title
在整个类。例如,您应该更改
public function setTitleAttribute($value)
{
$this->attributes['title'] = $value;
if (! $this->exists) {
$this->setUniqueSlug($value, '');
}
}
public function setVTitleAttribute($value)
{
$this->attributes['v_title'] = $value;
if (! $this->exists) {
$this->setUniqueSlug($value, '');
}
}