i have a problem in delete an order:
here the form where press on cancel to delete the order:
<form action="{{route('user.orders.delete', $order->id)}}" method="POST"
onsubmit="return confirm('Are you sure?');">
#method('DELETE')
#csrf
<button
type="submit" class="mt-8 mb-4 py-2 px-14 rounded-full
bg-red-900 text-white tracking-widest hover:bg-blue-800
transition duration-200">
Cancel
</button>
</form>
this is my router:
Route::delete('user/orders/delete/{order}', [OrderController::class, 'delete'])->name('user.orders.delete');
OrderController : delete function:
public function delete(Order $order)
{
try {
unlink(public_path().'/storage/orders/files/'.$order->file);
$order->delete();
}catch(\Exception $e) {
return redirect()->back()-with('status', 'you cannot delete this item');
}
return redirect()->back()->with('status', 'product deleted succefully');
}
so when click on the button (cancel) just go to the right url : when 21 is the order id
http://127.0.0.1:8000/user/orders/delete/21
and it is just not found page!!! and nothing deleted!!!
i think your app routes is cached
try to run the fallowing command (in cmd or terminal) to see all the routes, this will show registered routes and errors if exist:
php artisan route:list
if the route doesnt exist in route list, the route list may be cached. try the fallowing command to clear route cache:
php artisan route:clear
and then run app and test the route.
If you do not have another problem in coding or naming variables, the program will be fixed properly
*if all o the above is ok, it seems your apache config is not configure to delete request(for route delete method ) and you must change .htaccess requests LIMIT like the fallowing sample to Grant delete request :
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
As you can see the DELETE method(request) has been added to .htaccess
Related
I have following code in route web.php file.
Route::resource('dailyrevenue', DailyRevenueController::class)->middleware('auth');
Then in my DailyRevenueController.php
public function destroy(DailyRevenue $revenue)
{
$revenue->delete();
return redirect()->back();
}
And in my vue3 code:
const submit = function (id) {
const check = confirm("Are you sure to delete ?")
if (check) {
Inertia.delete(route('dailyrevenue.destroy',id), {
id: id,
method: 'delete',
forceFormData: true
})
}
}
Finally in the template:
<template #cell(actions)="{item: tax}">
<form method="delete" #submit.prevent="submit(tax.id)">
<input type="hidden" name="_method" value="delete"/>
<button type="submit">Delete</button>
</form>
</template>
Now request reaches to the method. But nothing is deleted. The request sent is DELETE request. Instead of 302 response it sends back 303 (See others).
Community help is appreciated.
Thanks.
I found the solution. The reson behind it was the variable name. In url endpoint the variable name was decleared as dailyrevenue and in the method as $revenue.
You can find your url variable by typing php artisan route:list.
I found that the url variable name must match with variable name in method.
Hope this helps others too.
Hello I have instructors table, and every instructor have name column and I have a page to show events and every event has instructor name and I need to go to the instructor profile page when clicking on the instructor name in the events page so I made this
<a href="{{ route('instructor.profile', ['name' => $event->instructor]) }}" class="a-reset">
<h5 class="color-primary fw-bold">{{ $event->instructor }}</h5>
</a>
web.php
Route::get('/instructor/{name}', 'App\Http\Controllers\InstructorController#profile')->name('instructor.profile');
InstructorController
public function profile($name) {
$instructor = Instructor::where('name', '=', $name)->first();
return view('instructor-profile', compact('instructor'));
}
But it gives me 404 Not Found error page
Make sure it returns value $event->instructor
Make sure the address is correct instructor-profile
by this command rtisan route:list open route list, make sure it is there /instructor/{name} route.
if everything is correct run this command
php artisan route:clear
php artisan config:cache
I have a button in my blade like this
#can('customer_show')
<a class = "btn btn-primary" href = "{{ route('admin.loan-applications.showCustView', $loanApplication->user_id) }}">
View Applicant
</a>
#endcan
And this is the route:
Route::get('loan-applications/{loan_application}/showCustView', 'loanApplicationsController#showCust')->name('loan-applications.showCustView');
And in my controller, i did:
public function showCust(LoanApplication $loanApplication)
{
$customerInformation = customerInfoModel::where('Cust_id', $loanApplication->user_id));
return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}
What i am trying to do is fetch the row from the database attached to customerInfoModel where the the Cust_id field equals the loanApplication->user_id of the loan being viewed currently in the blade above. When the button "view Applicant" is hit, i get an error 404 page. Why is that so?
Check it out the route list using this command
php artisan route:list
//this command will show all the routes in your application
If your route not listed on that route list checkout for routes with same Url on your route manually on routes file.
if you found change the url & use this command to clear cache
php artisan optimize:clear
i have found the comment of you on last answer.
check out for route model binding . i think you have to add
a
public function showCust( $loanApplicationCustId)
{
$customerInformation = customerInfoModel::where('Cust_id', $loanApplicationCustId))->first();
return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}
It should be like this .. i hope it works for you......
else share your project git repo link
I think it should be like following:
#can('customer_show')
<a class = "btn btn-primary" href = "{{ route('loan-applications.showCustView', $loanApplication->user_id) }}">
View Applicant
</a>
#endcan
try that
I'm trying to save data into db but its not saving and says that object not found, can anyone suggest me solution, i am following this tutorial: https://laracasts.com/series/laravel-from-scratch-2018/episodes/10
controller:
public function index()
{
$projects = Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store()
{
$project = new Project();
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
routes:
Route::get('/projects','ProjectsController#index');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/create','ProjectsController#create');
create.blade.php:
<form method="POST" action="/projects">
{{ csrf_field() }}
<div>
<input type="text" name="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Project description"></textarea>
</div>
<div>
<button type="submit">Create Project</button>
</div>
</form>
index.blade.php:
#foreach($projects as $project)
<li>{{ $project->title }}</li>
#endforeach
You have missed out passing request parameter in the controller store()
public function store(Request $request)
{
$project = new Project();
$project->title = $request->title;
$project->description = $request->description;
$project->save();
return redirect('/projects');
}
And also don't forget to include use Illuminate\Http\Request; above(outside) controller class.
The Laravel code you've posted is correct under a properly configured website. The error from your comments:
Object not found! The requested URL was not found on this server. The
link on the referring page seems to be wrong or outdated. Please
inform the author of that page about the error. If you think this is a
server error, please contact the webmaster. Error 404 localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
is an Apache error page, which means it's not requesting a page from your laravel project at all. The data is probably saving in your database, but then you redirect away to a page that is outside your project, and Apache can't find it.
Your website is located at http://localhost/laravel/public, which means you need to access the projects page at http://localhost/laravel/public/projects. However, redirect('/projects') gives you an absolute path instead of a relative path, sending you to http://localhost/projects, which does not exist.
Solutions
Since this is a local development project, I'm going to skip the issues with the improper Apache configuration and focus on other ways to avoid the error.
Option 1
Use a named route:
Route::get('/projects','ProjectsController#index')->name('projects.index');
and use the name of the route for the redirect:
return redirect()->route('projects.index');
This should generate correct urls within your project.
Option 2
Use serve for development instead of Apache.
Open a terminal in your Laravel project directory and run this command:
php artisan serve
This will start PHP's built-in webserver at http://localhost:8000, skipping Apache entirely. During development this is perfectly fine.
I've encountered a problem submitting my form in laravel. My form structure looks this.
<form class="form-group" action="{{ route('writepoem') }}"
method="post" name="publish" enctype="multipart/form-data"
onsubmit="return validateForm();">
<input type="hidden" name="_token" value="{{ Session::token() }}">
<input type="text" name="user">
<textarea name="poem"></textarea>
<input type="submit" value="save">
</form>
My web.php file has this route.
Route::post('/writepoem', ['uses'=>'PoemController#postCreatePoem','as'=>'writepoem']);
My PoemController.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Poem;
class PoemController extends Controller
{
public function postCreatePoem(Request $request)
{
//validation
$poem=new Poem();
$poem->poem=$request['poem'];
$poem->user=$request['user'];
//save poem
$request->user()->poems()->save($poem);
return redirect()->route('feed');
}
}
On submitting my form I get this Exception.
(1/1) NotFoundHttpException
in RouteCollection.php line 179.
What could be the issue with routing?
If your route points site.com/writepoem and this route returns notfoundexception; this means 2 possible things;
1) Your route has a group and this group has a parent namespace for url
Route::group(['prefix' => 'poems'], function() {
Route::post('/writepoem', ['uses'=>'PoemController#postCreatePoem','as'=>'writepoem']);
});
2) Or your route file is cached. please try; php artisan route:clear & php artisan route:cache
I found that my form was linked to the main page via
`require resource_path().'/sub-folder/write.php'`
and that was causing the error.
I moved the form to the main document and my issue was fixed.
I had the same problem but now I fixed my problem.
You need to follow these steps. If you have written the right code and then you get this error, then you need to follow steps.
Clear cache php artisan route:clear & php artisan route:cache run command one by one. If your problem not fixed yet then you need to follow the second step also
Run this command to reload composer composer dump-autoload