Passing parameter from route to controller in laravel - laravel-5

I am trying to pass a variable from route to controller. But not able to succeed.
my route entry is as below
Route::get('/Register', 'NewRegister#CheckCand');
now in the controller file I want to get one parameter. My controller function is as below
public function CheckCand()
{
echo $ID;
}
Now how do I pass a variable ID from route to controller. But i don't want to pass it in the URL and also in get function i dont want to change '/Register'.
Route::get('/Register', 'NewRegister#CheckCand');
Means it will be like a Hidden parameter passed from route to controller.
Might be question is confusing but i don't know how to explain better.

You can retrieve the parameters by Request so in your function request the parameters as you named when you passed them
public function CheckCand(){
$request = Request::all();
$ID = $request->ID ;
// Or
$ID = $request['ID'];
}

Pass as request parameter and retrieve it in the controller using request. Something like below.
jQuery
$.get('/register', {ID :'123'}, function(data) {});
or
Javascript
...
<form id="register-form" action="{{ url('register') }}" method="GET" style="display: none;"><input type="hidden" name="ID" value="..."/></form>
Controller
public function CheckCand(Request $request) {
$request->ID;
}

Related

Laravel missing parameters to update form in controller

So I have a little project, in it, there's the possibility to upload banners images to be shown on the main page. All the stuff related to the DB are already created and the option to create a banner is working, it creates the banner and then stores the image on the DB for use later. Now I'm trying to work on an edit function so I can change the description under the bannners. I have an Edit route in my controller which returns a view where I edit said banner then it calls the update function on the controller. But no matter what I put here, I'm always getting the Missing Required Parameters error once I try to Save the edit and open my controller through the Update function. Here's the code as it is now:
The route definition:
Route::resource('banner', 'BannerController');
The edit function on my controller:
public function edit($id)
{
return view('admin/edit-banners',['id'=>$id]);
}
The update function has not been implemented because I always start with a dd() function to check if everything is working fine:
public function update(Request $request, $id)
{
dd($request);
}
And here's the form line in my edit view that is trying to call the update route:
<form class="card-box" action="{{ route('banner.update',[$banner]) }}">
I also added this at the beginning of the view to store the data from the DB into a variable:
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
The $banner variable contains all the information on the banner being edited, and I can get the new description at the controller with the $request variable, so I honestly don't know what should I put here as parameters, any ideas?
The $banner variable is not a Model instance, it is a Collection.
Adjust your controller to pass this to the view instead of dong the query in the view:
public function edit($id)
{
$banner = Banner::findOrFail($id);
return view('admin.edit-banners', ['banner' => $banner]);
}
You could also use Route Model Binding here instead of doing the query yourself.
Remove that #php block from your view.
The form should be adjusted to use method POST and spoof the method PUT or PATCH (as the update route is a PUT or PATCH route) and you should adjust the call to route:
<form class="card-box" action="{{ route('banner.update', ['banner' => $banner]) }}" method="POST">
#method('PUT')
If you include $id in your function declaration then when you call the route helper it expects you to give it an id parameter. Try with
<form class="card-box" action="{{ route('banner.update',['id' => $id]) }}">
You should be able to retrieve the form data just fine form the $request variable. More info here.
The code below should be the error source. $banner variable then is an array but the update function accept object or id.
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
You should try to replay this code by this one...
#php
use\App\Banner;
$banner = Banner::find($id);
//you should put a dd here to view the contain of $banner if you like
#endphp
Hop it help...

Too few arguments to function App\Http\Controllers\SupervisorController::edit(), 0 passed and exactly 1 expected

Sorry this is very basic, but i dont know what is the problem.
I always get this error when trying to visit edit_supervisor.
Route:
Route::get('/edit_supervisor', 'SupervisorController#edit')->name('edit_supervisor');
SupervisorController:
public function edit($id)
{
return view('DataSupervisor.edit');
}
you can add $id as a parameter like this in Route
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
or you can change the function parameter $id to Request $request
public function edit(Request $request)
{
return view('DataSupervisor.edit');
}
in blade
edit
for showing previous data you need to get data using $id in controller
$supervisor = Model::findOrFail($id);
return view('DataSupervisor.edit', compact('supervisor'));
You need to pass the id :
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
On blade :
<a href="{{ route('edit_supervisor', $id) }}...

