2017-03-10 124 views
0

嗨大家即时通讯我的帖子表上有一行这个问题。SQLSTATE [23000]:完整性约束违规:1048'贴'栏不能为空Laravel

这是形式:

    {{ Form::label('title', ' Título', array('class' => 'fa fa-pencil')) }} 

        {{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255', 'placeholder' => 'Ingresar Título')) }} 

        {{ Form::label('body', ' Contenido', array('class' => 'fa fa-pencil-square-o')) }} 

        {{ Form::textarea('body', null, ['class' => 'form-control', 'required' => '', 'placeholder' => 'Redactar Contenido']) }} 

        {{ Form::submit('Publicar', array('class' => 'btn btn-info')) }} 


       </div> 
      </div> <!-- col-md-8 end --> 


      <div class="col-md-4"> 
      <div class="input-group"> 
       <h3 class="text-center">Categoría e imágen</h3> 
       <hr> 


        <br> <hr> 

        {{ Form::label('stickers', ' Etiqueta', array('class' => 'fa fa-tags')) }} 
        {{ Form::text('stickers', '', array('class' => 'form-control', 'maxlength' => '20', 'placegolder' => 'Ingresar Etiqueta')) }} 

        <br> <hr> 
        {{ Form::label('postimg', ' Seleccionar Imagen', array('class' => 'fa fa-picture-o')) }} 
        {{ Form::file('postimg') }} 

       {!! Form::close() !!} 

这是帖子移民文件:

public function up() 
{ 
    Schema::create('posts', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('title'); 
     $table->string('stickers'); 
     $table->text('body'); 
     $table->string('postimg'); 
     $table->timestamps(); 
    }); 
} 

/** 
* Reverse the migrations. 
* 
* @return void 
*/ 
public function down() 
{ 
    Schema::dropIfExists('posts'); 
} 

这是我的控制器 “PostsController” 的一部分:

public function create() 
{ 
    return view('posts.create'); 
} 

/** 
* Store a newly created resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store(Request $request) 
{ 
    // validate the data 
    $this->validate($request, array(
     'title' => 'required|max:255', 
     'body' => 'required', 
     )); 

    // store in database 
    $post = new Post; 

    $post->title = $request->title; 
    $post->body = $request->body; 
    $post->stickers = $request->stickers; 
    $post->postimg = $request->postimg; 

    $post->save(); 

    Session::flash('success', 'La publicación se ha creado correctamente'); 

    // redirect to another page 
    return redirect()->route('posts.show', $post->id); 
} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function show($id) 
{ 
    $post = Post::find($id); 
    return view('posts.show')->withPost($post); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function edit($id) 
{ 
    // 
} 

贴纸行属于我的数据库上的帖子表,所以当我发布数据时得到该错误信息。它昨天工作完美,但是y移动了一些文件夹,并且意外地删除了一部分代码,所以我再次写了它,但是有了这个错误,还有另外一个这个社区帮助解决的问题。

非常感谢

+0

一点也没有看起来你需要在你的表单中添加贴纸,编辑迁移来创建这个属性 - > nullable()' – upful

+0

你能展示你的模型是如何构建的吗?要查看您的模型中是否有数据通过可填充的属性 –

+0

发送数据其实我的帖子模型是空的,我还没有处理它。它昨天工作正常,因为我说,但试图解决它我卡在那里 –

回答

1

在模型中,添加 '贴' 到可填充阵列

protected $fillable = ['stickers']; //and any other mass asignable field. 
+0

我得到这个错误: TokenMismatchException in VerifyCsrfToken.php第68行: –

+0

您的表单中没有csrf标记,或者您的标记已过期。你使用'Form :: open()'? – EddyTheDove

+0

是的,我正在使用窗体打开 –

0

如果为空的在贴柱 添加可为空的()链方法

$table->string('stickers')->nullable(); 
+0

谢谢,我做到了:D问题是这样的: 'maxlength'=>'''' 我把它拿下来,问题解决了很多。 其实当我做了一个没有图像文件字段的新帖子时,我得到了同样的错误,所以我的贴纸表是一个255字符串的字符串,我将它限制在25个parsley.css和parsley.js,所以贴纸的数据永远不会从我的形式出来。感谢你们 –

相关问题