我已经与很多人建立了联系。下面的代码解释了我的所作所为。我已经在多选组合框中检索了干预区域,现在我试图将它们插入到数据库中。当我尝试选择多个区域时,会显示错误消息(在标题中)。如果有人可以帮助我,我将不胜感激。
模式1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class technicien extends Model
{
public function zoneintervention()
{
return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','z
oneintervention_id');
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
模型2
namespace App;
use Illuminate\Database\Eloquent\Model;
class zoneintervention extends Model
{
public function techniciens()
{
return $this->belongsToMany('App\technicien','technicien_zone','zoneintervention_id','technicien_id');
}
}
控制器
<?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 ->zoneintervention_id= $request->input('zoneintervention_id');
$technicien->save();
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 name="zoneintervention_id"
id="zoneintervention" class="form-control" multiple="multiple">
@foreach($zoneinterventions 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
technicien_zone
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTechnicienZone extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('technicien_zone', function (Blueprint $table){
$table->integer('technicien_id')->unsigned();
$table->foreign('technicien_id')->references('id')->on('techniciens');
$table->integer('zoneintervention_id')->unsigned();
$table->foreign('zoneintervention_id')->references('id')->on('zoneinterventions');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('technicien_zone');
}
}
答案 0 :(得分:1)
有很多关系,你不能这样做:
$technicien->zoneintervention_id= $request->input('zoneintervention_id');
使用attach()
方法将对象附加到其他对象:
$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');