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

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;

Related

On Form Submit Inertia is not redirecting to particular page

I'm using Inertia (Vue3 & Laravel 9). I have a form in "Register.vue" Component.
On submitting that form I'm calling a controller to process the request. Once the controller process the request I want the controller to redirect to an other component i.e. regComplete (where I want to show data which I received as a prop from controller).
Now the thing is the Controller is redirecting me to the desired page (Although I'm unable to get the prop data but I'm getting the other data successfully) but the URL is still same as it was on form submit.
"Register.vue"
<template>
<form #submit.prevent="submit">Here are the form fields i.e. email & password </form>
</template>
<script setup>
let form = reactive({
email: "",
password: "",
});
let submit = () =>{
Inertia.post('users',form);
}
</script>
Route.php
//Route on submitting the form
Route::post('users',[userController::class,'register']);
Controller = userController
public function register(Request $request){
// $email = $request->input('email');
// $password = $request->input('password');
// return "User with".$email." and with password ".$password." is created";
return Inertia::render('regComplete');}
Now my question is How to redirect to the settings page with desired props ?
for example return Inertia::render('regComplete',['msg'=>'User registerd']);
After successfully creating a new user, you return the Component rather than returning or redirecting to another route where the component (regComplete) is being loaded.
What you can do is add more routes that deal with the (regComplete) component.
On routes.php, add the new route /registration/complete
Route::post('users',[UserController::class,'register']);
Route::get('/registration/complete',[UserController::class,'regComplete']);
On UserController.php, add new function regComplete() and update register
// add the this function
public function regComplete () {
return Inertia::render('regComplete',[
'users' => User::all() // make sure you have defineProps()
]);
}
// update your register function
public function register(Request $request){
// creation of user process here
if(successfully created) {
return redirect('/registration/complete');
}
return back()->with('error','You error message here');
}
It is possible that it will not solve your problem. However, hopefully it will assist you in determining where the problem may be occurring.

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...

How do I pass a variable from blade file to controller in laravel?

I have ProjectController that fetches data from the database and passes it to a blade file. One of the data items is the project_id. I want to pass the project _id from the blade file to another controller BidController.
ProjectController.php
public function show($id)
{
$project = Project::find($id);
return view('project.show',['project'=>$project]);
}
show.blade.php
div class="card-header">PROJECT <p>{!! $project->id !!}</p></div>
BidController.php
public function store(Request $request)
{
$bid = new Bid;
$bid->project_id = $project_id;
dd($project_id);
}
The dd(); does not output the project_id. I need help in passing the project_id from the blade file to the BidController method.
You can't directly set a model's id like you're doing in the line $bid->id = $project_id;. Are you trying to set up a relationship? That should be more like $bid->project_id = $request->project_id;.
Blade templates can't really pass things back to controllers, once they're in the browser your app is sort-of finished running. You need to create an HTML link/request on the page (like a form post request) that'll request the next thing from your app when the user clicks it.
If you want to create a button that creates a new bid for an existing project, you could do something like set up a form with a hidden field of 'project_id' that posts back to '/bids' which goes to the route 'bids.store'. You'll find 'project_id' under $request->project-id'.
You can send an AJAX request from Javascript:
View
<script type="text/javascript">
var project_id= {!! json_encode($project->id) !!}
$.ajax({
type: 'POST',
url: url, //Your bidController route
data: {project_id: project_id},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown)
},
success: function()
{
console.log('successful')
}
});
</script>
This will sent the data to the controller asynchronously so the user experience doesn't get affected.
One extra point: In your Bid controller, as project_id is coming from the request, you'll have to use:
$bid->id = $request->project_id;
I hope it helps!
PS: I'm using JQuery for this, so you'll have to include it if you don't already have.
I think this will solve your problem :
ProjectController.php
public function show($id)
{
$project = Project::findOrFail($id);
return view('project.show',compact('project');
}
web.php
Route::post('/bids/store/{id}' , 'BidController#store')->name('bids.store');
show.blade.php
div class="card-header">PROJECT <p>{{$project->id}}</p></div>
<form action="{{route('bids.store', $project->id)}}" method="post">
BidController.php
public function store(Request $request, $id)
{
$bid = new Bid;
$bid->id = $id;
$bid->save();
dd($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.

Passing parameter from route to controller in laravel

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

Resources