BadMethodCallException Method App\Http\Controllers\TicketsController::route does not exist

am trying to store an ticket using store function in tickets Controller
// Create Ticket
$ticket=new Ticket;
$ticket->userName= $request->input('userName');
$ticket->userEmail= $request->input('userEmail');
$ticket->phoneNumber= $request->input('phoneNumber');
$ticket->regular_quantity= $request->input('regular_quantity');
$ticket->vip_quantity= $request->input('vip_quantity');
$ticket->event_id = $this->route('id');
$ticket->save();
return redirect('/');
}
This is the route
Route::post('ticketstore', 'TicketsController#store')->name('ticketstore');
The form action
<form action="{{route('ticketstore')}}" method="POST">
#csrf
am getting that error
So change this
$this->route('id');
with
$request->route('id');
calling it on this works within FormRequest.
--- EDIT
Now you are trying to get the ID of the event through the request but you are not passing it:
Route::post('ticketstore/{event}', 'TicketsController#store')->name('ticketstore');
Then in your route you should pass the event:
{{route('ticketstore', $event)}}
and you can get it using $request->route('event') or in the method signature like so:
public function store(Request $request, Event $event)
{
...
$ticket->event_id = $event->id;
...
}
Or if you have a dropdown with events in your view just get the event ID from the request $request->event_id;

Redirect from controller to named route with data in laravel

I'm gonna try to explain my problem:
I have a named route called 'form.index' where I show a html form.
In FormController I retrieve all form data.
After do some stuff with these data, I want to redirect to another named route 'form.matches' with some items collection.
URLS
form.index -> websiteexample/form
form.matches -> websiteexample/matches
FormController
public function match(FormularioRequest $request)
{
// Some stuffs
$list = /*Collection*/;
return redirect()->route('form.matches')->with(compact('list'));
}
public function matches()
{
// How to retrieve $list var here?
return view('form.views.matches')->with(compact('list'));
}
The problem:
When the redirects of match function occurs, I get an error "Undefined variable: list' in matches funcion.
public function match(Request $request)
{
// Operations
$list = //Data Collection;
return redirect()->route('form.matches')->with('list',$list);
}
In view
#if(Session::has('list'))
<div>
{!!Session::get('list')!!}
</div>
#endif
You can use Redirect::route() to redirect to a named route and pass an array of parameters as the second argument
Redirect::route('route.name',array('param1' => $param1,'param2' => $param2));
Hope this helps you.

Laravel 5.6 Function () does not exist in route/web.php

This is my code using to send an email
Route::post('/mail/send', [
'EmailController#send',
]);
in EmailController this is the send action
public function send(Request $request)
{
$data = $request->all();
$data['email'] = Input::get('email');
$data['name'] = Input::get('name');
$obj = new \stdClass();
$obj->attr = 'Hello';
Mail::to("dev#mail.com")->send(new WelcomeEmail($obj));
}
getting a error as Function () does not exist
In your route/web.php file
Change it to
Route::post('/mail/send', 'EmailController#send');
Refer to the documentation to see the possible options to define routes:
https://laravel.com/docs/5.6/routing
Route's action method can be defined using a array, but not simply wrap controller#action in an array, you should assign it to array's key 'uses'.
In your example, it should be like this:
Route::post('/mail/send', [
'uses' => 'EmailController#send',
//'middleware' => .... assign a middleware to this route, if needed
]);
the array form usually is used when we want to specify more specification about the route like use a specific middleware and pass middleware parameters.
if you just want to define route's processing method you can simply use controller#action as Route::post's second parameter:
Route::post('/mail/send','EmailController#send');
In your route ...
Route::post('/mail/send','EmailController#send')->name('send_email');
Inside of your HTML form add below code...
<form action="{{route('send_email')}}" method="post">
...
{{csrf_field()}}

Resources