How to autofill form with id from the previous Form - laravel

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]);
}

Related

Missing required parameters for [Route: berita.update]

I'm trying to get the parameter id_berita, it appears in url, but when i want to show the previous data it can't....
Controller
class KontenController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth:admin');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$berita = Berita::all();
return view('backpages/berita', compact('berita'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('backpages.berita_input'); //untuk menampilkan form add
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'id_berita' => 'required',
'id_kategori' => 'required',
'username' => 'required',
'tanggal' => 'required',
'judul' => 'required',
'isi' => 'required',
]);
Berita::create($request->all());
$request->session()->flash('pesan','Berita '.$request['id_berita'].' berhasil disimpan.');
return redirect()->route('berita.index');
}
/**
* Display the specified resource.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function show(Berita $berita)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function edit(Berita $berita)
{
// return view('backpages.berita_edit',compact('berita'));
$berita = Berita::find($berita);
// return view('backpages.berita_edit')->with('berita',$berita);
return view('backpages.berita_edit')->with('berita', $berita);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Berita $berita)
{
request()->validate([
'username' => 'required',
'tanggal' => 'required',
'judul' => 'required',
'isi' => 'required',
]);
$berita->update($request->all());
$request->session()->flash('pesan','Berita '.$request['judul'].' berhasil
diperbarui.');
return redirect()->route('berita.index');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Berita $berita
* #return \Illuminate\Http\Response
*/
public function destroy(Berita $berita)
{
//
}
}
berita_edit.blade.php for update process
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Edit Berita</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form action="{{route('berita.update')}}" method="POST">
{{ csrf_field() }}
{{method_field('PUT')}}
<div class="card-body">
<div class="form-group">
<label for="exampleInputPassword1">Username</label>
<input type="text" class="form-control" name="username" placeholder="Username" value="{{ $berita->username }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Tanggal</label>
<input type="date" class="form-control" name="tanggal" placeholder="Tanggal" value="{{ $berita->tanggal }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Judul</label>
<input type="text" class="form-control" name="judul" placeholder="Judul" value="{{ $berita->judul }}">
</div>
<div class="form-group">
<label>Isi</label>
<textarea class="form-control" rows="3" name="isi" value="{{ $berita->isi }}" placeholder="Enter ..."></textarea>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
For displaying data
<div class="card-body table-responsive p-0">
<table class="table table-hover text-nowrap">
<thead>
<tr>
<th>id_Berita</th>
<th>id_Kategori</th>
<th>Username</th>
<th>Tanggal</th>
<th>Judul</th>
<th>Isi</th>
<th colspan="2" style="text-align:left">Opsi</th>
</tr>
</thead>
<tbody>
#foreach($berita as $data)
<tr>
<td>{{$data -> id_berita}}</td>
<td>{{$data -> id_kategori}}</td>
<td>{{$data -> username}}</td>
<td>{{$data -> tanggal}}</td>
<td>{{$data -> judul}}</td>
<td>{{$data -> isi}}</td>
<td>
<a class="btn btn-block btn-info" href="{{ route('berita.edit', $data -> id_berita) }}"?>Edit</a>
</td>
<td>
<a class="btn btn-block btn-info" href="#"?>Hapus</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
You have to change action like this
<form action="{{route('berita.update',['berita'=>$berita->id])}}" method="POST">
<a class="btn btn-block btn-info" href="{{ route('berita.edit', ['berita'=>$data->id_berita]) }}"?>Edit</a>

Is there any way to get an id in a create method in 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')
);

PostTooLarge Exception Laravel

