Thursday, March 9, 2017

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'stickers' cannot be null Laravel

Hi everyone im having this issue with a row on my posts table.

This is the form:

                    

                    

                    

                    

                    


                </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>

                    
                    

                    <br> <hr>
                    
                    

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

This is the Posts Migration file:

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');
}

And this is part of my Controller "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)
{
    //
}

The sticker row belongs to posts table on my database, so when i post the data get that error message. It was working perfectly yesterday but y moved some folders and accidentally i delete part of the code so i wrote it again but with that bug, and another one that this community helped to fix.

Thanks a lot



via Juan Rincón

Advertisement