如何使用上一个表单中的ID自动填充表单

时间:2018-02-22 11:06:04

标签: laravel

我有一个以前的表单,其中我创建了一个用户和技术人员。数据填写在数据库的表中。现在,表单将我重定向到另一种需要技术人员ID的表单。我正在努力使新表单中的技术人员ID自动填充在上一个表单中。我希望我的问题很清楚,我可以帮助我。我把我的代码和我想要的表格自动填充。希望你能帮助我。

型号1

public function tarificationtache()
{
    return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicien_id','tarificationtache_id');
}

模型2

public function techniciens()
{
    return $this->belongsToMany('App\technicien','technicien_tarificationtache','tarificationtache_id','technicien_id');

}

technicien.Controller

public function store(Request $request)
{




    $user = new user();
    $user->nom = $request->input('nom');
    $user->prenom = $request->input('prenom');
    $user->tel = $request->input('tel');
    $user->mobil = $request->input('mobil');
    $user->role = $request->input('role');
    $user->email = $request->input('email');
    $user->password = $request->input('password');
    $user->save();

    $technicien = new technicien();
    $technicien->user_id = $user->id;
    $technicien->actif = $request->input('actif');
    $technicien->moyenne_avis = $request->input('moyenne_avis');
    $technicien->save();
    $technicien->zoneintervention()->attach($request->zoneintervention_id);

    $technicien->metier()->attach($request->metier_id);

    return redirect('tarification/create');

}

tarification.controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\tarificationtache;
use App\technicien;
use App\tache;

class TarificationController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
 public function index()
  {
    $Listtache=tache::orderBy('libelle_tache')->get();
    $Listtarification=tarificationtache::all();
    return view('tarification.index',
['tarification'=>$Listtarification]);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $techniciens = technicien::orderBy('id','desc')->get();
    $taches = Tache::orderBy('id', 'desc')->get();
    return view('tarification.create')->with('taches', $taches)->with('techniciens', $techniciens);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $tarification = new tarificationtache();

    $tarification ->tache_id = $request->input('tache_id');
    $tarification ->Tarif =$request->input('Tarif');
    $tarification->save();
    $tarification->techniciens()->attach($request->technicien_id);
    return redirect('tarification');
}

/**
 * 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'=>$tache],
['Tarif'=>$tache],['metier_id'=>$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');
}
}

查看

@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 ('tarification')  }}" method="post">
         {{csrf_field()}}

            <div class="form-group">
                <label for="technicien">Technicien</label>
                <select name="technicien_id" id="technicien" class="form-control" >

                        @foreach($techniciens as $techniciens)
                         <option value="{{ $techniciens->id }}">
                            {{$techniciens->id}}
                         </option>
                        @endforeach
                </select>
            </div>

            <div class="form-group">
                <label for="Tache">Libelle Tache</label>
                <select name="tache_id" id="Tache" class="form-control">

                        @foreach($taches as $tache)
                         <option value="{{ $tache->id }}">
                            {{$tache->libelle_tache}}
                         </option>
                        @endforeach
                </select>
            </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">
                <input type="submit" value = "enregistrer" class="form-control btn btn-primary">
            </div>
        </form>
    </div>
</div>


<script>

@endsection

enter image description here

3 个答案:

答案 0 :(得分:0)

您必须将该值传递给视图。

$technician_id = $user->id;
return view(‘tarification/create’)->with(‘technician_id’, $technician_id);

现在您可以在视图中使用此值。

<div class="form-group">
            <label for="technicien">Technicien</label>
            <input type="text" value={{$technician_id}} >
        </div>

答案 1 :(得分:0)

technicien.controller

public function store(Request $request)
{




    $user = new user();
    $user->nom = $request->input('nom');
    $user->prenom = $request->input('prenom');
    $user->tel = $request->input('tel');
    $user->mobil = $request->input('mobil');
    $user->role = $request->input('role');
    $user->email = $request->input('email');
    $user->password = $request->input('password');
    $user->save();

    $technicien = new technicien();
    $technicien->user_id = $user->id;
    $technicien->actif = $request->input('actif');
    $technicien->moyenne_avis = $request->input('moyenne_avis');
    $technicien->save();
    $technicien->zoneintervention()->attach($request->zoneintervention_id);

    $technicien_id = $technicien->id;
    return redirect('tarification/create')->with(‘technicien_id’, $technicien_id);


}

<强> tarification.view

<div class="form-group">
                <label for="technicien">Technicien</label>
                <select name="technicien_id" id="technicien" 
class="form-control" >

                        @foreach($techniciens as $techniciens)
                         <option value="{{ $techniciens->id }}">
                            {{ $technicien_id }}
                         </option>
                        @endforeach
                </select>
            </div>

答案 2 :(得分:0)

这是我新编写的代码,就像你告诉我的那样

enter image description here