armadillo's avatar

Multiple images for single task

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') ; 
  
        
    }

0 likes
3 replies
carlosmtns's avatar
Level 1

You can try something like save the task record first, and use that saved ID on those files record (assuming that they're differente tables). Example:

$album = new Album;

$album->name = $request->album_name;
$album->save();

foreach ($files as $file) {
      $image = new AlbumImage;

      $image->album_id = $album->id; //Got previous saved album ID
      $image->user_id = Auth::user()->id;
      $image->img_name = '/images/album/'. $album->id . '/' . $extension;
      $image->img_thumb = '/images/album/'. $album->id . '/thumb_' . $extension;
      $image->img_type = $file->getClientMimeType();
      $image->img_size = $file->getClientSize();
      $image->save();

}
armadillo's avatar

Thank you yoOwnage, here is how i was able to solve and images are now being uploaded and added to the current created task ID


            // First save Task Info
            $task = Task::create([
                'project_id' => $request->project_id,
                'task_title' => $request->task_title,
                'task'       => $request->task,
                'priority'   => $request->priority
            ]);

            // Then save files using the newly created ID above
            foreach ($request->photos as $file) {
                $filename = $file->store('taskfiles'); // /storage/app/taskfiles
                TaskFiles::create([
                    'task_id' => $task->id,
                    'filename' => $filename
                ]);
            }

1 like

Please or to participate in this conversation.