Is there any way to get an id in a create method in laravel - laravel

I am trying to pass a foreign directly to my create method
In the project I am working on, I have two different users. The first one is a farm who can create an animal
The second one is the clinic who can attach to an animal some clinic details showing if the animal was vaccinated or not. In my clinic details table, I have the animal as a foreign key but I do not know how I will pass that key to the create method
Here is my Clinic controller
<?php
namespace App\Http\Controllers;
use App\Animal;
use App\Clinic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ClinicController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = Auth::user();
$animal = Animal::all();
return view('clinic.index', compact('user', 'animal'));
}
/**
* Show the form for creating a new resource.
*
* #param $id
* #return void
*/
public function create($id)
{
$user = Auth::user();
$animal = Animal::query()->findOrFail($id);
$clinic = new Clinic();
return view('clinic/create', compact('user', 'animal', 'clinic'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$animal = Animal::query()->findOrFail($id);
return view('clinic.show', compact('animal'));
}
/**
* 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)
{
//
}
}
My clinic index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h3>Clinic Details Dashboard</h3></div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in! {{ $user->name}}
<hr>
<center><h3>Animals</h3></center>
<hr>
#foreach($animal as $animal)
<div class="row">
<div class="col-2">{{ $animal->id }}</div>
<div class="col-4">{{ $animal->type->category }}</div>
<div class="col-2">{{ $animal->user->name }}</div>
<div class="col-4">{{ $animal->created_at }}</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
my clinic show.blade.php
This is where I would like to pass the animal id to the create method but I do not know how.
Even my button link is not going to the create view
#extends('layouts.app')
#section('title', 'Details for animal ')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<center><h3>Details for the animal</h3></center>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $animal->id }}</p>
<p><strong>Animal: </strong>{{ $animal->type->category }}</p>
<p><strong>Farm: </strong>{{ $animal->user->name }}</p>
<p><strong>Gender: </strong>{{ $animal->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $animal->placeOfBirth }}</p>
<p><strong>Date: </strong>{{ $animal->created_at }}</p>
</div>
<div class="col-md-8 offset-md-4">
<a href="create">
<button type="button" class="btn btn-primary">
Attach Clinic Detail
</button>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
clinic create.blade.php
This view is still somehow empty
#extends('layouts.app')
#section('title', 'Add Clinical Details')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New Animal</div>
<div class="card-body">
<form action="{{ url('clinic') }}" method="POST">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection

You could simply add a parameter to your method like this:
public function create($id, $foreignKeyId) {
// ... to something
}
For that you would need to pass id as a hidden input.
You could also try to create a relation, that way you do not need to add the foreignKey as a parameter. You could simply access the foreign_key using the specified ORM Model.

You can use insertGetId() method for retrieving last created id after store method.
https://laravel.com/api/5.0/Illuminate/Database/Query/Builder.html#method_insertGetId
$id = DB::table('posts')->insertGetId(
array('title' => 'my post title', 'body' => 'my post body')
);

Related

Laravel 9. Blade components

is it possible to pass collections to a component like this:
class projectAccordion extends Component
{
/**
* Create a new component instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
$projects = Project::with('subservice', 'partner')->orderByDesc('id')->limit(4)->get();
return view('components.front.project-accordion', compact('projects'));
}
}
the component itself looks like this:
#foreach($projects as $project)
<div class="info-list-wrapper">
<div class="row">
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-title">
<div class="date">
<span>{{ date('d F, Y', strtotime($project->created_at)) }}</span>
</div>
<h4>
{{ $project->title }}
</h4>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-txt">
<p>
{{ $truncated = Str::of($project->descr)->limit(100) }}
</p>
</div>
</div>
<div class="col-xl-4 col-lg-4 col-md-12 project-info-list">
<div class="info-list-img">
<img src="{{ $project->partner->file_url }}" alt="">
</div>
</div>
</div>
</div>
#endforeach
this is how i call the component in homeage.blade.php
<x-front.project-accordion></x-front.project-accordion>
This method does not work on hosting, but everything works on the local version.Is this the right approach?

Laravel 8 Components

I am trying to create a component, and send it some data.
<div class="card">
<div class="card-wrapper">
<div class="card-header">
<div class="card-title">
<i class="fa fa-pie-{{ $icon ?? '' }} fa-fw"></i>
<span>{{ $title ?? '' }}</span>
</div>
<div class="card-version">
<i class="fa fa-question-circle fa-fw"></i>
</div>
</div>
<div class="card-body">
{{ $content ?? '' }}
</div>
</div>
</div>
I am then calling that component in Blade using
<x-com-card icon="globe" title="Test"> </x-com-card>
But I am getting the following error
Undefined variable $icon
My component controller looks like this
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class ComCard extends Component
{
private $icon;
private $title;
/**
* Create a new component instance.
*
* #param $icon
* #param $title
*/
public function __construct($icon, $title)
{
$this->icon = $icon;
$this->title = $title;
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('com.card');
}
}
Any idea why this is happening?
It is because you set your properties as private, and view can not access them.
When your component is rendered, you may display the contents of your
component's public variables by echoing the variables by name
public $icon;
public $title;
More you can find here, in official documentation

Laravel page not found at create function

Recently I started programming using Laravel as a framework. Everything goes fine, but I tried to write a create script with the following message at runtime:
Sorry, the page you are looking for could not be found.
That's after posting Accept in the Create form, I already have created Index, Show, Update and delete functions successfully. So the route to my controller is correct, the file exist ... I'm totally stuck and can not see what incorrect.
Please help.
I didn't redirect the public folder so I'm still using the full path with no problem at the other modules (/gymmgr/public/lockers).
I send you the code:
index.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Lockers</a>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-sm-1 d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Acciones</span>
<a class="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle"></span>
</a>
</h6>
<ul class="nav flex-column">
<li class="nav-item">
<span data-feather="home"></span>
<span class="sr-only"></span>
</li>
<li class="nav-item">
<a class="nav-link" href="/gymmgr/public/lockers/create">
<span data-feather="file"></span>
Nuevo
</a>
</ul>
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
<h1 class="h5">Catálogo</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group mr-2">
<button class="btn btn-sm btn-outline-secondary">Exportar</button>
</div>
</div>
</div>
<table class="table">
<thead class="thead-light">
<tr>
<th>Clave del locker</th>
<th>Ubicación</th>
</tr>
</thead>
<tbody>
#foreach($lockers as $locker)
<tr>
<td> {{ $locker->strClaveLocker }} ></td>
<td>{{ $locker->strUbicacion }}</td>
</tr>
#endforeach
</tbody>
</table>
</main>
</div>
</div>
#endguest
#endsection
LockersController.php
<?php
namespace App\Http\Controllers;
use App\Locker;
use Illuminate\Http\Request;
class LockersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$lockers = Locker::all();
return view('lockers.index', ['lockers'=>$lockers]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
return view('lockers.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
/*
if (Auth::check())
{*/
$locker = Locker::create(
['strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')
]
);
if($locker)
{
return redirect()->route('lockers.index')->with('success','Locker creado con éxito');
}
// }*/
}
/**
* Display the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function show(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
//$locker = Locker::where('idLocker', $locker->idLocker)->first();
//echo e($locker->idLocker);
return view('lockers.show', ['locker'=>$locker]);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function edit(Locker $locker)
{
//
$locker = Locker::find($locker->idLocker);
if ($locker)
{
return view('lockers.edit', ['locker'=>$locker])->with('success', 'Locker encontrado');
};
return view('lockers.edit', ['locker'=>$locker]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Locker $locker)
{
//
$lockerUpdate = Locker::where('idLocker', $locker->idLocker)->update([
'strClaveLocker'=>$request->input('strClaveLocker'),
'strUbicacion'=>$request->input('strUbicacion')]);
if ($lockerUpdate)
{
return redirect()->route('lockers.show',['lockers'=>$locker->idLocker])
->with('success', 'Locker actualizado correctamente');
}
return back()->withInput();
}
/**
* Remove the specified resource from storage.
*
* #param \App\Locker $locker
* #return \Illuminate\Http\Response
*/
public function destroy(Locker $locker)
{
//
$findLocker = Locker::find($locker->idLocker);
if($findLocker->delete())
{
return redirect()->route('lockers.index')
->with('success','Locker borrado exitosamente');
}
return back()->withInput()->with('error','El locker no pudo borrarse');
}
}
create.blade.php
#extends('layouts.app')
#section('content')
#guest
#else
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Locker') }}</div>
<div class="card-body">
</form>
<form method="post" action="{{ route('lockers.create') }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="put">
<div class="form-group">
<label for="locker-clave">Clave del locker<span class="required">*</span></label>
<input placeholder="Clave del locker"
id="locker-clave"
required
name="strClaveLocker"
spellcheck="false"
class="form-control"
/>
</div>
<div class="form-group">
<label for="locker-ubicacion">Ubicación del locker</label>
<input placeholder="Ubicación del locker"
id="locker-ubicacion"
required
name="strUbicacion"
class="form-control"
/>
</div>
<div class="form-group">
<input type="submit"
class="btn btn-primary"
value="Aceptar"
/>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endguest
#endsection
The route
web.php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('lockers','LockersController');
Your create form has:
<form method="post" action="{{ route('lockers.create') }}">
...
<input type="hidden" name="_method" value="put">
So you are sending a PUT request to your create route. As the docs describe, the .create route the resource declaration sets up is expecting a GET (check the table of actions it sets up). So that request will fail with a 404 as there is no matching route set up.
Also, though confusingly named, the create route is for displaying the create form, not saving data you've entered into that form. So you should not be submitting data to your create route.
You need to:
Change your form action to point to your store route: ... action="{{ route('lockers.store') }}
Remove the form method spoofing, store expects POST which is what you'll get by default without any spoofing.

