My EventsController function is this in My Laravel app
public function storetask(Request $request, $id, Task $task)
{
$task = new Task;
$task->task_name = $request->input('name');
$task->body = $request->input('body');
$task->assign = $request->input('status');
$task->priority = $request->input('status');
$task->duedate = $request->input('date');
$task->project_id = $id;
$task->save();
}
I need save form data in My events folder show.blade.php file using above controller function
My form route is this
<form method="post" action="{{ route('events.storetask') }}">
My route.php is this
Route::resource('events', 'EventsController');
but I got this error message
Route [events.storetask] not defined. (View: C:\Users\Flex\Desktop\kuruja\resources\views\events\show.blade.php)
how can solve this?
Define your route like below:
Route::post('/storeTask/{projectId}',array('uses'=>'EventsController#storetask','as'=>'storeTask'));
And call in your view
<form method="post" action="{{ route('storeTask',$yourProjectId) }}">
Replace your controller function with the below function:
public function storetask($id=0)
{
$task = new Task();
$task->task_name = \Input::get('name');
$task->body = \Input::get('body');
$task->assign = \Input::get('status');
$task->priority = \Input::get('status');
$task->duedate = \Input::get('date');
$task->project_id = $id;
$task->save();
}
You need a route for storetask. When you use resource it creates 7 routes you should have on your Events controller. index, create, store, show, edit, update, destroy. You could just direct your form to store or create storetask instead.
You can also use php artisan routes command to view all your routes and you can see there isn't one named storetask.
I think the issue is here:
<form method="post" action="{{ route('events.storetask') }}">
As when you register a resourceful route to the controller like:
Route::resource('photo', 'PhotoController');
This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have methods stubbed for each of these actions, including notes informing you which URIs and verbs they handle.
Actions Handled By Resource Controller
Verb Path Action Route Name
GET /photo index photo.index
GET /photo/create create photo.create
POST /photo store photo.store
GET /photo/{photo} show photo.show
GET /photo/{photo}/edit edit photo.edit
PUT/PATCH /photo/{photo} update photo.update
DELETE /photo/{photo} destroy photo.destroy
but in your case the the method storetask do not match with the above list. So change the name according to the list and try again.
Reference
Related
**i am confused with routes and how to specify the path . **
web.php- this route is inside my module called events
<?php
use Illuminate\Support\Facades\Route;
Route::prefix('event')->group(function() {
Route::get('/create', 'EventController#index');
});
Route::post('/create', 'EventController#store');
blade file-i have a form which calls create
<form class="form-horizontal" action="../create" enctype="multipart/form-data" method="post">
**controller-here in store method i store the get thevalues fromthe user and storeitin the db **
function store(StoreCompanyRequest $req)
{
//
$req->validate([
'name'=>'required',
'title'=>'required',
'description'=>'required',
'category'=>'required',
'sdate'=>'required',
'edate'=>'required',
'address_address'=>'required',
'address_latitude'=>'required',
'address_longitude'=>'required',
'images' => 'required',
'images.*' => 'mimes:jpeg,jpg,png,gif,csv,txt,pdf|max:2048'
]);
abort_unless(\Gate::allows('company_create'), 403);
if($req->hasfile('images')) {
foreach($req->file('images') as $file)
{
$image_name = $file->getClientOriginalName();
$file->move(public_path().'/uploads/', $image_name);
$imgData[] = $image_name;
}
$event = new Event;
$event->name=$req->name;
$event->save();
return view('/home');
}
}
Try to use action="/create" in your form like so:
<form class="form-horizontal" action="/create" enctype="multipart/form-data" method="post">
I don't work with PHP and have no prior experience with Laravel but I can see that you are creating a route for the POST /create endpoint. With your current form object you reference the route action with a relative URL. So, if the form is on a page under the /path/to/form route, then the form submission results in a call to the POST /path/to/create route instead of the (maybe) intended POST /create route.
In this app I have a User table, and a Posts table.
In the individual post blade (post/{id}) I have a button called "Pick Up Task". When clicked, it should set $post->user to the current user's ID. I have set up everything as per the current documentation, yet it still gives me the error: "The POST method is not supported for this route. Supported methods: GET, HEAD."
Here is my code:
Button in Post Blade
<form action="update" method="POST">
#csrf
<input type="hidden" name="user_id" value="{{Auth::user()->id}}"/>
<input type="hidden" name="task_id" value="{{ $task->id }}"/>
<button class="btn btn-outline-secondary" type="submit" value= "UPDATE" href="#">➕ Pick up Task</button>
</form>
Function in the Post Controller
// ADD USER TO TASK
public function update()
{
$user = request('user_id');
$task = request('task_id');
//Fill Post User
$selectedtask = Posts::findOrFail($task);
$selectedtask->update(['user', $user]);
//Success
Session::flash('success', 'You picked up a new task.');
return view('/');
}
My Routes (web.php)
//Add user as assignee
Route::POST('update', 'PostController#update');
try move
Route::POST('update', 'PostController#update');
to top of web.php file, then clear route cache, change code
<form action="/update" method="POST">
(add slash before update) and try your code
You're being sent to the wrong URI.
Change your form tag to this:
<form action="" method="POST">
This will ensure that you are being sent to the exact same route you're currently on, but with a POST method instead of the GET method.
Your routes should look like this:
Route::get('post/{post}', 'PostController#show'); // For viewing
Route::post('post/{post}', 'PostController#update'); // For updating
Correct your update controller method to work with the parameters via model binding:
public function update(Request $request, Post $post)
{
$user = request('user_id');
$task = request('task_id');
//Fill Post User
$selectedtask = Posts::findOrFail($task);
$selectedtask->update(['user', $user]);
//Success
Session::flash('success', 'You picked up a new task.');
return view('/');
}
I'm also assuming that you're mixing up task_id with the ID of a Post model, since you're fetching a post via that request variable? You can, by using model binding and accessing the user via the Request object, boil down your controller method to the following:
public function update(Request $request, Post $post)
{
$post->update(['user' => $request->user()->id]);
return redirect('/')->with('success', 'You picked up a new task.');
}
Model binding will automatically return a 404 if the doesn't exist.
working with Laravel 5.6 and Mysql. and need delete table data using following data.
<td><a class="button is-outlined" href="/student/{{$student->id}}/delete">Delete</a></td>
and Controller delete function is,
public function delete($id)
{
DB::table('students')
->where('id', $id)
->delete();
return redirect()->back();
}
and route is like this,
Route::resource('student','StudentController');
but when click delete button it is generated following error message,
(1/1) NotFoundHttpException
how can fix this problem?
If you use resource controller, you can't generate a link for the DELETE method.
By the way, it's not delete method, but destroy method and link.
The DELETE method expects the request to have DELETE header (like POST, GET or PUT).
The simplest way is to define an URL for you delete method :
Route::get('student/{site}/delete', ['as' => 'student.delete', 'uses' => 'StudentController#delete']);
Or you must use a form like this to call DELETE header :
<form action="{{ route('student.destroy', $studentId) }}" method="POST">
#method('DELETE')
#csrf
<button>Delete</button>
</form>
And you need to change the name of your method :
public function destroy($id)
If you are using Resource Controller you can try this. You can try to DELETE using AJAX Call too.
<td><a class="button is-outlined" id="delete-record">Delete</a></td>
DELETE verb will automatically call destroy action from your Resource Controller.
public function destroy($id)
{
$deleted = DB::table('students')
->where('id', $id)
->delete();
// return number of deleted records
return $deleted;
}
And to perform DELETE use AJAX call like this.
$('#delete-record').click(function(){
$.ajax({
url: '/student/'+{{$student->id}},
type: 'DELETE', // user.destroy
success: function(result) {
console.log("Success");
console.log("No. Of Deleted Records = "+result);
}
});
});
I hope this solution will also work like above one that is already provided.
Within a Laravel package I made, I want to redirect the user to a controller action that requires a parameter (within the same package).
Controller:
public function postMatchItem(Request $request, $id)
{
$this->validate($request, [
'item_match' => 'required|numeric|exists:item,id',
]);
$spot_buy_item = SpotBuyItem::find($id);
$item = Item::find($request->input('item_match'));
$price = $item->getPrice();
$spot_buy_item_response = new SpotBuyItemResponse();
$spot_buy_item_response->spot_buy_item_id = $id;
$spot_buy_item_response->spot_buy_id = $spot_buy_item->spot_buy_id;
$spot_buy_item_response->item_id = $item->id;
$spot_buy_item_response->user_id = $spot_buy_item->user_id;
$spot_buy_item_response->spot_buy_price = $price;
$spot_buy_item_response->created_ts = Carbon::now();
$spot_buy_item_response->save();
return redirect()->action('Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart', [$id]);
}
The action in the redirect is the same path I use in my routes.php file to direct the user to this controller action
Route:
Route::get('/part/{id}', 'Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart')->where('id', '[0-9]+');
I've tried variations of this path without success, including SpotBuyController#getPart like the documentation suggests (https://laravel.com/docs/5.1/responses#redirects)
Note: I got this to work by naming my route in routes.php and using return redirect()->route('route_name', [$id]);, but I still want to know how to pass a package controller action to the ->action() function.
It's trying to access your controller from within the App\Http\Controllers namespace. Can see they've added it to your controller name in your error:
App\Http\Controllers\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart
You need to escape the Ariel namespace with a \ at the start:
return redirect()->action('\Ariel\SpotBuy\Http\Controllers\Admin\SpotBuyController#getPart', [$id]);
Aloha, I'm making a workout manager in which you have a dashboard displaying your 5 last workouts. I have set a form for each one workout for allowing the user to delete any of them. Here the form in the dashboard:
{!! Form::open(['route' => ['dashboard.workout.destroy', $workout->id], 'style' =>'display:inline-block;', 'method' => 'DELETE']) !!}
This route will call this method in WorkoutController.php
public function destroy($id, Request $request)
{
$workout = Workout::findOrFail($id);
$workout->delete();
$message = "Workout deleted successfully!";
return redirect()->route('dashboard.index', ['message' => $message]);
}
And this route will call this method in DashboardController.php
public function index($message = null)
{
$user = Auth::user();
// Workouts
...
// Inbodies
...
// Measures
...
return view('dashboard.index', compact('user','workoutsDesc','workouts','lastInbody','inbodies','measures','lastMeasure','message'));
}
The question is that I'm trying to pass the variable $message from WorkoutController to DashboardController for displaying a successfull alert after deleting a workout, but I don't know how to do it. I have tried with:
return redirect()->action('Dashboard\DashboardController#index', [$message]);
return redirect()->action('Dashboard\DashboardController#index')->with('message', $message);
return redirect()->route('dashboard.index', $message);
But I still trying to find the way for doing it.
First of all, from Laravel 5.1 Documentation:
If your route has parameters, you may pass them as the second argument to the route method
As the message is not a parameter to your route, so you can't pass that. A possible solution can be Flashing data. Check the next controller if the session has that key and contain a value, then add it to a variable and pass to the view.
Hope this works.