Laravel store multiple value in different row











up vote
0
down vote

favorite












would you like to help me? I want to input multiple value and store in database with different row. For example :



In input form,



|project_id    milestone_id|
| 1 1,2,3 |
| 2 2,4 |


My expected result in database



|project_id    milestone_id|
| 1 1 |
| 1 2 |
| 1 3 |
| 2 2 |
| 2 4 |


This is my view (tasktableprojectmiles.blade.php)



<form action="{{route('project-miles.store')}}" method="post">
<label for="project" class="control-label mb-1">Project</label>
<div class="input-group col-lg-12">
<select name="project_id" id="project_id" class="form-control">
<option value="">Please Select</option>
@foreach ($projects as $item)
<option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
@endforeach
</select>
</div>
<label for="milestone" class="control-label mb-1">Milestone</label>
<div class="input-group col-lg-12">
<select multiple="multiple" name="milestone_id" id="milestone_id" class="form-control">
<option value="">Please Select</option>
@foreach ($milestones as $item)
<option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-lg btn-info btn-block"></button>
</form>


This is my controller (ProjectMilestoneController.php) and I take value from 2 model (Milestone and Project)



class ProjectMilestoneController extends Controller
{
public function index()
{
$projects = Project::get();
$milestones = Milestone::get();
return view('takstable.projectmiles',['projects'=>$projects, 'milestones'=>$milestones]);
}

public function store(Request $request)
{
ProjectMilestone::create($request->all());
return redirect()->route('project-miles.index');
}
}


This is my model (ProjectMilestone.php)



class ProjectMilestone extends Model
{
protected $fillable = ['project_id','milestone_id'];
public function project()
{
return $this->belongsTo(Project::class,'project_id','id');
}

public function milestone()
{
return $this->belongsTo(Milestone::class,'milestone_id','id');
}
}


Thank you in advanced










