2016-12-14 54 views
0

我使用Bootforms到博客上的SQL错误:通过表单

<?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() !!} 

这里编辑帖子发送空字符串是我PostController的功能,一旦我更新我的帖子:

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

但是,一旦我编辑我的帖子,我encouter这个错误就像我发送空字符串:SQLSTATE [23000]:完整性约束违规:1048'标题'列不能为空(SQL:更新posts设置title =,content =,updated_at = 2016-12- 14 20:48:25哪里id = 3)

如果你看到哪里出了问题,我可以利用一些帮助......

+0

错误不能更清楚,它告诉你,你试图在数据库中插入一个空的(NULL)“标题”字段,并且该字段被定义为非空。 – Anand

+0

我知道,我说我知道它发送空字符串。问题是我不知道如何解决它。但谢谢你试图帮助.. – Inas

+0

哎呀,没有意识到,对于讽刺感到抱歉。 – Anand

回答

1

它看起来像您使用的形式参数无效。如果你想使用默认值,你应该让作为例如从GitHub:

https://github.com/adamwathan/bootforms

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

现在你使用后$>标题字段名,因此$ _ POST [“标题”]是只是空着。

+0

哦,是的!这很愚蠢,但我无法弄清楚。谢谢,它的作品知道 – Inas

0

可能有两件事可以做。

1)您应该允许title列允许空值。 ALTER TABLE tableName MODIFY table VARCHAR(200);

2)您可以先检查是否设置了标题,如果未设置,则显示相应的错误消息。

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

看起来很直观,标题不应该是空的,所以在我的建议你应该先尝试第二种方法。