Laravel: Attempt to read property "customer_name" on bool - php

I'm new to laravel. I have to edit a form that contains a dropdown list. Edit form works fine works without the dropdown input. Updating with dropdown input throws the above error.
controller:
public function edit(QR_details $qR_detail)
{
return view('pages.edit_QR_details')->with('qr_details', $qR_detail);
}
public function update(Request $request, QR_details $qR_details)
{
$qR_details->update($request->all());
toastr()->success('QR Detail updated successfully');
return redirect()->route(('QR_code_details.index'));
}
Edit form:
form action="{{ route('QR_code_details.update', $qr_details->id) }}" method="POST" class="">
#csrf
#method("PATCH")
<div class="px-3 form-row">
<div class="form-group p-2 col-5">
<label for="" class="form-label fw-bold text-secondary">Customer Name*</label>
<select name="customer_name" class="form-select shadow" name="" id="">
#foreach ($qr_details as $qr_detail)
<option value="{{ $qr_detail->customer_name }}">{{ $qr_detail->customer_name }}</option>
#endforeach
</select>
</div>
<div class="form-group p-2 col-6">
<label for="" class="form-label text-secondary fw-bold">Background Image</label>
<input type="file" name="background image" class="form-control shadow" value="{{ $qr_details->background_image }}">
</div>
<div class="form-group p-2 col-6">
<label for="" class="form-label text-secondary fw-bold">Create QR Code</label>
<input type="file" name="create_qr_code" class="form-control shadow" value="{{ $qr_details->create_qr_code }}">
</div>
<div class="form-group p-2 col-6">
<label for="" class="form-label text-secondary fw-bold">Movable QR</label>
<input type="file" name="movable_qr" class="form-control shadow" value="{{ $qr_details->maovable_qr }}">
</div>
<div class="row ">
<div class="mt-4 col">
<button class="btn bg-gradient-danger btn-sm btn-rounded">Cancel</button>
</div>
<div class=" col mt-4">
<button class="btn bg-gradient-success btn-sm btn-rounded">Save</button>
</div>
</div>
</div>
</form>
Route:
Route::get('QR_code_detail_edit-{qR_detail}', [QRDetailsController::class, 'edit'])->name('QR_code_details.edit');
Route::post('QR_code_detail_edit-{qR_detail}', [QRDetailsController::class, 'update'])->name('QR_code_details.update');
Can someone help me out, how it should be done ?

Related

Can't insert data on my database from laravel

When I try to insert data into my database, Laravel does not insert the records, but it is strange because when I migrate the tables to be able to perform the database, Laravel creates them without any problem, I do not know what I can be doing wrong if the migration run but stored no
Route:
Route::post('/proyecto', [ctrlCars::class,'store'])->name('cars');
Controler:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\cars;
class ctrlCars extends Controller
{
public function store(Request $request){
$request->validate([
'carRegistration'=> ['required','min:6'],
'name'=> ['required','min:6'],
'fromProduction' => ['required','min:6'],
'stateStored'=> ['required'],
'model'=> ['required','min:4'],
'dateAssembled' => ['required'],
]);
$car = new cars;
$car->carRegistration = $request->carRegistration;
$car->name = $request->name;
$car->fromProduction = $request->fromProduction;
$car->stateStored = $request->stateStored;
$car->model = $request->model;
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
#extends('header')
#section('content')
<div class="container w-10 mt-5 border p-4">
<form action="{{ route('cars') }}" method="POST">
#csrf
#if (session('success'))
<h6 class="alert alert-success">{{ session('success') }}</h6>
#endif
#error('carRegistration')
<h6 class="alert alert-danger">{{ $message }}</h6>
#enderror
<p class="h2">Registro vehiculos</p>
<br>
<div class="row">
<section class="col-md-12">
<div class="form-group">
<section class="row">
<div class="col-md-4">
<label for="carRegistration" class="form-label">Placa</label>
<input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div class="col-md-4">
<label for="name" class="form-label">Nombre</label>
<input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
</div>
<div class="col-md-4">
<label for="fromProduction" class="form-label">Planta Produccion</label>
<input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section class="row mt-4">
<div class="col-md-4">
<label for="placa" class="form-label">Fecha Ensamble</label>
<input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
</div>
<div class="col-md-4">
<label for="model" class="form-label">Módelo Matricula</label>
<input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
</div>
<div class="col-md-4">
<label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
<Select type="text" class="form-control" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" class="btn btn-success mt-4">Guardar</button>
</form>
</div>
The problem is from your template. The select tag should have a name attribute.
Change your template to this
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
#extends('header')
#section('content')
<div class="container w-10 mt-5 border p-4">
<form action="{{ route('cars') }}" method="POST">
#csrf
#if (session('success'))
<h6 class="alert alert-success">{{ session('success') }}</h6>
#endif
#error('carRegistration')
<h6 class="alert alert-danger">{{ $message }}</h6>
#enderror
<p class="h2">Registro vehiculos</p>
<br>
<div class="row">
<section class="col-md-12">
<div class="form-group">
<section class="row">
<div class="col-md-4">
<label for="carRegistration" class="form-label">Placa</label>
<input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div class="col-md-4">
<label for="name" class="form-label">Nombre</label>
<input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
</div>
<div class="col-md-4">
<label for="fromProduction" class="form-label">Planta Produccion</label>
<input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section class="row mt-4">
<div class="col-md-4">
<label for="placa" class="form-label">Fecha Ensamble</label>
<input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
</div>
<div class="col-md-4">
<label for="model" class="form-label">Módelo Matricula</label>
<input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
</div>
<div class="col-md-4">
<label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
<Select type="text" name="stateStored" class="form-control" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" class="btn btn-success mt-4">Guardar</button>
</form>
</div>
Thanks for help, the error come from because I call from name on the controller and in this input only have ID, and in the validation for this field have 'required';
I don't know how reply comments, but thanks aynber and Daniel L, you were nice help
First time Comment your validation section and try again. If your data successfully inserted. Then Your need to modify your required field like below.
Replace
['required','min:6']
Like this:
'required|min:6',

Undefined variable: UserRegistration (View: C:\xampp\htdocs\laravel\Form_submit\resources\views\registration.blade.php)

I am new in laravel 8 & I am trying to edit & show the value of that field in a form and i am able to show it in that field.
But when i am trying to add another data it is showing Undefined variable: UserRegistration.
My registration.blade.php code:
<form action="{{$url}}", method="POST">
#csrf
<div class="row">
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1m" name="Fname"
class="form-control form-control-lg" value="{{$UserRegistration->Fname}}" />
<label class="form-label" for="form3Example1m">First name</label>
</div>
</div>
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1n" name="Lname"
class="form-control form-control-lg" value="{{$UserRegistration->Lname}}" />
<label class="form-label" for="form3Example1n">Last name</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1m1" name="mother"
class="form-control form-control-lg" value="{{ $UserRegistration->mother}}" />
<label class="form-label" for="form3Example1m1">Mother's
name</label>
</div>
</div>
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1n1" name="father"
class="form-control form-control-lg" value="{{$UserRegistration->father}}" />
<label class="form-label" for="form3Example1n1">Father's
name</label>
</div>
</div>
</div>
<div class="form-outline mb-4">
<input type="text" id="form3Example8" name="address" value="{{$UserRegistration->address}}" class="form-control form-control-lg" />
<label class="form-label" for="form3Example8">Address</label>
</div>
<div class="d-md-flex justify-content-start align-items-center mb-4 py-2">
<h6 class="mb-0 me-4">Gender: </h6>
<div class="form-check form-check-inline mb-0 me-4">
<input class="form-check-input" type="radio" name="gender"
id="femaleGender" value="M"{{$UserRegistration->gender=='M'?'checked':''}} />
<label class="form-check-label" for="femaleGender">Male</label>
</div>
<div class="form-check form-check-inline mb-0 me-4">
<input class="form-check-input" type="radio" name="gender"
id="maleGender" value="F" {{$UserRegistration->gender=='F'?'checked':''}} />
<label class="form-check-label" for="maleGender">Female</label>
</div>
<div class="form-check form-check-inline mb-0">
<input class="form-check-input" type="radio" name="gender"
id="otherGender" value="O" {{$UserRegistration->gender=='O'?'checked':''}} />
<label class="form-check-label" for="otherGender">Other</label>
</div>
</div>
<div class="row">
<h6 class="mb-0 me-4">State: </h6>
<div class="col-md-6 mb-4">
<select class="select" name="state">
<option value="">State</option>
<option value="Khulna" {{$UserRegistration->state=='Khulna' ? 'selected':''}}>Khulna</option>
<option value="Dhaka" {{$UserRegistration->state=='Dhaka' ? 'selected':''}}>Dhaka</option>
<option value="Dinajpur" {{$UserRegistration->state=='Dinajpur' ? 'selected':''}}>Dinajpur</option>
</select>
</div>
</div>
<div class="row">
<h6 class="mb-0 me-4">City: </h6>
<div class="col-md-6 mb-4">
{{-- <h6 class="mb-0 me-4">City: </h6> --}}
<select class="select" name="city">
<option value="">City</option>
<option value="Khulna" {{$UserRegistration->state=='Khulna' ? 'selected':''}}>Khulna</option>
<option value="Dhaka" {{$UserRegistration->state=='Dhaka' ? 'selected':''}}>Dhaka</option>
<option value="Dinajpur" {{$UserRegistration->state=='Dinajpur' ? 'selected':''}}>Dinajpur</option>
</select>
</div>
</div>
<div class="d-flex justify-content-end pt-3">
<button type="button" class="btn btn-light btn-lg mr-3">Reset all</button>
<button type="submit" name="submit" class="btn btn-warning btn-lg ms-2">{{$btntitle}}</button>
</div>
</form>
My controller UserSignup.php file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserRegistration;
class UserSignup extends Controller
{
//
public function homePage(){
return view('home');
}
public function signup(){
$url=url('/usersignup');
$title="Student registration form";
$btntitle="Submit";
$data=compact('url','title','btntitle');
return view('registration')->with($data);
}
public function storeData(Request $request){
//insert into table
$UserRegistration=new UserRegistration;
$UserRegistration->Fname=$request['Fname'];
$UserRegistration->Lname=$request['Lname'];
$UserRegistration->mother=$request['mother'];
$UserRegistration->father=$request['father'];
$UserRegistration->address=$request['address'];
$UserRegistration->gender=$request['gender'];
$UserRegistration->state=$request['state'];
$UserRegistration->pincode=$request['pincode'];
$UserRegistration->course=$request['course'];
$UserRegistration->city=$request['city'];
$UserRegistration->email=$request['email'];
$UserRegistration->dob=$request['dob'];
$UserRegistration->save();
return redirect('/signup/view');
}
public function viewData(){
// show data
$UserRegistration=UserRegistration::all();
$data=compact('UserRegistration');
return view("customer-view")->with($data);
}
public function edit($id){
$UserRegistration=UserRegistration::find($id);
if (is_null($UserRegistration)) {
# not found
return redirect('/signup/view');
}else{
$url=url('/signup/update').'/'.$id;
$title="Update Student registration form";
$btntitle="Update";
$data=compact('UserRegistration','url','title','btntitle');
return view('registration')->with($data);
}
}
public function update($id,Request $request)
{
$UserRegistration=UserRegistration::find($id);
$UserRegistration->Fname=$request['Fname'];
$UserRegistration->Lname=$request['Lname'];
$UserRegistration->mother=$request['mother'];
$UserRegistration->father=$request['father'];
$UserRegistration->address=$request['address'];
$UserRegistration->gender=$request['gender'];
$UserRegistration->state=$request['state'];
$UserRegistration->pincode=$request['pincode'];
$UserRegistration->course=$request['course'];
$UserRegistration->city=$request['city'];
$UserRegistration->email=$request['email'];
$UserRegistration->dob=$request['dob'];
$UserRegistration->save();
return redirect('/signup/view');
}
}
web.php:
Route::get('/home',[UserSignup::class,'homePage']);
Route::get('/signup',[UserSignup::class,'signup']);
Route::post('/usersignup',[UserSignup::class,'storeData']);
Route::get('/signup/view',[UserSignup::class,'viewData']);
Route::get('/signup/delete/{id}',[UserSignup::class,'delete'])->name('customer.delete');
Route::get('/signup/edit/{id}',[UserSignup::class,'edit'])->name('customer.edit');
Route::post('/signup/update/{id}',[UserSignup::class,'update'])->name('customer.update');
Try This in your registration.blade.php because you are using same file for add and update both.
{{isset($UserRegistration) ? $UserRegistration->Fname : ''}}

Record not updated using laravel

i am making a simple crud system using laravel.all working fine when i going to edit the records ran into the problem with record is not updated i didn't get any error. get the message record updated.when i check the table it is not updated.what i tried so far i attached below
Controller
public function edit(Student $student)
{
return view('students.edit',compact('student'));
}
public function update(Request $request, Student $student)
{
$request->validate([
]);
$student->update($request->all());
return redirect()->route('students.index')
->with('success','Student updated successfully');
}
Edit.blade.php view
<form action="{{ route('students.update',$student->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>StudName:</strong>
<input type="text" name="name" value="{{ $student->studname }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Course:</strong>
<input type="text" name="name" value="{{ $student->course }}" class="form-control" placeholder="course">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Fee</strong>
<input type="text" name="name" value="{{ $student->fee }}" class="form-control" placeholder="fee">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
I think the problem because of your blade file. you have to put the correct name for each input.
<input type="text" name="name" value="{{ $student->studname }}" class="form-control" placeholder="Name">
--------------------------
<input type="text" name="course" value="{{ $student->course }}" class="form-control" placeholder="Course">

GET not supported although I am using PUT in Laravel

I am using Laravel 7 and I can add entries and view them from the database. When I try to edit or update edited changes, I either get a warning from Laravel saying that the The GET method is not supported for this route. Supported methods: PUT. However, I am using PUT in both the web.php route as well as in my method calls. Surely I am doing something wrong.
Here is a view of my Routes calling artisan route:list
in my Route Group in web.php Here are the controllers I am calling:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::put('edit-empresa/{id}', 'Admin\EmpresaController#update');
});
Here is the update function I created in EmpresaController.php:
public function update(Request $request, $id)
{
$this->validate($request, [
'erfc' => 'required',
'enombre' => 'required',
'ecalle' => 'required',
'ecolonia' => 'required',
'eciudad' => 'required',
'eestado' => 'required',
'ecpostal' => 'required',
'epais' => 'required',
]);
$empr = Empresa::find($id);
$empr->erfc = $request->input('erfc');
$empr->enombre = $request->input('enombre');
$empr->ecalle = $request->input('ecalle');
$empr->ecolonia = $request->input('ecolonia');
$empr->eciudad = $request->input('eciudad');
$empr->eestado = $request->input('eestado');
$empr->ecpostal = $request->input('ecpostal');
$empr->epais = $request->input('epais');
$empr->update();
return redirect('/registered-empresa')->with('status', 'Empresa se actualizó correctamente.');
}
And finally, here is the location of my empresas table where I both add, view and update my table in my index.blade.php file:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
Home Page
<span>/</span>
<span>Empresas Registradas</span>
</h4>
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<div class="modal fade" id="modalRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Añadir Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="/save-empresa" method="POST">
{{ csrf_field() }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input type="text" name="erfc" id="orangeForm-erfc" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-erfc">RFC</label>
</div>
<div class="md-form mb-1">
<input type="text" name="enombre" id="orangeForm-enombre" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-enombre">Nombre</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecalle" id="orangeForm-ecalle" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecalle">Calle</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecolonia" id="orangeForm-ecolonia" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecolonia">Colonia</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eciudad" id="orangeForm-eciudad" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eciudad">Ciudad</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eestado" id="orangeForm-eestado" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eestado">Estado</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecpostal" id="orangeForm-ecpostal" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecpostal">Codigo Postal</label>
</div>
<div class="md-form mb-1">
<input type="text" name="epais" id="orangeForm-epais" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-epais">País</label>
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="orangeForm-euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="orangeForm-eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Añadir</button>
</div>
</form>
</div>
</div>
</div>
<div class="text-center">
<i class="fa fa-plus" aria-hidden="true"></i> Add
</div>
<!--edit modal start-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Editar Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="edit-empresa/" id="editForm">
{{ csrf_field() }}
#method('PUT')
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input placeholder="RFC" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Nombre" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Calle" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Colonia" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Ciudad" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Estado" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Codigo Postal" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="País" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
{{-- <div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div> --}}
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
<!--end edit modal-->
</div>
</div>
<!-- Heading -->
<!--Grid row-->
<!--Grid column-->
<div class="row">
<!--Card-->
<div class="col-md-12 mb-4">
<!--Card content-->
<div class="card">
<!-- List group links -->
<div class="card-body">
<table id="datatable2" class="table table-bordered">
<thead>
<tr>
<th>RFC</th>
<th>Nombre</th>
<th>Calle</th>
<th>Colonia</th>
<th>Ciudad</th>
<th>Estado</th>
<th>Codigo Postal</th>
<th>País</th>
<th>Acción</th>
</tr>
</thead>
<tbody>
#foreach ($empresas as $empresa)
<tr>
<td>{{ $empresa->erfc }}</td>
<td>{{ $empresa->enombre }}</td>
<td>{{ $empresa->ecalle }}</td>
<td>{{ $empresa->ecolonia }}</td>
<td>{{ $empresa->eciudad }}</td>
<td>{{ $empresa->eestado }}</td>
<td>{{ $empresa->ecpostal }}</td>
<td>{{ $empresa->epais }}</td>
<td>
<div class="text-center">
Editar
<a class="badge badge-pill btn-danger px-3 py-2" href="">Borrar</a>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- List group links -->
</div>
</div>
<!--/.Card-->
</div>
<!--Grid row-->
</div>
#endsection
#section('scripts')
<script>
$(document).ready(function() {
let table = $('#datatable2').DataTable();
// Start edit record
table.on('click', '.edit', function() {
$tr = $(this).closest('tr');
if($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
let data = table.row($tr).data();
console.log(data);
$('#erfc').val(data[0]);
$('#enombre').val(data[1]);
$('#ecalle').val(data[2]);
$('#ecolonia').val(data[3]);
$('#eciudad').val(data[4]);
$('#eestado').val(data[5]);
$('#ecpostal').val(data[6]);
$('#epais').val(data[7]);
$('#editForm').attr('action', '/edit-empresa/'+data[0]);
$('#editModal').modal('show');
});
// End edit record
});
</script>
#endsection
I am pretty sure it is in this file that I am doing something wrong. Any help on how I can do this better or if I missed something, I would surely appreciate it. Thank you in advance.
in web.php
Route::patch('edit-empresa/{id}', 'Admin\EmpresaController#update');
index.blade.php
#method('PATCH')
You need to specify method as POST when defining the form even though you are including the #method('PUT') inside the form. That is because HTML does not support PUT method directly, and by default it will be a GET. So to correct:
Change this:
<form action="edit-empresa/" id="editForm">
TO
<form action="edit-empresa/" id="editForm" method="POST">
For some reason I could not get this to work using the modal and jquery method so I eliminated the datatables jquery from the bottom of the index.blade.php. My first error was to not call the data first. I created a seperate file called edit.blade.php within the view-admin-empresa folder. Here is the code:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
<span>Empresa Registrada - Editar Empresa</span>
</h4>
</div>
</div>
<!-- Heading -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Editar Empresa</h4>
<form action="{{ url('empresa-update/'.$empresa->id) }}" id="editForm" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<label for="erfc">RFC</label>
<input value="{{ $empresa->erfc }}" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="enombre">Nombre</label>
<input value="{{ $empresa->enombre }}" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecalle">Calle</label>
<input value="{{ $empresa->ecalle }}" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecolonia">Colonia</label>
<input value="{{ $empresa->ecolonia }}" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecuidad">Ciudad</label>
<input value="{{ $empresa->eciudad }}" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="eestado">Estado</label>
<input value="{{ $empresa->eestado }}" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecpostal">Codigo Postal</label>
<input value="{{ $empresa->ecpostal }}" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="epais">País</label>
<input value="{{ $empresa->epais }}" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
Cancelar
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Then in the web.php, I created the following routes:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::get('/edit-empresa/{id}', 'Admin\EmpresaController#edit');
Route::put('/empresa-update/{id}', 'Admin\EmpresaController#update');
});
As mentioned earlier, I eliminated the edit modal and redirected to the empresa edit.blade.php file.
Now, I am able to edit without any problems. Thank you to Arjun bhati and user3532758 for taking a stab at this issue. I really appreciate it.

