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