简体   繁体   English

laravel 中的完整性约束违规

[英]Integrity constraint violation in laravel

在此处输入图像描述 在此处输入图像描述 I am passing the ID in URL to get to the Form Page I want but I want to pass in the same ID when I hit the submit button and I want to pass in the same ID to the database table name "created_by"我正在传递 URL 中的ID以访问我想要的表单页面,但是当我点击提交按钮时我想传递相同的ID ,并且我想将相同的ID传递给数据库表名“created_by”

Here's My Route:-这是我的路线:-

Route::get('/post/{id}', 'PostsController@create');

Here's My Controller:-这是我的 Controller:-

public function create(){
  return view('Posts.CreatePost');

}

public function store(Request $request){
    post::create([
              'title'=>$request->title,
              'body'=>$request->body,
              'user_id'=>Auth::user()->id,
              'filled_by'=>Auth::user()->id,
              'created_by' => $request->route('id'),
            ]);

    return redirect('/profile/' . auth()->user()->id);
    }

Here's My Migration:-这是我的迁移:-

public function up(){
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('body');
            $table->string('created_by');
            $table->string('filled_by');
            $table->timestamps();

            $table->index('user_id');
        });
    }

Here's My Model:-这是我的 Model:-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{ protected $gaurded = [];

  protected $fillable = ['title', 'body', 'user_id', 'created_by', 'filled_by'];

    public function user()
    {
      return $this->belongsTo(User::class);
    }
}

The Error that i am getting is SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_by' cannot be null (SQL: insert into posts ( title , body , user_id , filled_by , created_by , updated_at , created_at ) values (adsa, asdas, 1, 1, ?, 2020-04-16 20:54:30, 2020-04-16 20:54:30))我得到的错误是SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_by' cannot be null (SQL: insert into posts ( title , body , user_id , filled_by , created_by , updated_at , created_at ) 值 (adsa, asdas , 1, 1, ?, 2020-04-16 20:54:30, 2020-04-16 20:54:30))

I am just a beginner at programming and laravel so excuse me if I am being stupid我只是编程和laravel的初学者,如果我很愚蠢,请原谅

-ThankYou -谢谢你

You are getting this error because $request->route('id') is empty and your database doesn't allow for the created_by column to be empty.您收到此错误是因为$request->route('id')为空,并且您的数据库不允许created_by空。

To debug, check the content of your request:要调试,请检查您的请求内容:

public function store(Request $request){

    // Die and dump the request:
    dd($request->all());

    post::create([
              'title'=>$request->title,
              'body'=>$request->body,
              'user_id'=>Auth::user()->id,
              'filled_by'=>Auth::user()->id,
              'created_by' => $request->route('id'),
            ]);

    return redirect('/profile/' . auth()->user()->id);
    }

I believe you are not correctly retrieving the value id from the request.我相信您没有正确地从请求中检索值id

You probably want to retrieve it with $request->id but it's hard to say without looking at your blade view.您可能想用$request->id检索它,但如果不查看刀片视图就很难说。 If you can share your view I'll edit my answer.如果您可以分享您的观点,我将编辑我的答案。


Edit: Also, your route is incorrect.编辑:另外,您的路线不正确。 To point to your controller create() method, you shouldn't have to pass an id to your route.要指向您的 controller create()方法,您不必将id传递给您的路线。 By definition, you want to CREATE an object that doesn't yet exist and therefore doesn't yet have an ID.根据定义,您要创建一个尚不存在因此还没有 ID 的 object。

With the create() method, your route should look like this:使用 create() 方法,您的路线应如下所示:

Route::get('/post/create', 'PostsController@create');

Also, if you want to follow Laravel naming conventions (trust me, you do), your controllers should be named in singular and url models in plural.此外,如果您想遵循 Laravel 命名约定(相信我,您会这样做),您的控制器应该以单数命名,url 型号以复数命名。 Meaning it would look like this:这意味着它看起来像这样:

Route::get('/posts/create', 'PostController@create');

And the route for your store() method should look like this:您的 store() 方法的路径应如下所示:

Route::post('/posts', 'PostController@store');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM