Laravel - Route resource update functions has not working - php

I could not run the in a resource route, Controller > Update function. When I try send a form to the update, the page is return to back. Even if the update functions is completely empty. But the functions has not be triggered.
My codes:
// Controller
public function update(UserRequest $r, $id)
{
die('asdasd');
}
// HTML
<form class="ban-form" style="visibility: hidden" action="{{ route('user.update', $user->id) }}" method="POST">
#method('PUT')
#csrf
</form>

The problem is probably in your UserRequest file.
Your request doesn't not pass the validation. Therefore you are redirected back to your form.
First, try to replace UserRequest with a simple Request in order to check if that's the problem and if it is, triple check your validations and dump the content of the $errors (using {{ dump($errors) }}) variable that is injected in your view when the validation is failling.
This is the full documentation on this topic: https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors.

<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
class_alias('App\Http\Controllers\HomeController', 'HomeController');
class_alias('App\Http\Controllers\AdminController', 'AdminController');
class_alias('App\Http\Controllers\FormController', 'FormController');
class_alias('App\Http\Controllers\ActionController', 'ActionController');
class_alias('App\Http\Controllers\UserController', 'UserController');
Route::get('/', 'HomeController#index')->name('home');
Route::get('giris-yap', 'HomeController#login')->name('user.login');
Route::get('kayit-ol', 'HomeController#register')->name('user.register');
Route::get('sifremi-unuttum', 'HomeController#forgotPassword')->name('user.forgot-password');
Route::get('hesabim', 'HomeController#account')->name('user.account');
Route::get('satin-al/{id}', 'HomeController#buy')->where('id','[0-9]+')->name('user.buy');
Route::get('satin-al/adim/2', 'HomeController#pay')->name('pay');
Route::get('{slug}', 'HomeController#slug')->name('slug');
Route::prefix('action')->group(function()
{
Route::get('logout', 'ActionController#logout')->name('user.logout');
});
Route::prefix('form')->group(function()
{
Route::post('login', 'FormController#login')->name('form.user.login');
Route::post('forgot-password', 'FormController#forgotPassword')->name('form.user.forgot-password');
Route::post('pay/{method}', 'FormController#pay')->where('method','(transfer|credit-card|crypto-currency)')->name('form.user.pay');
Route::post('contact', 'FormController#contact')->name('form.contact');
Route::post('update/pp', 'FormController#updatePP')->name('form.user.update.pp');
Route::post('update/profile', 'FormController#updateProfile')->name('form.user.update.profile');
});
Route::prefix('admin')->group(function()
{
Auth::routes();
Route::get('/index', 'App\Http\Controllers\AdminController#index')->name('admin.login');
Route::group(['middleware' => 'auth'], function () {
Route::resource('user', 'UserController');
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'App\Http\Controllers\ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'App\Http\Controllers\ProfileController#update']);
Route::get('upgrade', function () {return view('pages.upgrade');})->name('upgrade');
Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'App\Http\Controllers\ProfileController#password']);
});
});

Related

laravel specify the routes for every role

I am trying to set up routing for different roles in my application but I am encountering an error. I want to know if the approach I am using is correct. I would like to specify the routes for each role and I am unsure if my method is the right one to achieve this.
This is my web.php file:
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//guest pages
Route::get('/', function () {
return redirect()->route('login');
});
Auth::routes();
route::middleware('auth')->group(function () {
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::middleware('hasRole:Super Admin')->prefix('SuperAdmin')->group(function () {
Route::prefix('users')->group(function () {
//users routes
Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
});
Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
//fournisseurs routes
Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
Route::delete('{id}', [App\Http\Controllers\FournisseurController::class, 'destroy'])->name('destroy');
Route::get('edit_fournisseur/{id}', [\App\Http\Controllers\FournisseurController::class, 'edit'])->name('edit');
Route::put('fournisseurs/{id}', [\App\Http\Controllers\FournisseurController::class, 'update'])->name('update');
Route::get('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'create'])->name('create');
Route::post('ajouter_fournisseur', [App\Http\Controllers\FournisseurController::class, 'store'])->name('store');
});
Route::prefix('factures')->name('facture.')->group(function () {
//factures routes
Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
Route::delete('{id}', [App\Http\Controllers\FactureController::class, 'destroy'])->name('destroy');
Route::get('ajouter_facture', [App\Http\Controllers\FactureController::class, 'create'])->name('create');
Route::post('ajouter_facture', [App\Http\Controllers\FactureController::class, 'store'])->name('store');
Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
});
});
Route::middleware('hasRole:Admin')->prefix('Admin')->group(function () {
Route::prefix('users')->group(function () {
//users routes
Route::get('', [\App\Http\Controllers\UserController::class, 'index'])->name('users.index');
Route::get('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
Route::put('profile/{id}', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
Route::delete('/delete_user/{id}', [\App\Http\Controllers\UserController::class, 'destroy'])->name('users.destroy');
Route::get('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'create'])->name('user.create');
Route::post('ajouter_utilisateur', [App\Http\Controllers\UserController::class, 'store'])->name('user.store');
});
Route::prefix('fournisseurs')->name('fournisseur.')->group(function () {
//fournisseurs routes
Route::get('', [App\Http\Controllers\FournisseurController::class, 'index'])->name('index');
});
Route::prefix('factures')->name('facture.')->group(function () {
//factures routes
Route::get('', [App\Http\Controllers\FactureController::class, 'index'])->name('index');
Route::get('download/{id}', [App\Http\Controllers\FactureController::class, 'downloadFacture'])->name('downloadFacture');
});
});
});
And with this solution i get the error message
Optimization failed (See output console for more details)
Can someone help me to find out the solution for this issue or suggest me the right way to do it?
for role management in Laravel I suggest you use "spatie/Laravel-permission".
this is documention link:
https://spatie.be/docs/laravel-permission/v5/introduction
after install this package you must create middleware for each role and then check those on web.php

Laravel 5.6 Route Group

My controllers which are HomeController and BlogController in Admin folder. My views like:
/admin
index.blade.php
/blog
index.blade.php
I want to call /admin0admin url to /resources/views/admin/index.blade.php.
I want to call /admin0admin/blog url to /resources/views/admin/blog/index.blade.php
Here how i call in view:
<a href="{{ route('admin0admin.blog') }}" class="br-menu-link">
And my routes like:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin0admin'], function () {
Route::get('/', 'HomeController#index')->name('index');
Route::group(['prefix' => 'blog'], function () {
Route::get('/', 'BlogController#index')->name('index');
});
});
And my BlogController index method:
return view('admin.blog.index');
I got an 404 not found error.
Route [admin0admin.blog] not defined
Laravel Version is : 5.6.*
You need to name the route admin0admin.blog, not index. prefix does not affect names of routes, so you need to write it out.

Laravel route Resource breaks another route

When I use my route like this, the test route is working fine.
Route:-
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::get('user/test','UserController#test'); // is working
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'USerController#planed']);
.
.
.
.
But when I want to use it like following, it shows a blank page :
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
Route::get('user/test','UserController#test'); // is not working
.
.
.
.
What is my mistake?
Because the call to test is intercepted by
Route::resource('user', 'UserController');
because if you check the routes in the console with
$> php artisan route:list
you'll see it includes
GET|HEAD | user/{user}
and then your first route
Route::get('user/test','UserController#test'); // is not working
is never reached. Try to put it BEFORE the other line.
I thing the issue in naming the route check This
Try like this
Route::group(array('namespace' => '\User'), function () {
Route::get('user/my-favorite','UserController#myFavorite');
Route::resource('user', 'UserController');
Route::group(['middleware' => ['auth']], function () {
Route::get('user/planed','UserController#planed'])->name('user.planed');
Route::get('user/test','UserController#test'); // is not working
Just replace
Route::get('user/planed',['as' => 'user.planed', 'uses' => 'UserController#planed']);
With
Route::get('user/planed','UserController#planed'])->name('user.planed');

laravel redirect to route when user is logged in

i am a beginner in laravel, i am trying to redirect to another route if the user is logged in, the signup and login are working perfectly and are not a problem, but when i try to do
#if(Auth::check())
{{
redirect()->route('news')
}}
#endif
the redirect script gets output on the screen like this:
HTTP/1.0 302 Found Cache-Control: no-cache, private Location: http://localhost/red-sec/public/news <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="1;url=http://localhost/red-sec/public/news" /> <title>Redirecting to http://localhost/red-sec/public/news</title> </head> <body> Redirecting to http://localhost/red-sec/public/news. </body> </html>
Please excuse me if i did a rookie mistake i am extremely new to laravel, and the news route is set up correctly and is working
EDIT 1:
for the first comment, yes, here is my web.php file:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::post('/signup', [
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
Route::post('/signin', [
'uses' => 'UserController#postSignIn',
'as' => 'signin'
]);
Route::get('/news', [
'uses' => 'userController#getNews',
'as' => 'news',
'middleware' => 'auth'
]);
You shouldn't try to (and can't) redirect in views. Views should be ONLY used to display data, not to do business logic.
Because you are not using controller to do any logic (you return view directly from router), you can do something like this:
Route::get('/', function () {
if(Auth::check()) {
return redirect()->route('news');
}
return view('welcome');
})->name('home');
Text displayed in your view is actually a HTTP response.
so I'm assuming you want to see if they're already logged in, and if they are you want to redirect them away from the login page? You could accomplish that in a Route::get('/signin') method on UserController. Before it returns the signin view you could do Auth::check(), and if that is true, then do the redirect()->route('news')
You should note, however, that Laravel ships with a ton of authentication scaffolding already in place, which you can read about here.
In your web.php, have this take the place of the / route:
Route::get('/', function() {
if (Auth::check()) {
return redirect()->route('news');
}
else {
return view('welcome');
}
}

Skipping a middleware for a particular route inside a route group in Laravel

I Want to skip the middleware for a particular route in a route group. How can I do this?
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
Route :: get('/api','testController#apiCall');
});
I want to skip the 'login.oauth' middleware for the Route :: get('/api','testController#apiCall')
Please keep that testgroup function must be accessible to all routes and middleware function to particular(some other route) in the same function
Route::group(['prefix' => 'testgroup'], function () {
Route::group(['middleware' => ['login.oauth'], function() {
Route :: get('/', 'testController#index');
});
Route :: get('/api','testController#apiCall');
});
Just create a second group without the middleware:
Route::group(['prefix' => 'testgroup','middleware' => ['login.oauth']],function (){
Route :: get('/', 'testController#index');
});
Route::group(['prefix' => 'testgroup'],function (){
Route :: get('/api','testController#apiCall');
});

Categories