我想通过组合框进行多项选择并将它们插入数据库。我的想法是制作一个阵列,然后提出他的呼吁。 通过我所做的,单一选择增加了基础。有人会知道如何将所有选定的区域插入我的数据库。谢谢
模型1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class zoneintervention extends Model
{
public function techniciens()
{
return $this-
belongsToMany(&#39;应用软件\技术员&#39;&#39; technicien_zone&#39;&#39; zoneintervention_id&#39;&#39; technicien_id&#39);
}
}
模型2
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class technicien extends Model
{
public function zoneintervention()
{
return $this-
belongsToMany(&#39;应用软件\ zoneintervention&#39;&#39; technicien_zone&#39;&#39; technicien_id&#39;&#39; Z oneintervention_id&#39);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\technicien;
use App\zoneintervention;
class TechnicienController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Listzone=zoneintervention::orderBy('code_postal')->get();
$Listtechnicien=technicien::all();
return view('technicien.index',['technicien'=>$Listtechnicien]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$zoneintervention = Zoneintervention::orderBy('id', 'desc')-
>get();
return view('technicien.create')->with('zoneintervention',
$zoneintervention);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$technicien = new technicien();
$technicien ->actif =$request->input('actif');
$technicien ->moyenne_avis =$request->input('moyenne_avis');
$technicien = new technicien();
$technicien->actif = $request->input('actif');
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();
$technicien->zoneintervention()->attach($request-
>zoneintervention_id);
return redirect('technicien');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
创建
@extends('Layouts/app')
@section('content')
@if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
@foreach($errors ->all() as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('technicien') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="zoneintervention">zoneintervention</label>
<select multiple name="zoneintervention_id"
id="zoneintervention" class="form-control" >
@foreach($zoneintervention as
$zoneintervention)
<option value="{{ $zoneintervention->id }}">
{{$zoneintervention->code_postal}}
</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-
control"value="{{old('moyenne_avis')}}">
</div>
<div class="form-group">
<label for="">Etat</label>
<input type="text" name ="actif" class="form-
control"value="{{old('actif')}}">
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
@endsection
答案 0 :(得分:0)
在您的刀片文件中,您可以执行以下操作:
...
<select multiple name="zoneintervention_id[]" id="zoneintervention" class="form-control" >
...
在你的控制器中,
public function store(Request $request)
{
//This will give you an array of all the selected item
dd($request['zoneintervention_id']);
}