I have uploaded 15mb image and it throws exception of PostTooLarge Exception instead of exception i have to show flash error message but could not get it.
below is the handler i have used for Handler.php it works great but not display flash message.
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
{
return redirect()->route('users.add')->withFlashError('Image Size too large!');
}
then i tried validate of laravel for image in my controller which is as below
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
}
but still no luck
below is blade file with form:
<form method="post" action="{{route('users.submit')}}" id="adduser" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">User name*</label>
<input type="text" class="form-control" name="name" placeholder="User name" value="{{ old('name') }}">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">User email*</label>
<input type="text" class="form-control" name="email" value="{{ old('email') }}" placeholder="User email" autocomplete="off">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Password*</label>
<input id="password" type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">Confirm password</label>
<input type="password" class="form-control" name="confirmpassword" placeholder="Confirm password">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Phone number*</label>
<input type="text" class="form-control" name="mobile" value="{{ old('mobile') }}" placeholder="Phone Number">
</div>
<div class="col-md-6">
<label class="col-form-label">User role*</label>
<select name="role" class="form-control valid">
<option value="">Select Role</option>
<option {{ old('role')==3?'selected':'' }} value="3">Author</option>
<option {{ old('role')==5?'selected':'' }} value="5">Product Admin</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label class="col-form-label">User image</label>
<div class="card-body">
<div class="was-validated">
<label class="custom-file">
<input type="file" name="image" accept=".jpg, .png,.jpeg" id="file" class="custom-file-input">
<span class="custom-file-control"></span>
</label>
<ul class="preview" style="margin-top: 10px;">
</ul>
</div>
</div>
</div>
</div>
</div>
<button style="float: right;" type="submit" class="btn btn-primary" id="validateForm">Save</button>
</form>
and below is controller code :
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Validator;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::whereNotIn('role', array(1,4))->orderBy('id','DESC')->get();
return view('admin.users.index',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.users.add');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data=$request->all();
$validate = Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
]);
if ($validate->fails()) {
return redirect('users/add')->withFlashError('Email already exists')->withInput();
}
if(isset($_FILES['image']))
{
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
//return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
//return redirect()->route('user')->withErrors(['error' => 'Image size exceeds 4MB']);
//return redirect('users/add')->withFlashError('Image size exceeds 4MB')->withInput();
return redirect()->back()->withErrors(['error' => 'Image size exceeds 4MB']);
}
if(basename($_FILES['image']['name']) != "")
{
$target_path = base_path().'/public/user/';
$time=round(microtime(true));
$newfilename = $target_path .$time . '.jpg';
$target_path = $target_path .time().basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $newfilename))
{
$user_data['image'] =$time.'.jpg';
}
}
else
{
$user_data['image'] = "";
}
}
$insert_data = array
(
"name" => $data['name'],
"email" => $data['email'],
"password" => bcrypt($data['password']),
"mobile" => $data['mobile'],
"image" => $user_data['image'],
"role" => $data['role'],
"status" => 1
);
User::create($insert_data);
return redirect()->route('users')->withFlashSuccess('User added successfully');
}
/**
* 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)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users')->withFlashSuccess('User deleted successfully');
}
public function delete($id)
{
$news = User::find($id);
$news->delete();
}
public function status($id)
{
$store=User::find($id);
if($store->status==1)
{
$status=0;
}
else
{
$status=1;
}
User::whereId($id)->update(array('status'=>$status));
return redirect()->route('users')->withFlashSuccess('User status changed');
}
public function saverole(Request $request)
{
$data=$request->all();
$string_role = implode(', ', $data['role']);
User::whereId($data['user_id'])->update(array('role'=>$string_role));
return redirect()->route('users')->withFlashSuccess('User role changed');
}
}
Change your validate line of code to this:
$this->validate(request(), [
'image' => 'max:4000'
],
[
'image.max' => 'Image size exceeds 4MB'
]);
And inside your view file, run this code to display the errors:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif
Because the php.ini stands first while throwing exception, it will not carry forward that expectation to your defined errors.

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

Laravel 5.2 FatalErrorException in casasController.php line 51

i need your help. i'm building a webApp in Laravel5.2. This is the probles i can't solve...FatalErrorException in casasController.php line 51; i really dont know what to do...the odject is correctly declared in namespace and its saved once validate!
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Oggetto;
use App\Categoria;
use DB;
use Session;
class casasController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$oggetti = DB::table('casas')->get();
$categoriasSelect = DB::table('categorias')->get();
return view('pages.casa')->with('categoriasSelect', $categoriasSelect)->with('oggetti', $oggetti);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'nome' => 'required',
'categoria' => 'required',
'descrizione' => 'required'
]);
$oggetto = new Oggetto;
$oggetto->nome = $request->nome;
$oggetto->categoria = $request->categoria;
$oggetto->descrizione = $request->descrizione;
$status = $request->nome;
$oggetto->save();
Session::flash('success', 'Il tuo oggetto per la casa è stato salvato con successo!');
return view('pages.casa')->with('oggetto', $oggetto);
}
/**
* 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)
{
//
}
}
These are my routes:
Route::get('/', 'pagesController#index');
Route::group(array('prefix' => 'luogo'), function(){
Route::get('casa',['as' => 'opzione1', 'uses' => 'casasController#index']);
Route::get('parco',['as' => 'opzione2', function(){
return view('pages.parco');
}]);
Route::get('stazione',['as' => 'opzione3', function(){
return view('pages.stazione');
}]);
});
Route::post('nome', ['as' => 'nomeInput', 'uses' => 'pagesController#form']);
//Authentication Routes(login) -->built in
Route::get('auth/login', ['as' => 'login', 'uses' => 'Auth\AuthController#getLogin']);
Route::post('auth/login', ['as' => 'loginPost', 'uses' => 'Auth\AuthController#postLogin']);
Route::get('auth/logout', [ 'as' => 'logout', 'uses' => 'Auth\AuthController#getLogout']);
//Registration Routes(new users) -->built in
Route::get('auth/register', ['as' => 'register', 'uses' => 'Auth\AuthController#getRegister']);
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::post('categoria', ['as' => 'categ', 'uses' => 'categoriasController#newCat']);
Route::resource('oggetto/{status?}', 'casasController', ['except' => ['show']]);
This is my view:
#extends('main')
#section('content')
<h1 class="text-center">Benvenuto a Casa</h1>
<h3 class="text-center"> Aggiungi un oggetto</h3>
<div class="row">
<div class="col-md-8 col-md-offset-2">
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">{{$error}}</div>
#endforeach
#elseif(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
<table class="table table-striped">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Descrizione</th>
<th colspan="2">Azioni</th>
</tr>
</thead>
<tbody>
#foreach($oggetti as $oggetto)
<tr>
<td>{{$oggetto->nome}}</td>
<td>{{$oggetto->categoria}}</td>
<td>{{$oggetto->descrizione}}</td>
<td><button type="button" class="btn btn-info btn-block">Editare</button></td>
<td><button type="button" class="btn btn-danger btn-block">Cancellare</button></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
<div class="row" id="formOggetto" style="display: none; margin-top: 25px">
<div class="col-md-8 col-md-offset-2">
<form action="{{action('casasController#store')}}" method="POST">
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
<div class="form-group">
<label for="nome">Nome</label>
<input type="text" class="form-control" id="nome" name="nome" placeholder="Nome">
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="categoria">Categoria</label>
<select class="form-control" name="categoria">
<option value="null">Scegli...</option>
#foreach($categoriasSelect as $categoriaSelect)
<option>{{$categoriaSelect->nome}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<label>Aggiungi nuova categoria</label>
<button type="button" class="btn btn-warning btn-block" data-toggle="modal" data-target="#modalCateg">Aggiungi</button>
</div>
</div>
<div class="form-group">
<label for="descrizione">Descrizione</label>
<textarea type="text" class="form-control" id="descrizione" name="descrizione" placeholder="Descrizione"></textarea>
</div>
<input type="submit" class="btn btn-primary btn-block" value="Salva">
</form>
</div>
</div>
#stop
Ok, you don't have Oggetto model, so you need to create it first.
Also change this:
$oggetto = new Oggetto;
to this:
$oggetto = new Oggetto();

Resources