0

I am developing a simple task management web application using laravel. The requirement states that we need to save the general information such as TaskDate, AssignedTo in a taskinfo table. List of tasks for one specific person are saved in another table called tasks. The tasks table has TaskDetailID (PK), TaskID (FK from the above table), TaskDescription, HoursRequired, etc... The form allows users to add as many rows as they can which means a person could get assigned unlimited amount of tasks.

screenshot My problem now is saving the tasks data in the table. I've successfully saved the data for the taskinfo table, and i can even save the data for the table but only when it's one column.

Here is my store function on TaskInfoController

public function store(Request $request)
    {   
        $validator = Validator::make(
            $request->all(), 
            [
            'TaskDate.*' => 'required',
            'AssignedTo.*' => 'required',
            ]
            ,
            [
            'TaskDate.*.required' => 'Task Date is required.',
            'AssignedTo.*.required' => 'Please assign the task to someone.',
            ]
            );
    if ($validator->fails())
    {
       //redirect errors to mars
    }

    $taskinfo = new TaskInfo();
    $taskinfo->TaskDate = Carbon::createFromFormat("m/d/Y", $request->input('TaskDate'));
    $taskinfo->TaskAssignedTo = $request->input('TaskAssignedTo');
    // Some more columns here
    $taskinfo->Save();

    // Now for the tasks table
    $tasksbulkinsert = array();

    foreach ($request->input('TaskDescription') as $taskdescription)
    { 
        $tasksbulkinsert[] = array('TaskID' => Uuid::uuid4(), 'TaskDescription' => $taskdescription);
    }

    Task::insert($tasksbulkinsert); 

    return redirect()->action('TaskInfoController@index')->with('flash_message', 'Successfully Saved!');}

The above code actually works perfectly but i don't know how i can insert the HoursRequired, or any additonal value with the corresponding taskdescription on the tasks table.

I tried a few approaches

  1. having an incremental count such as i++ to know which row index (so to speak) of the taskdescription the query is currently procession, and having another foreach loop with it's own counter for the hoursrequired input and getting the value where the taskdescription's counters is equal to the hoursrequired counter. But it didn't work and even if it did, i don't think having multiple foreach loops for every column is good for performance.

  2. Having different arrays with their own foreach loop to get the values from the inputs and then somehow merge the arrays.

Here is my HTML form

<input class="form-control" name="TaskDescription[]" type="text">
<input class="form-control" name="HoursRequired[]" type="text">

Main Question.

  1. How can I save the TaskDescription and the HoursRequired into the database with one query.

Not so important question

  1. The array validation at the top works but is there a way to have an error message that states the row of the error. For example, Date is required for row number n.

1 Answer 1

1

You can simply:

foreach ($request->input('TaskDescription') as $i=>$taskdescription)
{ 
    $tasksbulkinsert[] = array(
        'TaskID' => Uuid::uuid4(), 
        'TaskDescription' => $taskdescription,
        'HoursRequired' => $request->input('HoursRequired')[$i]
    );
}

For your second question:

$messages = [];
foreach($this->request->get('TaskDescription') as $key => $val)
{
    $messages['TaskDescription.'.$key.'.required'] = 'TD is required for row $key';
    $messages['some_field.'.$key.'.some_rule'] = 'some_field custom message on row $key';
}


$validator = Validator::make(
        $request->all(), 
        [
        'TaskDate.*' => 'required',
        'AssignedTo.*' => 'required',
        ]
        ,
        $messages;
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely Perfect! Works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.