代码之家  ›  专栏  ›  技术社区  ›  IT2704

SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“zoneintervention\u id”

  •  1
  • IT2704  · 技术社区  · 7 年前

    我已经建立了多对多的联系。下面的代码解释了我所做的事情。我已经在一个多选组合框中检索到了干预区域,现在我正在尝试将它们插入到数据库中。当我尝试选择多个区域时,会显示错误消息(在标题中)。如果有人能帮我,我将不胜感激。

    模式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\u区

    <?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 回复  |  直到 7 年前
        1
  •  1
  •   Alexey Mezenin    7 年前

    在多对多关系中,你无法做到这一点:

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