【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'stickers' cannot be null LaravelSQLSTATE [23000]:违反完整性约束:1048 列“贴纸”不能为空 Laravel
【发布时间】:2017-07-31 05:28:25
【问题描述】:

大家好,我的帖子表中有一行存在这个问题。

这是表格:

                    {{ 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)
{
    //
}

贴纸行属于我数据库上的帖子表,所以当我发布数据时会收到该错误消息。它昨天工作得很好,但是你移动了一些文件夹并且不小心删除了部分代码,所以我再次编写了它,但是有那个错误,以及这个社区帮助修复的另一个错误。

非常感谢

【问题讨论】:

  • 您的表单中似乎不需要贴纸,请编辑迁移以创建此属性-&gt;nullable()
  • 您能展示一下您的模型的结构吗?要查看您的模型中是否有您通过属性 fillable 发送数据
  • 实际上我的 Posts 模型是空的,我还没有处理它。正如我所说,它昨天工作正常,但试图修复它我卡在那里
  • EddyTheDove 给出了答案,所以我要求您展示您的模型以确认您使用的是可填充属性。
  • @EdwardPalen 实际上这不是答案。问题是 class="form-control" 不要问我为什么,但据我记得昨天它正在与那个类一起工作,我决定删除那个类,瞧……现在运行顺利。

标签: php mysql laravel blade


【解决方案1】:

在您的模型中,将“贴纸”添加到可填充数组中

protected $fillable = ['stickers']; //and any other mass asignable field. 

【讨论】:

  • 我收到此错误:VerifyCsrfToken.php 第 68 行中的 TokenMismatchException:
  • 要么您的表单中没有 csrf 令牌,要么您的令牌已过期。你用Form::open() 吗?
  • 好的,检查文件的布局。看来您将表单拆分为多个部分。我建议在&lt;div class='row'&gt;&lt;/div&gt; 之前打开表格,以便将其他所有内容都包含在内。无论如何,您的 html 文件看不清楚所以我不知道您是否真的破坏了您的表格。尝试查看您的来源并检查您的表单是否隐藏了带有name="_token" 的输入。
  • 谢谢,我做到了 :D 问题是这样的:'maxlength' => '20' 我把它取下来了,问题解决了,非常感谢。
【解决方案2】:

如果在贴纸列中可以为空 添加 nullable() 链式方法

$table->string('stickers')->nullable();

【讨论】:

  • 谢谢,我做到了 :D 问题是这样的:'maxlength' => '20' 我把它取下来了,问题解决了,非常感谢。实际上,当我在没有图像文件字段的情况下发布新帖子时,我得到了同样的错误,所以我的贴纸表是一个 255 个字符的字符串,我用 parsley.css 和 parsley.js 将它限制为 25,所以来自贴纸的数据永远不会从我的表格。谢谢各位
猜你喜欢
  • 2021-02-01
  • 2020-08-07
  • 2019-11-20
  • 2021-08-16
  • 2019-10-06
  • 2020-10-25
  • 2017-02-28
  • 2017-04-15
  • 2017-12-26
相关资源
最近更新 更多