0

I am beginner at laravel and i want to add multiple record into database table and I have a returned request below but I have no idea how to submit data into table how can I make logic in controller Does anyone have an idea how to to do that please help me thanks. Database table

    projectissue table  

     Id  | task  | date| project_id

Controller

         public function issuesUpdate(Request $request,Project $project)
         {


              return redirect()->route('project');
         }

return $request

               {
  "_token": "S6typbw0hywPqaUdxCTqWeJNieyl3VieQhCHqDZ7",
    project_id=42;
  "date": [
    "2020-07-06",
    "2020-07-22",
    "2020-07-11"
  ],
  "issue": [
    "dsfsdfsdfsdf",
    "dsfsdfsdfsdf",
    "dsfsdfsdfsdf"
  ]
}

Route

  Route::post('/projects/{project}/issues/update', "ProjectController@issuesUpdate")- 
  >name('project.issues.update');

Html view

     <form action="{{ route('project.issues.update',[$project->id])}}" method="POST">
      @csrf    
      <div class="portlet">
        <div class="portlet-heading bg-light-theme">
          <h3 class="portlet-title">
            <span class="ti-user mr-2">
            </span>Add Issues
          </h3>
          <div class="portlet-widgets">
            <span class="divider">
            </span>
            <button type="submit"  class="btn btn-white waves-effect btn-rounded">
              <span class="btn-label">
                <i class="fa fa-save">
                </i>
              </span> Save
            </button>
          </div>
          <div class="clearfix">
          </div>
        </div>
        <div id="bg-inverse" class="panel-collapse collapse show" style="">
          <div class="portlet-body">
            <div class="card-box">
              <div class="row">
                <div class="col-md-12 mt20">
                  <div class="addMore">
                    <div class="addmore_cont">
                      <div class="addMore_btn">
                       
                        <div class="mt12 pull-right">
                          <button type="button" title="Add More" class="btn btn-success waves-effect waves-light btn-sm add_more" data-key="">
                            <i class="fa fa-plus">
                            </i>
                          </button>
                        </div>
                        <div class="row addmore_issues">
                          <div class="col-md-12">
                            <div class="row">
                              <div class="col-md-5">
                                <div class="form-group">
                                  <input required type="date" value="" name="date[]" class="form-control"
                                         aria-describedby="emailHelp" >
                                </div>
                              </div>
                              <div class="col-md-5">
                                <div class="form-group">
                                  <input required type="text" value="" name="issue[]" class="form-control"
                                     aria-describedby="emailHelp" placeholder="task...">
                                </div>
                              </div>
                              <div class="col-md-1 mt12">
                                <button type="button" class="btn btn-danger waves-effect waves-light btn-sm delete">
                                  <i
                                     class="fa fa-times">
                                  </i>
                                </button>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </form>

2 Answers 2

1

I would suggest to read the documentation first: https://laravel.com/docs/7.x/eloquent. The documentation is written very well. Don't forget to validate the data using form requests (recommended way if you ask me): https://laravel.com/docs/7.x/validation#form-request-validation

I'll give you a head start:


class YourFormRequest
{
    public function rules()
    {
        return [
            'date'    => ['required', 'array'],
            'date.*'  => ['required', 'date'],
            'issue'   => ['required', 'date'],
            'issue.*' => ['required', 'string'],
        ];
    }
}

class SomeController
{
    public function issuesUpdate(YourFormRequest $request)
    {
        foreach ($request->post('date') as $i => $value) {
            ProjectIssue::insert([
                'attribute' => $request->post(),
            ]);
        }

        return redirect()->route('project');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
$dates = request('date');
$issues = request('issue');

for($i = 0; $i < count($issues); $i++)
{

    $issue = new ProjectIssue();
    $issue->task = $issues[$i];
    $issue->date = $dates[$i];
    $issue->project_id = //however you're getting your project id to the controller;
    if($issue->save()){
       continue;
    } else {
      //process error the way you want
    }


}

so what my code is doing is storing the request arrays in a variable and looping through the amount of issues that your form submitted. for each issue that is there, it creates a new entry in the table and then goes on to the next one. I'm making a few assumptions in the way I named things, but it should be enough to get you started.

Comments

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.