如何使用先前表单中的ID自动填充表单 [英] how to autofill a form with id from a previous form
问题描述
我以前有一个创建用户和技术人员的表格.数据已很好地填充到数据库的表中.现在,该表单将我重定向到另一个需要技术人员ID的表单.我正在努力使新表格中的技术人员ID自动从以前的表格中填写.我希望我的问题很清楚,希望您能帮到我.我把我的代码和想要自动填写的表格放进去了.希望你能对我有所帮助.
I have a previous form in which i created a user and technician. the data is well filled in the tables of the database. Now the form redirects me to another form in which the technician id is needed. I'm working to make the technician id in the new form auto filled from the previous form. I hope my question is clear and I which you could help me. I put my code and the form I want to be auto-filled.Hope you could help me.
Model1
public function tarificationtache()
{
return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicien_id','tarificationtache_id');
}
Model2
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
推荐答案
您必须将该值传递给视图.
You have to pass that value to the view.
$technician_id = $user->id;
return view(‘tarification/create’)->with(‘technician_id’, $technician_id);
现在您可以在视图中使用此值.
Now you can use this value in your view.
<div class="form-group">
<label for="technicien">Technicien</label>
<input type="text" value={{$technician_id}} >
</div>
这篇关于如何使用先前表单中的ID自动填充表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!