How to autofill form with id from the previous Form

I'm trying to autofill form with id of techinician from the previous form after clicking add button.
I have a list of technicians and each technician has multiple "tarification tache"associated with him then
when i press the add button i am directed to the form to add "tarification tache" in this form i have to choose the technician. So I want the form to be autofilled with the ID of the technician in question and this from the previous form.
#extends('Layouts/app')
#extends('Layouts.master')
#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
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var getMetiersByTechnicienUrl = "{{url('/metiersbytechnicien')}}";
var getTachesByMetierUrl = "{{url('/tachesbymetier')}}";
//console.log(getMetiersByTechnicienUrl,getTachesByMetierUrl,
getTarificationsByTacheUrl);
function getMetiersByTechnicien(val) {
if(val.length>0) {
var technicien_id = val;
$.get(getMetiersByTechnicienUrl+'/'+technicien_id,function(res) {
var html = '<option value="">-Select-</option>' ;
$.each(res.metiers,function(index,item) {
html+='<option
value="'+item.id+'">'+item.libelle_metier+'</option>';
});
$('#metiers').html(html);
});
}
}
function getTachesByMetier(val) {
if(val.length>0) {
var metier_id = val;
$.get(getTachesByMetierUrl+'/'+metier_id,function(res) {
var html = '<option value="">-Select-</option>' ;
$.each(res.taches,function(index,item) {
html+='<option
value="'+item.id+'">'+item.libelle_tache+'</option>';
});
$('#taches').html(html);
});
}
}
</script>
<div class="container">
<div class="row"></div>
<div class="col-md-12">
Z
<div class="col-md-10">
<h1>Tarification tache</h1>
<form action=" {{url ('tarification') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="technicien">Technicien</label>
<select onchange="getMetiersByTechnicien(this.value)"
name="technicien_id"
id="technicien" class="form-control">
<option value="">-Select-</option>
#foreach($technicien as $t)
<option value="{{$t->id }}">
{{$t->user->nom}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="col-md-4">
<label>Metier: </label>
<select onchange="getTachesByMetier(this.value)" style="width:
200px"
class="productm form-control" id="metiers">
<option value="">-Select-</option>
</select>
</div>
<div class="col-md-4">
<label>tache: </label>
<select style="width: 200px" class="productname form-
control"
name="tache_id" id="taches">
<option value="">-Select-</option>
</select>
</div>
<div class="col-md-4">
<label>tarification: </label>
<input style="width: 200px" class="productname form-
control" type="text"
name ="Tarif" class="form-control" value="{{old('tarif')}}">
</div>
</div>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-primary">
</div>
</div>
</div>
</div>
#endsection
tarificationcontroller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\tarificationtache;
use App\technicien;
use App\tache;
use App\metier;
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()
{
$technicien = technicien::orderBy('id','desc')->get();
$taches = Tache::orderBy('libelle_tache', 'asc')->get();
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tarification.create')->with('taches', $taches)-
>with('technicien', $technicien)-
>with('metiers', $metiers);
}
/**
* 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('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)
{
$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');
}
public function getTarificationsByTache($tache_id)
{
$t =tache::find($tache_id);
return response()->json(['tarifications' => $t->tarificationtache]);
}

New “Metier” is created when editing a “Metier”

When I try to edit a "Metier", a new "Metier" is created and the old one stays the same. I want to crush the old "Metier" and create a new one just by editing. Here is my code in relation with the edit function.
Controller
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
View
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-
primary">
</div>
route
Route::get('/metier', 'MetierController#index');
Route::get('/metier/create', 'MetierController#create');
Route::post('/metier', 'MetierController#store');
Route::get('/metier/{id}/show', 'MetierController#edit');
Route::get('/metier/{id}/edit', 'MetierController#edit');
Route::upd('/metier/{id}/update', 'MetierController#update');
Route::delete('/metier/{id}', 'MetierController#destroy')
MetierController.php
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier = Metier::find($id);
$metier->libelle_metier = $request->libelle_metier;
$metier->save();
return back();
}
edit.blade.php
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action=" {{url ('metier') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
#endsection
That's because you don't even try to update the DB record. Do something like this instead:
public function update(Request $request, $id)
{
Metier::where('id', $id)->update($request->all());
return back();
}
Or without using the mass assignment:
public function update(Request $request, $id)
{
$metier = Metier::find($id);
$metier->libelle_metier = $request->libelle_metier;
$metier->save();
return back();
}
Update
Thanks for sharing the whole form. You're also using POST method instead of PUT. Change the form URL and add this field to the form:
<form action="{{ url('metier/' . $libelle_metier->id . '/update') }}" method="post">
{{ method_field('PUT') }}
Then the update() method will be executed instead of store().
And change the route to put:
Route::put('/metier/{id}/update', 'MetierController#update');
Also, it's a good idea to use Route::resource instead of manually creating the same routes. It will allow you to avoid this kind of errors.
https://laravel.com/docs/5.6/helpers#method-method-field
add {{ method_field('PATCH') }} to your form and change action to named route and pass metier id to it.
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action="{{ route('metier.update', $libelle_metier->id) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
#endsection
Route file
Route::patch('/metier/{id}/update', 'MetierController#update')->name('metier.update');
Hint: delete all these
Route::get('/metier', 'MetierController#index');
Route::get('/metier/create', 'MetierController#create');
Route::post('/metier', 'MetierController#store');
Route::get('/metier/{id}/show', 'MetierController#edit');
Route::get('/metier/{id}/edit', 'MetierController#edit');
Route::upd('/metier/{id}/update', 'MetierController#update');
Route::delete('/metier/{id}', 'MetierController#destroy')
and either add just it all as a single resource, so that all REST urls will be added in one shot.
this is how should be if its REST specification. read it
REST Resource Naming Guide and here
Route::resource('metier', 'MetierController');
or add it this way instead of resource
Route::get('/metier', 'MetierController#index')->name('metier.index');
Route::get('/metier/create', 'MetierController#create')->name('metier.create');
Route::post('/metier', 'MetierController#store')->name('metier.store');
Route::get('/metier/{id}', 'MetierController#show')->name('metier.show');
Route::get('/metier/{id}/edit', 'MetierController#edit')->name('metier.edit');
Route::patch('/metier/{id}', 'MetierController#update')->name('metier.update');
Route::delete('/metier/{id}', 'MetierController#destroy')->name('metier.destroy');
Learn about Resource Controllers
Controller
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
public function update(Request $request, $id)
{
// do some request validation
$metier=Metier::find($id);
$metier->update($request->all());
return redirect()->route('metier.show', $metier->id);
}
if you are having mass assignment error.
add protected $guarded = []; to the Metier model

Resources