UserController not found - laravel

i have my blade file like this:
<div class="nav-item dropdown">
#if (Auth::check())
{{ Auth::user()->name }}
<div class="dropdown-menu">
Profile
My Order
<a href="#" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout </a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
#csrf
</form>
</div>
#else
<div class="nav-item dropdown">
Login
<div class="dropdown-menu">
Customer
Register
</div>
</div>
#endif
</div>
and my router like this:
/multi-auth router
Route::prefix('user') ->name ('user.') ->group (function(){
Route::middleware (['guest']) ->group (function(){
Route::view('/login', 'dashboard.user.login') ->name ('login');
Route::view('/register', 'dashboard.user.register') ->name ('register');
});
Route::middleware(['auth']) ->group(function(){
Route::view('/home', 'dashboard.user.home') ->name ('home');
});
});
i have already add use App\Http\Controllers\UserController; in the web.php but i still get that UserController cannot found. I really dont know where i am wrong. Is it perhaps i dont use Route::has (user.login)?
i am using laravel/ui but i have changed my blade to the one i wrote above

but you didnt mention the UserCotroller in your route? how do you expect ot to find it ? you are returning views through your route, you are not redirectting it to your controller
here is how you should declare it :
Route::get('/register', [UserController::class, 'create'])
->middleware('guest')
->name('register');
Route::post('/register', [UserController::class, 'store'])
->middleware('guest');
Route::get('/login', [UserController::class, 'create'])
->middleware('guest')
->name('login');
Route::post('/login', [UserController::class, 'store'])
->middleware('guest');
and in the create method you can write
return view(dashboard.user.login);

Related

Larave 8 jetstream domain/logout not working

When I try to http://domain.test/logout then showing "The GET method is not supported for this route. Supported methods: POST"
But normal logout with post method working perfectly. How can I change the /logout route Post to get.
in Jetstream, fortify
if you list your routes using php artisan route:list you'll see that logout route is defined for POST, so you just need to submit a form to this route in order to fire logout.
POST | logout | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController#destroy | web
Now if you wish to convert that function to respond to GET method, you'll need to change that route in /vendor/laravel/fortify/routes/route.php
I have not tested it in any way, but the method and routes are there.
If still anyone is looking for it you can try this.
In Laravel 8 get method doesn't support logout route you can try post method like below.
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
You can also try this by check in if it's login then it will show you logout otherwise it will show you login .
#if (auth()->id())
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
#else
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}" role="button">
<i class="fas fa-sign-in-alt"></i>
Login
</a>
</li>
#endif
For Anyone Facing This Problem in Laravel Jetstream use this Code
<form method="POST" action="{{ route('logout') }}">
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}" onclick="event.preventDefault(); this.closest('form').submit();">
<i class="fa fa-sign-out"></i>{{ __('Logout') }}
</x-jet-dropdown-link>
</form>
You should put them in the base.blade.php file or the file where the logout form is supposed to be

How to fix DELETE method not working in Laravel

