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
laravel
add a comment |
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
laravel
add a comment |
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
laravel
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
laravel
asked Nov 22 at 3:40
M. Syafi'i Asy'ari
54
54
add a comment |
add a comment |
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
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
add a comment |
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.
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 at 4:05
Dhruv Raval
519111
519111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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