NotFoundHttpException in RouteCollection.php line 161: laravel 5.2

I created one table in one page, to that table fetching data from database. Then give dynamic buttons for delete and Edit/View. When i click on Delete , it will delete corresponding row from database. Previously it was working properly. But now it showing error "NotFoundHttpException in RouteCollection.php line 161:". Can anyone tell what wrong i did in my code?
My vehicleController.php
<?php
namespace App\Http\Controllers;
use Mail;
use Illuminate\Support\Facades\DB;
use App\Device;
use App\Account;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\Paginator;
class VehicleController extends Controller
{
public $type = 'Device';
public function getIndex()
{
$devices = DB::table('device')->simplePaginate(15);
return view('vehicle.vehicleAdmin')->with('devices', $devices);
}
public function vehicleInsert()
{
$postUser = Input::all();
//insert data into mysql table
$account = Account::select('accountID')->get();
foreach ($account as $acc) {
$abc = $acc->accountID;
}
$data = array("accountID" => $abc,
"vehicleID"=> $postUser['vehicleID']
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('device')->Insert($data);
//echo "Record Added Successfully!";
$devices = DB::table('device')->simplePaginate(50);
return view('vehicle.vehicleAdmin')->with('devices', $devices);
}
public function delete($id)
{
DB::table('device')->where('vehicleID', '=', $id)->delete();
return redirect('vehicleAdmin');
}
public function edit($id)
{
try {
//Find the user object from model if it exists
$devices = DB::table('device')->where('vehicleID', '=', $id)->get();
//$user = User::findOrFail($id);
//Redirect to edit user form with the user info found above.
return view('vehicle.add')->with('devices', $devices);
} catch (ModelNotFoundException $err) {
//redirect to your error page
}
}
}
my vehicleAdmin.blade,php
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="templatemo-content">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Vehicle information</li>
</ol>
<h1>View/Edit Vehicle information</h1>
<p></p>
<div class="row">
<div class="col-md-12">
<div class="table-responsive" style="overflow-x:auto;">
<table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
<h3>Select a Vehicle :</h3>
<thead>
<tr>
<th>Vehicle ID</th>
<th>Unique ID</th>
<th>Description</th>
<th>Equipment Type</th>
<th>SIM Phone</th>
<th>Server ID</th>
<th>Ignition State</th>
<th>Expecting ACK</th>
<th>Active</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($devices as $device)
<tr>
<td>{{ $device->vehicleID }}</td>
<td>{{ $device->uniqueID }}</td>
<td>{{ $device->description }}</td>
<td>{{ $device->equipmentType }}</td>
<td>{{ $device->simPhoneNumber }}</td>
<td></td>
<td>
#if(#$device->ignitionIndex == '0')
OFF
#else
ON
#endif
</td>
<td>{{ $device->expectAck }}</td>
<td>
#if($device->isActive == '1')
Yes
#else
No
#endif
</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li>
View/ Edit
</li>
<li>Delete</li>
</ul>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
{{--{!! $results->appends(['sort' => $sort])->render() !!}--}}
{{$devices->links()}}
</div>
</div>
</div>
</div>
</div>
{{--{!! $device->links()!!}--}}
</br>
<h4>Create a new Vehicle</h4>
<form role="form" method="POST" action="{{ url('vehicleAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6 margin-bottom-15">
<input type="text" class="form-control" name="vehicleID" value="{{ old('vehicleID') }}" placeholder="Enter vehicle ID">
</div>
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
#endsection
Edit page add.blade.php
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">View/Edit Vehicle</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">View/Edit Vehicle Information</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('vehicle/update/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($devices as $device)
<div class="form-group">
<label class="col-md-4 control-label">Vehicle ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="vehicleID" value="{{ ($device->vehicleID)}}" placeholder="Enter User ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Creation date</label>
<div class="col-md-6">
<input type="text" class="form-control" name="creationTime" value="{{ ($device->creationTime)}}">
</div>
</div>
<!--<div class="form-group">
<label class="col-md-4 control-label">Server ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="userID" value="{{ ($device->userID)}}" placeholder="Enter User ID">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">Unique ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="uniqueID" value="{{ ($device->uniqueID)}}" placeholder="Enter Unique ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Active</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->isActive) }}" name="isActive" >
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Vehicle Description</label>
<div class="col-md-6">
<input type="text" class="form-control" name="description" value="{{ ($device->description) }}" placeholder="Enter the description">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Short Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="displayName" value="{{ ($device->displayName) }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Vehicle ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="vehicleID" value="{{ ($device->vehicleID) }}" placeholder="Enter Vehicle ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">License Plate</label>
<div class="col-md-6">
<input type="text" class="form-control" name="licensePlate" value="{{ ($device->licensePlate) }}" placeholder="Enter license Plate">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">License Expiration</label>
<div class="col-md-6">
<input type="text" class="form-control" name="licenseExpire" value="{{ ($device->licenseExpire) }}" placeholder="Enter license Expire Date">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Equipment Type</label>
<div class="col-md-6">
<input type="email" class="form-control" name="equipmentType" value="{{ ($device->equipmentType) }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Equipment Status</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->equipmentStatus) }}" name="equipmentStatus" >
<option value="0">In Service</option>
<option value="#">Rented</option>
<option value="#">Pending</option>
<option value="#">Completed</option>
<option value="#">Available</option>
<option value="#">Unavailable</option>
<option value="#">Repair</option>
<option value="#">Retired</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">IMEI/EDN Number</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->imeiNumber) }}" placeholder="Enter IMEI/EDN Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Serial Number</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->serialNumber) }}" placeholder="Enter Serial Number">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Data Key</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->notifyEmail) }}" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">SIM Phone</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->simPhoneNumber) }}" placeholder="Enter SIM Phone Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">SMS Email Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->smsEmail) }}" placeholder="Enter SMS E-Mail Address">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Group Pushpin ID</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ ($device->notifyEmail) }}" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">Map Route Color</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->timeZone) }}" name="timeZone" >
<option value="0">Black</option>
<option value="#">Brown</option>
<option value="#">Red</option>
<option value="#">Orange</option>
<option value="#">Green</option>
<option value="#">Blue</option>
<option value="#">Purple</option>
<option value="#">Grey</option>
<option value="#">Cyan</option>
<option value="#">Pink</option>
<option value="#">None</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Fuel Capacity</label>
<div class="col-md-6">
<input type="email" class="form-control" name="fuelCapacity" value="{{ ($device->fuelCapacity) }}" >
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Driver ID</label>
<div class="col-md-6">
<input type="email" class="form-control" name="driverID" value="{{ ($device->driverID) }}">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Reported Odometer</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ old('notifyEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Reported Engine Hours</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="{{ old('notifyEmail') }}" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Save
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Routes.php
Route::any('vehicleAdmin', 'VehicleController#getIndex');
Route::post('vehicleAdmin', 'VehicleController#vehicleInsert');
Route::get('vehicle/edit/{id}', 'VehicleController#edit');
Route::delete('vehicle/delete/{id}', 'VehicleController#delete');
I think when you click the link, it is probably sending a GET request to that end point unless you set the method to delete in ajax call,. CRUD in Laravel works according to REST. This means it is expecting a DELETE request instead of GET.
So I would suggest you to make your route as follow
Route::get('/vehicle/delete/{id}', 'VehicleController#delete');

Categories