Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Flex's avatar
Level 4

SQLSTATE[23000]: Integrity constraint violation:

Hi, I need pass Project Model id as project_id to My Task Model table. this is My TaskController

public function store(Request $request, Project $project)
{
    $task = new Task;
    $task->task_name   = $request->input('name');
    $task->body = $request->input('body');
    $task->assign = $request->input('status');
    $task->priority = $request->input('status');
     $task->duedate  = date("Y-m-d", strtotime($request->input("date")));
    

    // This will set the project_id on task and save it
    $project->tasks()->save($task);
}

this is My form action route regarding to store task data in projects folder blade file is show.blade.php

<form method="post" action="{{ route('tasks.store') }}">

this is Task Mode

<?php

namespace App;

use App\User;
use App\Auth;
use App\Project;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
     protected $fillable = ['task_name', 'body', 'assign','priority','duedate','project_id'];



      public function scopeProject($query, $id)
    {
        return $query->where('project_id', $id);
    }


     public function projects()
     {
         return $this->belongsTo('App\Project');
     }
    
    
    //
}

this is Project Model

<?php

namespace App;
use Auth;
use App\Task;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    protected $fillable = ['project_name','project_notes','project_status','color','group'];
    //

    public function tasks(){
         return $this->hasMany('App\Task');
}


}


but I got this error massage here

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null

how can fix this problem?

0 likes
10 replies
Snapey's avatar

yes idea

are you passing project id in the form url? If not, how do you expect to get an instance of Project passed into your store method?

change the form statement

<form method="post" action="{{ route('tasks.store', $project) }}">

If this does not work, reply with your task routes

Flex's avatar
Level 4

@Snapey above code segment occurred same error

My task roures is as follow

Route::resource('tasks','TasksController');
Snapey's avatar

ok so how will project be passed into the task controller?

I know there has been some work done recently regarding nested resources, but I admit to not being fully up to speed on it. Without considering that, you need something like

Route::resource('project/{project}/tasks','TasksController');
Flex's avatar
Level 4

@Snapey did it with your routes but same error has occurred here

Snapey's avatar

If you are not willing to diagnose the problem yourself, this is going to take a long time...

Step 1. What URL are you creating in the form? View the html source and look at the <form tag. check the action contains your project id

Step 2. In your taskController store method `dd($project) and see if it is being initialised

Alternatively pass the project id in a hidden form field

by the way. At some point check that the project belongs to the user to stop person A adding tasks to person B's project

1 like
Flex's avatar
Level 4

@Snapey ok got lot of things from you now I need know some thing this is My new routes

Route::post('projects/{projects}/tasks/', [
    'uses' => '\App\Http\Controllers\TasksController@postNewTask',
    'as' => 'projects.tasks.create'
]);


and this is my new form URL

<form  method="post" action="{{ route('projects.tasks.create', $project->id) }}">

what are the wrongs with this both routes and form actions

Flex's avatar
Level 4

@Snapey I found some error in My Migration files that means I did not put primary and forign key as well between My project and Task files is this problem with this mattes

Task migration file

public function up()
    {
         Schema::create('tasks', function(Blueprint $table)
        {
            $table->increments('id')->unsigned();
            $table->longText('task_name');
            $table->text('body');
            $table->string('assign');
            $table->string('priority');
            $table->date('duedate');
            $table->integer('project_id')->unsigned();
            $table->timestamps();
 
          
        });
        
    }

projects migration file

public function up()
    {
        Schema::create('projects', function(Blueprint $table)
            {
                $table->increments('id')->unsigned();
                $table->string('project_name');
                $table->longText('project_notes');
                $table->string('project_status')->default('Upcoming');
                $table->integer('user_id')->unsigned();
                $table->char('color', 7);
                $table->string('group');
                $table->timestamps();

                $table->foreign('user_id')
                      ->references('id')->on('users')
                      ->onUpdate('cascade')
                      ->onDelete('cascade');
            });
        //
    }
Snapey's avatar

Step 1. What URL are you creating in the form? View the html source and look at the <form tag. check the action contains your project id

Step 2. In your taskController store method `dd($project) and see if it is being initialised

Flex's avatar
Flex
OP
Best Answer
Level 4

@Snapey finally got the result. in this way in My TaskController

  public function postNewTask(Request $request,$id, Project $project)
{

    $task = new Task;
    $task->task_name   = $request->input('name');
    $task->body = $request->input('body');
    $task->assign = $request->input('status');
    $task->priority = $request->input('status');
    $task->duedate  = date("Y-m-d", strtotime($request->input("date")));
    $task->project_id = $id;
    $task->save();

form url

<form  method="post" action="{{ route('projects.tasks.create', $project->id) }}">

and routes

Route::post('projects/{projects}/tasks', [
    'uses' => '\App\Http\Controllers\TasksController@postNewTask',
    'as' => 'projects.tasks.create'
]);

thanks very much for your instructions....

Please or to participate in this conversation.