Page Not Found in Laravel - php

Code in my Route file look like this :
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
Route::get('/home', 'HomeController#index');
Route::get('/redirect/{provider}', 'SocialAuthController#redirect');
Route::get('/callback/{provider}', 'SocialAuthController#callback');
});
Code in view file:
<form action="/subtask1/delete/{{1}}" method="POST" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" id="delete-task-{{$subtask->id }}" class="btn btn-danger btn-xs">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
And code on the Controller:
public function subtaskdestroy(Request $request, Subtask $subtask)
{
$this->authorize('checkTaskOwner', $subtask);
$subtask->delete();
return redirect('/tasks');
}
With this code, I am getting an error like this:
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 161:

You mistake when defined route for delete. It should be like this:
Route::delete('/subtask1/delete/{subtask}', 'TaskController#subtaskdestroy');
But you given:
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
More about Route Parameters:
Laravel Route Parameters

You used return redirect('/tasks'); in your controller. With this line the page will redirecte to route /tasks after successfully delete the data. Make sure you have /tasks route in your route file. Example:
Route::get('/tasks','YourController#method');

Related

Route [crops.restore] not defined

I created a resource controller. The problem is when I code a route in my view, the browser displays Route [crops.restore] not defined. I run the php artisan route:list command and I realize that the route does not exist in the route list.
My controller is this
// Display all crops
public function index(Request $request)
{
if ($request->has('trashed')) {
$records = Crop::onlyTrashed()
->get();
} else {
$records = Crop::get();
}
return view('crops.index', compact('records'));
}
//restore a file
public function restore(Request $request, $id)
{
Crop::withTrashed()->find($id)->restore();
return redirect()->back();
}
My route
// Crop controllers
Route::resource('crops', 'CropsController');
My View
#foreach ($records as $crops)
//code ...
#if(request()->has('trashed'))
<a class="btn btn-success btn-sm float-left mr-1" href="/crops/{{ $crops['id'] }}/edit">Edit</a>
Restore
#else
<form method="POST" class="float-left" action="{{ route('crops.destroy', $crops->id) }}">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-danger delete btn-sm " title='Delete'>Delete</button>
</form>
#endif
#endforeach
What's wrong with my code?
Restore provide this methods in your controller: index, store, edit, update, show, destroy. If you want redirect to your custom method like restore your have to rewrite your route in this matter:
Route::get('/restore', [App\Http\Controllers\CropsController::class, 'restore']);
Just add the route in the web.php file. For example:
Route::get('/restore', '\App\Http\Controllers\HomeController#restore');

How to change the boolean in DB calling a function in href?

In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>

Laravel missing required parameters on destroy route

I've just created a new project something like todo list in Laravel. When I try to do simple deleting I get this error:
Missing required parameters for [Route: destroy] [URI: {}]. (View: C:...\resources\views\index.blade.php)
Here is part of code from index.blade.php:
#if($todos)
<ol>
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="" method="post">
#csrf
#method('Delete')
x
</form>
#endforeach
</ol>
#endif
so I am just checking if there is anything, if not, then don't show list.
Part of code from controller:
public function index()
{
$todos = Todo::all();
return view('index', ['todos' => $todos]);
}
public function destroy($id)
{
Todo::findOrFail($id)->delete();
}
and line of code from web.php:
Route::resource('/', 'TodosController');
This is so basic and it is making me crazy because I can't figure out what is causing this error. Seems like everything is good.
Im not 100% certain, but try updating your resource route declaration to:
Route::resource('todos', 'TodosController');
and changing your link to
x
In the docs here, https://laravel.com/docs/5.7/controllers#restful-naming-resource-route-parameters
They describe the routes as being created like:
Actions Handled By Resource Controller
DELETE /photos/{photo} destroy photos.destroy
You don't actually submit your form. You just click on a link, so you in essence make a GET request, and not a POST request.
You need to send it through a form.
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST">
#csrf
#method('DELETE')
<input type="submit" class="btn btn-danger" value="x" />
</form>
#endforeach
Having an inline form like that can appear a bit weird, so you can instead put the button outside, and submit your form on click.
#foreach($todos as $todo)
<li>
{{ $todo->todo }}
<a href="#" class="btn btn-danger"
onclick="event.preventDefault();
document.getElementById('todo-destroy-{{ $todo->id }}').submit();">x</a>
</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST" id="todo-destroy-{{ $todo->id }}">
#csrf
#method('DELETE')
</form>
#endforeach

MethodNotAllowedHttpException in RouteCollection.php line 218 laravel 5.2 on update data

Please help me, im newbie in laravel and get an error
when I will update data field from button, like this
MethodNotAllowedHttpException in RouteCollection.php line 218:
and im using Routes:
Route::post('/verify', 'DataPelamarController#verify');
and I write my Controller like bellow:
public function verify(Request $request, $id){
DB::table('databeasiswas')->update([
'status_pendaftaran'=>'1']);
return redirect(url('pendaftar'));
}
and this is my view
<a href ="{{ url('verify') }}" name="_method" type="hidden" value="PATCH" >
<button type="submit" class="btn btn-vk btn-sm"><i class="fa fa-check"></i> Verify Data</button>
</a>
thanks before..
Just change this line. Patch is for updating, Post for new db entries. This is just a convention. You can use post for both.
Route::patch('/verify', 'DataPelamarController#verify');

Laravel Delete Method: MethodNotAllowedHttpException in RouteCollection.php line 233:

I tried to delete the cart from a list. When I tried to delete it, it shows an Error. Below here is my code:
Web.php
Route::post('cart/delete/{id}','ProductController#deleteCart');
blade.php
<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item">
<i class="fa fa-times"></i>
</a>
<form action="{{ url('/cart/delete',$row->id)}}" method="POST" style="display: none;">
{!! Form::hidden('id',$row->id) !!}
</form>
Controller.php
public function deleteCart($id){
$cart = Cart::find($id);
$cart->destroy();
return Redirect::to('/shop-cart');
}
Simply change the following line of code:
Route::post('cart/delete/{id}','ProductController#deleteCart');
into:
Route::get('cart/delete/{id}','ProductController#deleteCart');
Reason for this error is sending a GET request to a POST route. In your code you are sending a GET request by calling a URL.
<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item">
<i class="fa fa-times"></i>
</a>
Or otherwise if you want to keep the route as it is (as a POST route) just use the following code and make some adjustments accordingly:
<form action="{{ url('/cart/delete') }}" method="POST" style="display: none;">
{!! Form::hidden('id', $row->id) !!}
<input type="submit" value="Submit">
</form>
And it is better to modify the route as follows as the '/{id}' part is not needed as we are sending the id along with the POST request:
Route::post('cart/delete','ProductController#deleteCart');
Import Http\Request into your controller using:
use Illuminate\Http\Request;
And update your controller function as follows:
public function deleteCart(Request $request){
$cart = Cart::find($request['id']);
$cart->destroy();
return Redirect::to('/shop-cart');
}
But for this scenario GET route seems a good choice to avoid complexity.
Remove a href link, because it will send GET request, instead of POST. Add submit button to the form:
<form action="{{ url('/cart/delete/'.$row->id) }}" method="POST" style="display: none;">
{!! Form::hidden('id', $row->id) !!}
{!! Form::submit('delete') !!}
</form>
The error say that there is no route like the one you try to call. you have a route like this in your route file : /cart/delete/{id} where expects an id, and you call this from your form /cart/delete?id=theid
Fix
Change this from your submit url : /cart/delete',$row->id)
To this: /cart/delete/{{$row->id}}

Categories