samedi 19 août 2017

foreign key not define in migration file in Laravel 5.2

I have two migration files in my laravel app as projects and task. project id column is foreign key to task table project_id. I need save project id as project_id in task table. but I did not configure foreign key ralationship in my task migration file 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();
 });

    }

this is my TaskController

public function postNewTask(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")));
     $task->project_id = $project->id;

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

}

Task 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 project()
     {
         return $this->belongsTo('App\Project');
     }

Project Model

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

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

my form action

<form  method="post" action="">

and routes

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

but when I going to save form data has occurred following error message

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

how can solved this problem. is this my migration file foreign key not define problem?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2vd0NRH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire