2

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 (tasktable\projectmiles.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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your help. I try to use the second code and I got error like this Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, object given, called in C:\xampp3\htdocs\budgeting\app\Http\Controllers\ProjectMilestoneController.php on line 45 (line 45 is DB::table('project_milestones')->insert($project_milestones))
Finally I use the first code and it works, thank you very much
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()));
0

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.

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.