2017-03-10 121 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,所以貼紙的數據永遠不會從我的形式出來。感謝你們 –

相關問題