i have a many to many realtionship and a pivot table now i want to update the pivot table data with a form that users sends. here for example is assign a client to a sellman . here is my code :
in route :
Route::get('admin/client/assign','ClientController#assignsellman');
controller :
public function assignsellman(Request $request){
$user = User::all();
$client_list = Client::all();
$client = Client::with('sellmanlist')->firstOrFail();
$sellman = $request->input('sellman');
$client->sellmanlist()->attach($sellman);
return view('admin.client.assign',compact('client_list','user'));
}
and finally here is the form of view file that i want to get 2 variables one the id of the client and the secound the id of sell man
<form action="/admin/client/" method="post">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="row">
<div class="col-xs-4">
<div class="form-group">
<label for="client">مشتری</label>
<select class="select-search select2-hidden-accessible" tabindex="-1" aria-hidden="true"
name="client">
#foreach($client_list as $client_lists)
<option value="">{{$client_lists->title}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-xs-4 text-center">
<i class="icon-arrow-left7 mr-3 icon-3x" style="font-size: 130px"></i>
<h4>ارجاع به</h4>
</div>
<div class="col-xs-4">
<div class="form-group">
<div class="form-group">
<label for="sellman">کارشناس فروش</label>
<select class="select-search select2-hidden-accessible" tabindex="-1"
aria-hidden="true" name="sellman">
#foreach($user as $users)
<option value="1">{{$users->name}}</option>
#endforeach
</select>
</div>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button type="submit" class="btn btn-primary">تایید</button>
</form>
with this code i get this error
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
thanks for help
Edit your route
Route::post('admin/client/','ClientController#assignsellman');
That is because the route you've created is HTTP GET, and in your form you're using HTTP Post.
<form action="/admin/client/" method="post">
Try switching to a GET method and it should work
<form action="/admin/client/" method="get">
or switch your route to
Route::post('admin/client/assign','ClientController#assignsellman');
Please take a look at the different HTTP Verbs and apply them to your needs.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Related
I'm getting an error when I try to pass data from the controller to the view and I spent a lot of time trying to solve it. The error is "Undefined Variable $instEntNacs". There is other form this view, because depending on the rol, it will show the first or the second one.
Here's the controller "ConvenioController":
public function create()
{
$instEntNacs = DB::table('inst_ent_nacs')->select('id', 'nombre')->get();
return view('convenios.create', ['instEntNacs' => $instEntNacs]);
}
And the view:
#if (auth()->user()->rol_id == "2")
<form method="POST" class="form-conv-nac border border-2 rounded-3 shadow-lg" action="{{ route('convenios.store_nac') }}" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="offset-1 col-10 mt-4">
<select class="form-control border border-dark" id="con_instEntNac" name="con_instEntNac">
<option selected value="">-- Institución o Entidad --</option>
#foreach ($instEntNacs as $item)
<option value="{{ $item->id }}"> {{ $item->nombre }}</option>
#endforeach
</select>
</div>
</div>
<div class="row mt-4 mb-5">
<div class="offset-1 col-2">
Regresar
</div>
<div class="offset-2 col-3">
<button type="submit" class="w-100 btn_2 btn-danger rounded-pill border border-dark">Cancelar</button>
</div>
<div class="col-3">
<button type="submit" class="w-100 btn_1 btn-primary rounded-pill border border-dark">Registrar</button>
</div>
</div>
</form>
#endif
Have you tried this?
$data['instEntNacs'] = DB::table('inst_ent_nacs')->select('id', 'nombre')->get();
return view('convenios.create')->with($data);
You can try compact('instEntNacs') with return view('convenios.create').
return view('convenios.create', compact('istEntNacs'));
i am creating crud in laravel.i ran into the problem with Missing required parameters for [Route: employees.update] [URI: employees/{employee}]. (View: C:\xampp\htdocs\jbs\resources\views\employees\edit.blade.php)what i tried so far i attach below. i added the model view controller below
Edit Blade Page
#extends('employees.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Employee</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('employees.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<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 action="{{ route('employees.update',$employees->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>StudentName:</strong>
<input type="text" name="name" value="{{ $employees->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="{{ $employees->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="{{ $employees->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>
#endsection
Controller
public function edit(Employee $employees)
{
return view('employees.edit',compact('employees'));
}
public function update(Request $request, Employee $employees)
{
$request->validate([
'studname' => 'required',
'course' => 'required',
'fee' => 'required',
]);
$employees->update($request->all());
return redirect()->route('employees.index')
->with('success','Employee updated successfully');
}
Model
protected $fillable = [
'studname', 'course','fee',
];
}
routes
Route::resource('employees','App\Http\Controllers\EmployeeController');
Your route parameter is employee not employees. Route parameter names for resource routes are singular by default. The route is actually like this:
employees/{employee}
So in your edit method of your Controller you are using the wrong parameter name so you are getting a new instance of Employee instead of the Implicit Route Model Binding that would inject the instance based on the route parameter, you need to match the typehinted parameter name to the route parameter name:
// {employee}
public function edit(Employee $employee)
{
...
return view(...., ['employee' => $employee]);
}
Now in the view you will have your actual existing Employee instance that you can use to generate the URL to the route instead of an empty Employee that does not have an id which was returning null:
{{ route('employees.update', ['employee' => $employee->id]) }}
// or
{{ route('employees.update', ['employee' => $employee]) }}
The route system can get the correct key from the model instance.
You should pass $employees->id as a hidden input field.
Make your route as
Route::post('employee/update', 'YourController#yourFunction');
and in the edit page the form action should look like
<form action="{{ route('employees.update'}}" method="POST">
make a hidden input field for passing id
<input type="hidden" name="id" value="{{$employees->id}}"></input>
You should pass the route name to the route function like this
route('route name',parameter1)
Example :
Route::post('employee/update/{id}' ,YourController#update)->name('update_employee');
<form action="{{ route('update_employee',$employees->id) }}" method="POST">
#csrf
...
i am new to laravel..Kind of stuck at this place. Tried many solutions for this but none worked yet, There are similar question but most unresolved, or proper evident solution not posted yet(from google,stackoverflow ..etc)
i have defned a custom route
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
whenever i submit the view with 'POST' method as
<form action="/ComplaintGenerate" method="POST" >
without any validation rule in my Complaintscontroller everything works fine and i can save data. but when i put validation either through Requests or direct it throws error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if i remove validation everything works fine. I also tried with GET method but still dint work.
A little peace of advice will be very much appreciated.
Web.route
Route::middleware(['auth'])->group(function(){
Route::post('/Complaint', 'ComplaintsController#find');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');});
My controller:
public function generate(GenerateInitialComplaintRequest $request)
{
$complaint = Complaint::find($request->complaint_id);
$complaint->update([
'complaint_date'=>$request->complaint_date,
'complaint_description'=>$request->complaint_description,
]);
return redirect(route('Complaints.index')->with('complaint', Complaint::all()));
}
my View:
<div class="container my-5">
<div class="col d-flex justify-content-center my-4">
<div class="card">
<div class="card-header">
<form action="/ComplaintGenerate" method="POST" >
#csrf
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
What is the route for displaying your form? When validation fails, Laravel makes redirection using GET method to the route it was displayed from.
I assume the form might be displayed in the find method of your ComplaintsController, and when validation fails, there's redirection to this route and that is what throws an error.
Can you also show your validation methods and what data are you trying to send through form?
i found the solution as mentioned by Ankur Mishra and Aryal,
We have to remember as mentioned by Aryal When validation fails, Laravel makes redirection using GET method to the route it was displayed from. And i displayed my form through below
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Controller method:
public function save_customer($id)
{
$complaint = Complaint::create([
'customer_id'=>$id
]);
// $complaint = Complaint::whereCustomer_id($id)->firstorfail();
return view('complaints.initial_complaint')->with('complaint', $complaint);
}
'complaints.initial_complaint' is the view which has the form which gave me the error of
The GET method is not supported for this route. Supported methods: POST. on submission
So i change POST route to GET :-
Route::middleware(['auth'])->group(function(){
//Route::resource('Complaints', 'ComplaintsController');
Route::get('/Complaint', 'ComplaintsController#find');
Route::get('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');
});
and in view i passed GET as hidden method
<form action="/ComplaintGenerate" method="POST" >
#csrf
#method('GET')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
and now it is working me.. Just posted so if anybody could use it for future reference
you should add
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
Hello I know its simple but I am unable to do it.
I have #foreach when I have list with my users,(it works) but I need to send the selected user id to another view, yet I always see the id of last user in my database. How I can save the data from loop? by $_POST?
<div class="container mt mb-4">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h2>Edit </h2>
<form method="POST" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div>
<select name="name" id="">
#foreach($user as $us)
<option value="{{$us->id}}" >{{$us->name}} {{$us->lastname}}</option>
#endforeach
</select>
</div>
Edit
</form>
</div>
Now I have in my form, the id of last user but I need the id of the user which is selected.
Thanks.
You are accessing $us object outside of foreach loop, that's why it getting only last userId, add link inside foreach loop.
<div class="container mt mb-4">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h2>Edit </h2>
<form method="POST" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
#foreach($user as $us)
$option = '<option value="{{$us->id}}" >{{$us->name}} {{$us->lastname}}
</option>';
Edit
#endforeach
<div>
<select name="name" id="">
{!! $option !!}
</select>
</div>
</form>
</div>
When you use '$us->id' outside your foreach, it will give you the value of the last user as that line will be executed after the loop is ended and last value will be stored in that variable. That was the reason u were getting last user's id.
<div class="container mt mb-4">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h2>Edit </h2>
<form method="POST" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div>
<select name="name" id="">
#foreach($user as $us)
<option value="{{$us->id}}" >{{$us->name}} {{$us->lastname}}</option>
Edit
#endforeach
</select>
</div>
</form>
Your link is outside of the loop. When the loop finishes the variable $us will stay with the value of the last user. You need to pass the link to inside of the loop as pointed out in the comments:
<select name="name" id="mySelect">
#foreach($user as $us)
<option value="{{$us->id}}" >
{{$us->name}} {{$us->lastname}}
</option>
#endforeach
</select>
But this approach doesn't make much sense (inserting a link inside of a <select> seems... well... weird). I guess you didn't formulate your question correctly and I will assume you would like to show the links to edit every user so you would need to do:
#foreach($user as $us)
Edit
#endforeach
UPDATE
After OP commented in this answer I understood what he would like to do.
He would like to have an Edit link whenever he clicks on the link and be redirected to the page to edit a certain user.
I'm not sure if there is a pure HTML way to accomplish your goal (at least thinking right now) so you would need to use a little bit of Javascript magic.
You need to listen to changes in select and update the link whenever there is a change:
<select name="name" id="mySelect">
#foreach($user as $us)
<option value="{{$us->id}}" >
{{$us->name}} {{$us->lastname}}
</option>
#endforeach
</select>
Edit
<script>
var baseLink = {{ URL::to('admin/paneladministratora/editUser/') }}
var mySelect;
window.onload = function() {
mySelect = document.getElementById("mySelect");
mySelect.addEventListener("change", updateLink);
}
function updateLink(){
document.getElementById("editLink").href = baseLink + mySelect.options[mySelect.selectedIndex].value
}
</script>
Not tested right now but you get the idea
You can achieve what you described in a number of ways, but the best in my opinion, with the least changes to your code, is to submit the form to a URL and retrieve the user ID from the form data, in your controller.
Try this:
<div class="container mt mb-4">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h2>Edit </h2>
<form method="POST" role="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div>
<select name="name" id="">
#foreach($user as $us)
<option value="{{$us->id}}" >{{$us->name}} {{$us->lastname}}</option>
#endforeach
</select>
</div>
<button type="submit"> Edit </button>
</form>
</div>
Hellow, finally i made it.
<h2>Edit </h2>
<form method="POST" role="form" >
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div>
<select name="name" id="test" onchange="javascript:doUrl(event)">
#foreach($user as $us1)
<option value="{{$us1->id}}">{{$us1->name}}</option>
#endforeach
</select>
</div>
</form>
<a id="linkEdit" href="{{ URL::to('admin/paneladministratora/editUser/'. $us1->id) }}">Edytuj3 </a>
</div>
<script>
function doUrl(e){
$('#linkEdit').attr('href','/admin/paneladministratora/editUser/'+e.target.value)
}
</script>
I'm trying to create a situation where a user can load a page, select a timesheet number, and then have the system delete all records associated with that timesheet number. This function loads the form page:
public function deletetimesheetLoad()
{
$timesheets = collect(DB::select("SELECT dbo.TIME.Source AS Source
FROM dbo.TIME
GROUP BY dbo.TIME.Source
ORDER BY dbo.TIME.Source ASC"));
return view('utilities/deletetimesheet',['timesheets' => $timesheets]);
}
This is the actual blade template:
<form method="post"
action="{{url('/time/deletetimesheet/process')}}"
enctype="multipart/form-data"
<div class="form-group">
<label class="col-md-12 control-label" for="TIMESHEETNUMBER">Timesheet Number</label>
<div class="col-md-12">
<select required id="TIMESHEETNUMBER" name="TIMESHEETNUMBER" class="form-control select2_field">
<option value=""></option>
#foreach ($timesheets as $row)
<option value="{{ $row->Source }}">{{ $row->Source }}</option>
#endforeach
</select>
</div>
</div>
<div id="saveActions" class="form-group">
<input type="hidden" name="save_action" value="Submit">
<div class="btn-group">
<button type="submit" class="btn btn-success">
<span class="fa fa-save"></span>
<span data-value="Submit">Submit</span>
</button>
</div>
<span class="fa fa-ban"></span> Cancel
</div>
This is the function that the form action is pointing to:
public function deletetimesheetProcess(TimesheetRequest $request)
{
DB::table('TIME')->where('Source', $request->get('TIMESHEETNUMBER'))->delete();
\Alert::success(trans('yay'))->flash();
return view('details/customershow',[]);
}
Here are the defined routes for the two functions:
Route::get('/time/deletetimesheet', 'Admin\TimeCrudController#deletetimesheetLoad');
Route::post('/time/deletetimesheet/process', 'Admin\TimeCrudController#deletetimesheetProcess');
Currently the blade template loads correctly, and does not throw an error on submit - just reloads the current page. What am I doing wrong?