php - foreign key not define in migration file in Laravel 5.2 -
i have 2 migration files in laravel app projects , task. project id column foreign key task table project_id. need save project id project_id in task table. did not configure foreign key ralationship in 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 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; // set project_id on task , save $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="{{ route('projects.tasks.create', $project->id) }}">
and routes
route::post('projects/{projects}/tasks', [ 'uses' => '\app\http\controllers\taskscontroller@postnewtask', 'as' => 'projects.tasks.create' ]);
but when going save form data has occurred following error message
sqlstate[23000]: integrity constraint violation: 1048 column 'project_id' cannot null
how can solved problem. migration file foreign key not define problem?
finally got results 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(); }
Comments
Post a Comment