share|improve this question


























    up vote
    0
    down vote

    favorite












    would you like to help me? I want to input multiple value and store in database with different row. For example :



    In input form,



    |project_id    milestone_id|
    | 1 1,2,3 |
    | 2 2,4 |


    My expected result in database



    |project_id    milestone_id|
    | 1 1 |
    | 1 2 |
    | 1 3 |
    | 2 2 |
    | 2 4 |


    This is my view (tasktableprojectmiles.blade.php)



    <form action="{{route('project-miles.store')}}" method="post">
    <label for="project" class="control-label mb-1">Project</label>
    <div class="input-group col-lg-12">
    <select name="project_id" id="project_id" class="form-control">
    <option value="">Please Select</option>
    @foreach ($projects as $item)
    <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
    @endforeach
    </select>
    </div>
    <label for="milestone" class="control-label mb-1">Milestone</label>
    <div class="input-group col-lg-12">
    <select multiple="multiple" name="milestone_id" id="milestone_id" class="form-control">
    <option value="">Please Select</option>
    @foreach ($milestones as $item)
    <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
    @endforeach
    </select>
    </div>
    <button type="submit" class="btn btn-lg btn-info btn-block"></button>
    </form>


    This is my controller (ProjectMilestoneController.php) and I take value from 2 model (Milestone and Project)



    class ProjectMilestoneController extends Controller
    {
    public function index()
    {
    $projects = Project::get();
    $milestones = Milestone::get();
    return view('takstable.projectmiles',['projects'=>$projects, 'milestones'=>$milestones]);
    }

    public function store(Request $request)
    {
    ProjectMilestone::create($request->all());
    return redirect()->route('project-miles.index');
    }
    }


    This is my model (ProjectMilestone.php)



    class ProjectMilestone extends Model
    {
    protected $fillable = ['project_id','milestone_id'];
    public function project()
    {
    return $this->belongsTo(Project::class,'project_id','id');
    }

    public function milestone()
    {
    return $this->belongsTo(Milestone::class,'milestone_id','id');
    }
    }


    Thank you in advanced










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      would you like to help me? I want to input multiple value and store in database with different row. For example :



      In input form,



      |project_id    milestone_id|
      | 1 1,2,3 |
      | 2 2,4 |


      My expected result in database



      |project_id    milestone_id|
      | 1 1 |
      | 1 2 |
      | 1 3 |
      | 2 2 |
      | 2 4 |


      This is my view (tasktableprojectmiles.blade.php)



      <form action="{{route('project-miles.store')}}" method="post">
      <label for="project" class="control-label mb-1">Project</label>
      <div class="input-group col-lg-12">
      <select name="project_id" id="project_id" class="form-control">
      <option value="">Please Select</option>
      @foreach ($projects as $item)
      <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
      @endforeach
      </select>
      </div>
      <label for="milestone" class="control-label mb-1">Milestone</label>
      <div class="input-group col-lg-12">
      <select multiple="multiple" name="milestone_id" id="milestone_id" class="form-control">
      <option value="">Please Select</option>
      @foreach ($milestones as $item)
      <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
      @endforeach
      </select>
      </div>
      <button type="submit" class="btn btn-lg btn-info btn-block"></button>
      </form>


      This is my controller (ProjectMilestoneController.php) and I take value from 2 model (Milestone and Project)



      class ProjectMilestoneController extends Controller
      {
      public function index()
      {
      $projects = Project::get();
      $milestones = Milestone::get();
      return view('takstable.projectmiles',['projects'=>$projects, 'milestones'=>$milestones]);
      }

      public function store(Request $request)
      {
      ProjectMilestone::create($request->all());
      return redirect()->route('project-miles.index');
      }
      }


      This is my model (ProjectMilestone.php)



      class ProjectMilestone extends Model
      {
      protected $fillable = ['project_id','milestone_id'];
      public function project()
      {
      return $this->belongsTo(Project::class,'project_id','id');
      }

      public function milestone()
      {
      return $this->belongsTo(Milestone::class,'milestone_id','id');
      }
      }


      Thank you in advanced










      share|improve this question













      would you like to help me? I want to input multiple value and store in database with different row. For example :



      In input form,



      |project_id    milestone_id|
      | 1 1,2,3 |
      | 2 2,4 |


      My expected result in database



      |project_id    milestone_id|
      | 1 1 |
      | 1 2 |
      | 1 3 |
      | 2 2 |
      | 2 4 |


      This is my view (tasktableprojectmiles.blade.php)



      <form action="{{route('project-miles.store')}}" method="post">
      <label for="project" class="control-label mb-1">Project</label>
      <div class="input-group col-lg-12">
      <select name="project_id" id="project_id" class="form-control">
      <option value="">Please Select</option>
      @foreach ($projects as $item)
      <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
      @endforeach
      </select>
      </div>
      <label for="milestone" class="control-label mb-1">Milestone</label>
      <div class="input-group col-lg-12">
      <select multiple="multiple" name="milestone_id" id="milestone_id" class="form-control">
      <option value="">Please Select</option>
      @foreach ($milestones as $item)
      <option value="{{$item->id}}">{{$item->value}} - {{$item->description}}</option>
      @endforeach
      </select>
      </div>
      <button type="submit" class="btn btn-lg btn-info btn-block"></button>
      </form>


      This is my controller (ProjectMilestoneController.php) and I take value from 2 model (Milestone and Project)



      class ProjectMilestoneController extends Controller
      {
      public function index()
      {
      $projects = Project::get();
      $milestones = Milestone::get();
      return view('takstable.projectmiles',['projects'=>$projects, 'milestones'=>$milestones]);
      }

      public function store(Request $request)
      {
      ProjectMilestone::create($request->all());
      return redirect()->route('project-miles.index');
      }
      }


      This is my model (ProjectMilestone.php)



      class ProjectMilestone extends Model
      {
      protected $fillable = ['project_id','milestone_id'];
      public function project()
      {
      return $this->belongsTo(Project::class,'project_id','id');
      }

      public function milestone()
      {
      return $this->belongsTo(Milestone::class,'milestone_id','id');
      }
      }


      Thank you in advanced







      laravel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 3:40









      M. Syafi'i Asy'ari

      54




      54
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The Eloquent way to achieve this is,



          public function store(Request $request)
          {

          foreach($request->milestone_id as $milestone_id) {

          ProjectMilestone::create([
          'project_id' => $request->project_id,
          'milestone_id' => $milestone_id
          ]);
          }

          return redirect()->route('project-miles.index');
          }


          But this affect the efficiency of the system because if there are n number of milestones selected, there will be n number of queries to the database.



          In order to avoid this issue.



          public function store(Request $request)
          {

          $prject_milestone = collect();

          foreach($request->milestone_id as $milestone_id) {

          $project_milestones->push(
          ProjectMilestone::make([
          'project_id' => $request->project_id,
          'milestone_id' => $miletone_id
          ])
          );
          }

          DB::table('project_milestones')->insert($project_milestones);

          return redirect()->route('project-miles.index');
          }


          this will execute only one query to the database.



          The Choice is yours






          share|improve this answer





















          • thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
            – M. Syafi'i Asy'ari
            Nov 22 at 4:16










          • Finally I use the first code and it works, thank you very much
            – M. Syafi'i Asy'ari
            Nov 22 at 9:46










          • In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
            – Tharaka Dilshan
            Nov 22 at 9:50










          • ah thank you, it's works
            – M. Syafi'i Asy'ari
            Nov 23 at 3:11


















          up vote
          0
          down vote













          You have to make array on view also. ex :



          <select name="milestone_id" id="milestone_id" class="form-control">


          Refer @tharakaDilshan's answer for controller.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423551%2flaravel-store-multiple-value-in-different-row%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            The Eloquent way to achieve this is,



            public function store(Request $request)
            {

            foreach($request->milestone_id as $milestone_id) {

            ProjectMilestone::create([
            'project_id' => $request->project_id,
            'milestone_id' => $milestone_id
            ]);
            }

            return redirect()->route('project-miles.index');
            }


            But this affect the efficiency of the system because if there are n number of milestones selected, there will be n number of queries to the database.



            In order to avoid this issue.



            public function store(Request $request)
            {

            $prject_milestone = collect();

            foreach($request->milestone_id as $milestone_id) {

            $project_milestones->push(
            ProjectMilestone::make([
            'project_id' => $request->project_id,
            'milestone_id' => $miletone_id
            ])
            );
            }

            DB::table('project_milestones')->insert($project_milestones);

            return redirect()->route('project-miles.index');
            }


            this will execute only one query to the database.



            The Choice is yours






            share|improve this answer





















            • thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
              – M. Syafi'i Asy'ari
              Nov 22 at 4:16










            • Finally I use the first code and it works, thank you very much
              – M. Syafi'i Asy'ari
              Nov 22 at 9:46










            • In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
              – Tharaka Dilshan
              Nov 22 at 9:50










            • ah thank you, it's works
              – M. Syafi'i Asy'ari
              Nov 23 at 3:11















            up vote
            0
            down vote



            accepted










            The Eloquent way to achieve this is,



            public function store(Request $request)
            {

            foreach($request->milestone_id as $milestone_id) {

            ProjectMilestone::create([
            'project_id' => $request->project_id,
            'milestone_id' => $milestone_id
            ]);
            }

            return redirect()->route('project-miles.index');
            }


            But this affect the efficiency of the system because if there are n number of milestones selected, there will be n number of queries to the database.



            In order to avoid this issue.



            public function store(Request $request)
            {

            $prject_milestone = collect();

            foreach($request->milestone_id as $milestone_id) {

            $project_milestones->push(
            ProjectMilestone::make([
            'project_id' => $request->project_id,
            'milestone_id' => $miletone_id
            ])
            );
            }

            DB::table('project_milestones')->insert($project_milestones);

            return redirect()->route('project-miles.index');
            }


            this will execute only one query to the database.



            The Choice is yours






            share|improve this answer





















            • thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
              – M. Syafi'i Asy'ari
              Nov 22 at 4:16










            • Finally I use the first code and it works, thank you very much
              – M. Syafi'i Asy'ari
              Nov 22 at 9:46










            • In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
              – Tharaka Dilshan
              Nov 22 at 9:50










            • ah thank you, it's works
              – M. Syafi'i Asy'ari
              Nov 23 at 3:11













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            The Eloquent way to achieve this is,



            public function store(Request $request)
            {

            foreach($request->milestone_id as $milestone_id) {

            ProjectMilestone::create([
            'project_id' => $request->project_id,
            'milestone_id' => $milestone_id
            ]);
            }

            return redirect()->route('project-miles.index');
            }


            But this affect the efficiency of the system because if there are n number of milestones selected, there will be n number of queries to the database.



            In order to avoid this issue.



            public function store(Request $request)
            {

            $prject_milestone = collect();

            foreach($request->milestone_id as $milestone_id) {

            $project_milestones->push(
            ProjectMilestone::make([
            'project_id' => $request->project_id,
            'milestone_id' => $miletone_id
            ])
            );
            }

            DB::table('project_milestones')->insert($project_milestones);

            return redirect()->route('project-miles.index');
            }


            this will execute only one query to the database.



            The Choice is yours






            share|improve this answer












            The Eloquent way to achieve this is,



            public function store(Request $request)
            {

            foreach($request->milestone_id as $milestone_id) {

            ProjectMilestone::create([
            'project_id' => $request->project_id,
            'milestone_id' => $milestone_id
            ]);
            }

            return redirect()->route('project-miles.index');
            }


            But this affect the efficiency of the system because if there are n number of milestones selected, there will be n number of queries to the database.



            In order to avoid this issue.



            public function store(Request $request)
            {

            $prject_milestone = collect();

            foreach($request->milestone_id as $milestone_id) {

            $project_milestones->push(
            ProjectMilestone::make([
            'project_id' => $request->project_id,
            'milestone_id' => $miletone_id
            ])
            );
            }

            DB::table('project_milestones')->insert($project_milestones);

            return redirect()->route('project-miles.index');
            }


            this will execute only one query to the database.



            The Choice is yours







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 3:59









            Tharaka Dilshan

            1,7861410




            1,7861410












            • thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
              – M. Syafi'i Asy'ari
              Nov 22 at 4:16










            • Finally I use the first code and it works, thank you very much
              – M. Syafi'i Asy'ari
              Nov 22 at 9:46










            • In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
              – Tharaka Dilshan
              Nov 22 at 9:50










            • ah thank you, it's works
              – M. Syafi'i Asy'ari
              Nov 23 at 3:11


















            • thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
              – M. Syafi'i Asy'ari
              Nov 22 at 4:16










            • Finally I use the first code and it works, thank you very much
              – M. Syafi'i Asy'ari
              Nov 22 at 9:46










            • In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
              – Tharaka Dilshan
              Nov 22 at 9:50










            • ah thank you, it's works
              – M. Syafi'i Asy'ari
              Nov 23 at 3:11
















            thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
            – M. Syafi'i Asy'ari
            Nov 22 at 4:16




            thank you for your help. I try to use the second code and I got error like this Argument 1 passed to IlluminateDatabaseQueryBuilder::insert() must be of the type array, object given, called in C:xampp3htdocsbudgetingappHttpControllersProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
            – M. Syafi'i Asy'ari
            Nov 22 at 4:16












            Finally I use the first code and it works, thank you very much
            – M. Syafi'i Asy'ari
            Nov 22 at 9:46




            Finally I use the first code and it works, thank you very much
            – M. Syafi'i Asy'ari
            Nov 22 at 9:46












            In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
            – Tharaka Dilshan
            Nov 22 at 9:50




            In case you are still interested in the second approach, maybe the Collection objects causing the error. so try this. DB::table('project_milestones')->insert($project_milestones->toArray()));
            – Tharaka Dilshan
            Nov 22 at 9:50












            ah thank you, it's works
            – M. Syafi'i Asy'ari
            Nov 23 at 3:11




            ah thank you, it's works
            – M. Syafi'i Asy'ari
            Nov 23 at 3:11












            up vote
            0
            down vote













            You have to make array on view also. ex :



            <select name="milestone_id" id="milestone_id" class="form-control">


            Refer @tharakaDilshan's answer for controller.






            share|improve this answer

























              up vote
              0
              down vote













              You have to make array on view also. ex :



              <select name="milestone_id" id="milestone_id" class="form-control">


              Refer @tharakaDilshan's answer for controller.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You have to make array on view also. ex :



                <select name="milestone_id" id="milestone_id" class="form-control">


                Refer @tharakaDilshan's answer for controller.






                share|improve this answer












                You have to make array on view also. ex :



                <select name="milestone_id" id="milestone_id" class="form-control">


                Refer @tharakaDilshan's answer for controller.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 4:05









                Dhruv Raval

                519111




                519111






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423551%2flaravel-store-multiple-value-in-different-row%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Trompette piccolo

                    Slow SSRS Report in dynamic grouping and multiple parameters

                    Simon Yates (cyclisme)