我有两张桌子,一张桌子“métier”和一张桌子“tâche”,有一对多连接,“métier”有几个“tâche”。在我的形式“ajoutertâches”中我有一个组合框,我从中选择了与“tâche”相关的“métier”。 现在我只想用libelle_metier填充表metier中的组合,然后将我选择的libelle_metier的id添加到表tache然后将其添加到metier_id.Any help?
模型专家
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}
模型专家
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
public function metier()
{
return $this->belongsTo(Metier::class);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
tache.blade.php
@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 ('tache') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Libelle Tache</label>
<input type="text" name ="libelle_tache"
class="form-control"value="{{old('libelle_tache')}}">
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-
control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<label for="metier">metier</label>
<select name="metier_id" id="metier" class="form-
control">
@foreach($metiers as $metier)
<option value="{{$metier}}">
{{$metier->libelle_metier}}
</option>
@endforeach
</select>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
@endsection
tache controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Listmetier=metier::orderBy('libelle_metier')->get();
$Listtache=tache::all();
return view('tache.index',['tache'=>$Listtache]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('id', 'desc')->get();
return view('tache.create')->with('metiers', $metiers);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id = $request->input('metier_id');
$tache->save();
return redirect('tache');
}
/**
* 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)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}
/**
* 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)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
答案 0 :(得分:0)
如果我理解正确,您希望将metier id
传递到metiers
的地方,则需要更改
<强> tache.blade.php 强>
...
<option value="{{ $metier->id }}">
...