My delete method is not deleting from the database and I can't seem to see what it is I'm missing in my code when I click on delete. I've provide my blade file, controller and wweb.php file below, any assistance will be highly appreciated.
Blade File
<div class="row max-inner">
#foreach ($tshirts as $tshirt)
#auth
<form action="{{ route('t-shirtsAdmin.destroy', $tshirt) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-lg">DELETE</button>
</form>
#endauth
<div class="columns col-6 collection-item">
<div class="row">
<a href="#">
<div class="slider">
#foreach (json_decode($tshirt->filenames) as $picture)
<div>
<img src="{{ asset('files/' . $picture) }}" alt="kids top" loading="lazy">
</div>
#endforeach
</div>
<div>
<div>
<div class="columns col"><i class="fas fa fa-child"></i> {{ $tshirt->gender }} top </div>
<div class="columns col"><i class="fas fa fa-calendar"></i> {{ $tshirt->age }} Years</div>
<div class="columns col"><i
class="fas fa fa-tag"></i>
Kshs 250</div>
</div>
<div class="row" style="display: flex; text-align: center; ">
<div class="columns col"><i class="fas fa fa-phone"></i> 0700 00000</div>
</div>
</div>
</a>
</div>
</div>
#endforeach
</div>
web.php
Route::resource('t-shirtsAdmin', 'App\Http\Controllers\TshirtController');
CONTROLLER
<?php
namespace App\Http\Controllers;
use App\Models\Tshirt;
use Illuminate\Http\Request;
class TshirtController extends Controller
{
public function destroy(Tshirt $tshirt)
{
$tshirt->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
}
ok , this problem because model binding , you can solve this by 2 way first :-
public function destroy($id)
{
Tshirt::find($id)->delete();
// or Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
second :- for route binding to work you should have type-hinted variable names match a route segment name, as the doc required : so your method will be like this :-
public function destroy(Tshirt $t-shirtsAdmin)
{
// $t-shirtsAdmin is the name of the router
$t-shirtsAdmin->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
you can find the route name var , when you run
php artisan route:list , you will find the var like this : t-shirtsAdmin/{t-shirtsAdmin} so you should use it in controller to bind it.
You cant just call the $tshirt param and expect it to delete, try something like this:
$id = $tshirt->id;
Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');

Laravel Blade Display Only if Not Logged In

I 'm using Laravel's middleware, and I'm trying to have an empty div if the user is not authenticated. Otherwise, I am trying to have a div that allows the user to create an account. I have tried the following:
#auth
<div class="form-group col-md-6"></div>
#else
<div class="form-group col-md-6">
<h6>Create Account</h6>
</div>
#endauth
Routes
Route::get('/checkout', 'CheckoutController#index')->name('checkout.index')->middleware('auth');
Route::get('/guestCheckout', 'CheckoutController#index')->name('guestCheckout.index');
CheckoutController.php
if (auth()->user() && request()->is('guestCheckout')) {
return redirect()->route('checkout.index');
}
The above does not work, and I have tried deleting the history, clearing caches, etc. but to no avail. Suggestions? Thank you!
trying to have an empty div if user is not authenticated...
Your blade setup is really close. Try this!
#auth
<!-- the user is authenticated -->
<div class="form-group col-md-6>
</div>
#endauth <!-- note the end auth -->
#guest
<!-- the user is NOT authenticated -->
<div class="form-group col-md-6>
<h6>Create Account</h6>
</div>
#endguest
You can read up more about this here
As for your controller, try using:
if( Auth::check() )
{
// The user is authenticated..
} else {
// The user is NOT authenticated..
}
There's out of the box way to do this!
#guest
<div class="form-group col-md-6>
</div>
#else
<div class="form-group col-md-6>
<h6>Create Account</h6>
</div>
#endguest
You don't need the middleware thingy if I were you.
You can Do it like this
<ul>
#guest
#if (Route::has('login'))
<li><a class="nav-link scrollto {{Request::url() === route('login') ? 'active' : ''}}" href="{{route('login')}}">Login</a></li>
#endif
#if (Route::has('register'))
<li><a class="nav-link scrollto {{Request::url() === route('register') ? 'active' : ''}}" href="{{route('register')}}">Register</a></li>
#endif
#else
<li class="dropdown"><span>{{ Auth::user()->name }}</span> <i class="bi bi-chevron-down"></i>
<ul>
<li>
{{ __('Logout') }}
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
#csrf
</form>
</li>
</ul>
</li>
#endguest
</ul>
The current (Laravel 8) way to do this is by using the Auth helper directives.
https://laravel.com/docs/8.x/blade#authentication-directives
#auth
This only shows for logged in users
#endauth
#guest
This only shows for guests
#endguest

trying to get property of non-object multi-auth

I use Hesto/multi-auth package. The username when I login success default redirect in customer.layout.auth, how can I to redirect in my blade, Example: welcome.blade.php, I can't use {{ Auth::user()->name }} in another blade, it error Trying to get property of non-object . How to fix it, please help me !
AppServiceProvider.php
public function register()
{
//
if ($this->app->environment() == 'local') {
$this->app->register('Hesto\MultiAuth\MultiAuthServiceProvider');
}
}
auth.blade.php
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ url('/customer/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/customer/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
#endif
I had the same problem and I think I found the solution.
You are creating your routes on the web route file.
You need to create your routes inside user route file created by Hesto/multi-auth package.
I don't know if this is the right way of doing it but it worked for me.

anchor tag not working on laravel 5.2

I wont to make direct page with anchor tag in blade template engine, but its not work.
_sidebar.blade.php
<li class="active treeview">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
routes.php
Route::get('/', 'PagesController#getIndex');
Route::get('data_jamaah', 'PagesController#getJamaah');
PagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Inbox;
class PagesController extends Controller
{
public function getIndex() {
return view('pages.index');
}
public function getJamaah() {
return view('pages.data_jamaah');
}
}
The problem is When i click a nothing to happen, thanks for your help
Edit:
This is main.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
#include('partials._head')
#yield('stylesheets')
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
#include('partials._nav')
#include('partials._sidebar')
#yield('content')
<footer class="main-footer">
#include('partials._footer')
</footer>
</div>
#include('partials._javascript')
#yield('javascripts')
</body>
</html>
Your issue is coming from of how you are using adminLTE Sidebar.
When you specify treeview in your class, you will have a dropdown list. And the anchor will be deactivated.
So try something like that :
<li class="active">
<a href="{{ url('/') }}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li>
<a href="{{ url('data_jamaah') }}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
</li>
I think you can try this hope this work for you:
Route::get('/', 'PagesController#getIndex')->name('home');
Route::get('data_jamaah', 'PagesController#getJamaah')->name('dataJamaah');
<li class="active treeview">
<a href="{{route('home')}}">
<i class="fa fa-dashboard"></i> <span>Menu Utama</span>
</a>
</li>
<li class="treeview">
<a href="{{route('dataJamaah')}}">
<i class="fa fa-users"></i>
<span>Data Jamaah</span>
</a>
Laravel has it's own helper function to create the routes, which you can see here:
https://laravel.com/docs/5.4/helpers#method-route
Edit:
You also need to have a name for your routes to work from the helper function.
On your routes.php:
Route::get('/', ['uses' => 'PagesController#getIndex', 'as' => 'homepage']);
Route::get('data_jamaah', ['uses' => 'PagesController#getJamaah', 'as' => 'data.jamaah']);
You need to change the href from url() to route().
Change
From:
<a href="{{ url('/') }}">
To:
<a href="{{ route('homepage') }}">
and for other route
From:
<a href="{{ url('data_jamaah') }}">
To:
<a href="{{ route('data.jamaah') }}">
And it will work perfectly fine.
If we specify the name the laravel routes automatically gets the route and renders for you.

Resources