The problem i have is that i have a tasks table and a task_file table, when a new task is created i would like to add an input field for uploading 1 or more files (images) ,
Here is the task_files migration file
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskFilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task_files', function (Blueprint $table) {
$table->increments('id');
$table->integer('task_id')->unsigned();
$table->string('filename');
$table->timestamps();
});
/*
Delete files associated with Task ID
*/
Schema::table('task_files', function (Blueprint $table) {
$table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade') ;
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task_files');
}
}
and the tasks migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->unsigned();
$table->string('task_title');
$table->text('task') ;
$table->integer('priority')->default(0) ;
$table->boolean('completed')->default(0) ;
$table->timestamps();
});
/*
Delete tasks associated with this project ID
*/
Schema::table('tasks', function (Blueprint $table) {
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade') ;
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
The problem is with the TasksController.php , i can successfully create a task, the problem is if i add a input field for files how can i add those files and add it to the task ID if the task ID is not created yet?
public function store(Request $request)
{
// dd( $request->all() ) ;
/*
Here is where i am not really sure how to proceed
How can i upload the images and assign to this new task that is being created
when i dont yet have its task ID to associate the uploaded images to it?
*/
$this->validate( $request, [
'task_title' => 'required',
'task' => 'required',
'project_id' => 'required'
]) ;
$post = Task::create([
'project_id' => $request->project_id,
'task_title' => $request->task_title,
'task' => $request->task,
'priority' => $request->priority
]);
Session::flash('success', 'Task Created') ;
return redirect()->route('task.show') ;
}