I'm new to laravel framework and trying to implement login system to my website. My web site has a landing page. Base on user login status I need to change this links like bellow.
<ul class="secondary_menu">
#if(Auth::guard('customer')->check())
<li><a href="{{url('/logout')}}" >Logout</a></li>
#else
<li><a href="{{url('/login')}}" >Login or Register</a></li>
#endif
</ul>
Here's the controller responsible for loading this view
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('public.index');
}
}
I'm getting an error that saying that Class 'Auth' not found
I try adding use Auth and use \Auth as suggested in other slimier kind of questions in stackoverflow but did not works for me. What am i doing wrong. Isn't is possible to check call Auth from view ?
Put the following code to use the Auth in your code:
use Illuminate\Support\Facades\Auth;
and then use it like:
if (Auth::check())
{
// your code
}
As alternative to Auth:: facade you can use global helper:
auth()->check();
auth()->user();
Try this on your controller
use Illuminate\Support\Facades\Auth;
Related
After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.
I am trying to pass all available designations in register form as follow in laravel 5.7.21 . This is the function in RegisterController
public function showRegistrationForm(){
$designations = Designations::all();
return view('auth.register', compact('designations'));
}
these are upper lines of RegisterController
namespace App\Http\Controllers\Auth;
use App\User;
use App\Designation;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
This the file structure of my project
Please click here to view file structure
When I try to acces Registraion page via http://localhost:8081/register
It generate a error like this
please click here to view the erro
Could anyone please help me slov this hens Im new to laravel. Thanks
You spelled Designations wrong. It should be Designation::all() (as you have imported use App\Designation;).
I have application in laravel.When I try to access a custom method using URL localhost/blog/public/contact/myownview it doesn't give any output. I want to redirect it to view called video.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
//My Custom method
public function myownview(){
echo "yest";
//return view('video');
}
}
routes/web.php
Route::get('/contact/myownview','ContactController#myownview');
Try this
Route::get('/my-own-view', 'ContactController#myownview')->name('my-own-view);
and hit the http://localhost:8000/my-own-view, <url>+route name
return view('video');
Make sure in resources/views file has video.blade.php
You need to custom your route.php
Route::get('/blog/public/contact/myownview','ContactController#myownview');
Coming from CodeIgniter, I decided that it's time for something new and went for Laravel. I love the syntax of the framework and how clean it is, however, I am overwhelmed by how complicated simple things seem to be. I have a controller and want to link to a function in said controller. Whatever I do, I keep getting this error:
ReflectionException in Route.php line 280:
Class App\Http\Controllers\TasksController does not exist
I have Googled this issue but I just can't seem to figure it out. It seems like I got everything right but then again, I'm completely new to this framework so I don't really know. I have a namespace, a route and all of that stuff. Anyway, here is my code:
The link
<a class="nav-link" href="<?= url('tasks') ?>">Tasks</a>
My route in routes.php
Route::resource('tasks', 'TasksController');
TasksController.php
<?php
namespace App\Http\Controllers\Controller;
use App\User;
use App\Http\Controllers\Controller;
class TasksController extends Controller
{
public function index()
{
$tasks = DB::table('tasks')->get();
return view('tasks', ['tasks' => $tasks]);
}
}
Thank you for any answers and if you need more information, please say so.
Given that you are using default Laravel installation, the current directory of controllers does not exist.
Try changing
<?php
namespace App\Http\Controllers\Controller;
to
<?php
namespace App\Http\Controllers;
in your TasksController.php file
Your namespace (in controller file) is App\Http\Controllers\Controller and should be App\Http\Controllers.
I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}