1
votes

I've already made an connection many to many. The code below explains what I did. I have retrieved the intervention zones in a multiple choice combobox and now I'm trying to insert them in the database.When I try to choose more than one zone the error message (in the title) is displayed. I'd be grateful if someone could help me with this.

Mode 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);
}

}

Model 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');

}
}

Controller

 <?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)
{
    //
}
}

View

@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');
}
}

My Data Base]1

1

1 Answers

1
votes

With many to many relationship you can't do this:

$technicien->zoneintervention_id= $request->input('zoneintervention_id');

Use the attach() method instead to attach an object to other one:

$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');