0

I am using Bootforms to edit posts on a blog

<?php $formOptions = [
        'url' => 'user',
        'sm' => [2, 5],
        'lg' => [2, 5],
        'method'=> 'put'
    ]; ?>

    {!! BootForm::openHorizontal($formOptions)->action(route('news.update', $post)) !!}
    <input type="hidden" name="_method" value="PUT">
    {!! BootForm::text('Titre', $post->title) !!}
    {!! BootForm::text('Slug', $post->slug) !!}
    {!! BootForm::textarea('Contenu', $post->content) !!}
    {!! BootForm::submit('Editer') !!}
    {!! BootForm::close() !!}

Here is my PostController function once I update my post :

 public function update($id, Request $request)
{
    $post = Post::findorFail($id);

    $title = $request->input('title');

    $post->title = $title;
    $post->content = $request->input('Contenu');

    $request->has('save');

    $post->save();
    return redirect(route('news.index'));
}

But once I edit my post, I encouter this error like i am sending empty strings : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: update posts set title = , content = , updated_at = 2016-12-14 20:48:25 where id = 3)

If you see where is the problem, I could use some help ...

3
  • The error could not be clearer, it is telling you that you are trying to insert an empty (NULL) "title" field in the database and that the field is defined as not null.
    – Anand
    Dec 14, 2016 at 21:11
  • I know that, and I said that I knew that it's sending empty strings. The problem was I didn't know how to fix it. But thanks for trying to help ..
    – Inas
    Dec 14, 2016 at 21:22
  • oops, didn't realize, sorry about the sarcasm.
    – Anand
    Dec 14, 2016 at 21:28

2 Answers 2

1

It looks like you use invalid parameters in forms. If you want use default values, you should make it as in example from github:

https://github.com/adamwathan/bootforms

BootForm::text('Titre', 'title')->defaultValue($post->title);

Now you use $post->title as field name, so $_POST['title'] is just empty.

1
  • Oh yes ! That was silly but I couldn't figure it out. Thank you, it works know
    – Inas
    Dec 14, 2016 at 21:23
0

May be two things can be done.

1)You should either allow title column allow null values. ALTER TABLE tableName MODIFY table VARCHAR(200);

2) You can first check if title is set or not and display appropriate error message if its not set.

public function update($id, Request $request)
{
    $post = Post::findorFail($id);

    $title = $request->input('title');
    /*
     First check if tile is not empty
    */
     if (empty($title)){
        //show error message // echo 'Please fill in title or whatever';
     }else{

       $post->title = $title;
       $post->content = $request->input('Contenu');

       $request->has('save');

       $post->save();
       return redirect(route('news.index'));
     }
}

It seems intuitive that title should not be empty, So in my advice you should try the 2nd method first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.