Class 'auth' not found in some views - php

i put this code to my header layout that get a field from users table
<li class="tophe">
Your ID is : {{auth::user()->shenase}}
</li>
This code works good to my index page but when i open other pages i get this error
Class 'auth' not found (View:
###\resources\views\layouts\panel\header.blade.php)
What's the problem?

In laravel, there is an auth helper you can use to get the information you need about the currently authenticated user and you use it like so :
<li class="tophe">
Your ID is : {{auth()->user()->someAttribute}}
</li>
// not auth alone it's a function

You should use it as Auth, or auth():
<li class="tophe">
Your ID is : {{auth()->user()->shenase}}
</li>
Or:
<li class="tophe">
Your ID is : {{Auth::user()->shenase}}
</li>
Hope it helps.

<li class="tophe">
Your ID is : {{\Illuminate\Support\Facades\Auth::user()->shenase}}
</li>
You can try with give absolute path to auth class

Related

Laravel error: route not defined

Possible duplicate of this. But unable to get the answer in my case.
I need to use a link on my Laravel website, but it keeps resulting in "route not defined" error.
Html:
<ul class="nav navbar-nav">
#auth
<li>
Add post
</li>
#endauth
</ul>
web.php:
Route::get('/add-post', 'PagesController#add_post');
PagesController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function add_post()
{
return view('add-post');
}
}
You need to name the route in order to do that:
For example:
Route::get('/add-post', 'PagesController#add_post')->name('add-post);
This is because when you use route('add-post') are are requesting a URL based on the name set in the web.php file
So you basically have two options here. When you use the route function, Laravel is looking for a named route. In order to name a route, you can add ->name('name-of-route-here') at the end of your route definition.
If you don't want to name your route, you can just use the url helper function instead of route, so your code would be url('add-post')
Documentation on named routes: https://laravel.com/docs/5.5/routing#named-routes
Documentation on url function: https://laravel.com/docs/5.5/helpers#method-url
Just change this:
<ul class="nav navbar-nav">
#auth
<li>
Add post
</li>
#endauth
web.php:
Route::get('add-post', 'PagesController#add_post');

Missing required parameters for [Route: homepage.edit] with link in layout

I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])

404 not found laravel

I am new to Laravel , I am getting 404 not found error when returning view to salary report from my controller. The below mentioned is my function which returns my simple view to salary report.
public function getSalaryReport()
{
return view('Company.salaryReport');
}
the routes.php file ahs route to company controller.
Route::group(['middleware' => 'auth.company'], function () {
Route::get('company/notice-board/create', 'CompanyController#getNoticeBoardCreate');
Route::get('company/notice-board/{id}/edit', 'CompanyController#getNoticeBoardEdit');
Route::get('company/designation/{id}/edit', 'CompanyController#getDesignationEdit');
Route::get('company/all-user/{id}/force', 'CompanyController#getForce');
Route::post('company/all-user/{id}/force', 'CompanyController#postForce');
Route::controller('company', 'CompanyController')
this is my view which i am trying to display from my controller.
#extends('Company.CompanyLayout')
#section('content')
<div>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
<a href='{!! URL::to("company/report-summery") !!}'>Summery Report</a>
</li>
</ul>
</div>
#endsection
where i am going wrong and what should be done to make my view visible. Thanks to all in advance.
Route::controller is depricated in the latest versions of Laravel, try not to use it anymore.
You can use Route::resource or create a specific route for your salary report like this:
Route::get('company/salary-report', 'CompanyController#getSalaryReport');
Also make sure that you have resources\views\Company\salaryReport.blade.php as your view.
404 not found is an error because you don't have any routes for the given url. And I didn't find any routes in your example for the function getSalaryReport()
if you want to call this method, at least add this to your routes:
Route::get('company/report-summery', 'CompanyController#getSalaryReport');

CakePHP view not rendering

I have a basic application being baked by CakePHP.
Now i want to add a new action'listjobs' in my model controller class and a corresponding 'listjobs' view in the /Template/Job/listjobs.ctp.
From my model's index view i added one more action in the side navigation bar.
like this
<li><?= $this->Html->link(__('View Jobs'),['action' => 'listjobs']) ?></li>
When i click on the link 'View Jobs' control is directed to the action method of my model's controller but its not taking the list jobs 's view.
Code for my action method
public function listjobs()
{
$this->log("inside list jobs",'debug');
$this->render('listjobs');
}
The listjobs.ctp contains a very basic code as following
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li>< Git Hub >
</li>
<li>< Stack overflow >
</li>
</ul>
</div>
Not able to find out why the view is not rendered rather the index view is getting rendered.
Links loaded according to chrome console in order
(1) http://localhost/myjobs/job/listjobs
(2) http://localhost/myjobs/job
So (1) should have been loaded with the view..it should not been redirected to (2)
Regards,
Saurav
I think you need to use like this.
<li><?php echo $this->Html->link('View Jobs',array('controller' => 'MyModel', 'action' => 'listjobs')); ?></li>
Your view will be.
public function listjobs()
{
$this->log("inside list jobs",'debug');
}
I think you don't need to render, your view will be render by default, you will need to send data like that $this->set(compact('VarName'));
Secondly $this->log("inside list jobs",'debug'); if log is a method then you need to set action, then it will be.
$this->setAction('log');
Try, if you get error, then let me know. If your work proper then, you view will work.
http://localhost/myjobs/job/listjobs
this link defines.
http://localhost/SiteName/ControllerName/ActionName/Parameter
Get review and study this page.
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html
Thanks
ok...i did some debugging of the cakephp framework code.
I was using an auth component for my login functionality.
I found that the newly added action was not allowed to pass the through filter of auth component. so i added the newly added action in the beforefilter method of appcontroller like this. i guess the crud operations are already added in the filter by default.
$this->Auth->allow(['index', 'view', 'display','listjobs','add','delete']);
cheers,
Saurav

Laravel 4 User Profile Link

I currently have a route that looks like this:
Route::get('/profile/{username}',array(
'as' => 'profile',
'uses' => 'ProfileController#getProfile'
));
The idea is that a link, like http://www.website-here.com/profile/johnnyappleseed, containing a username will do a search for the username in the database and return the profile that matches based on the username in the URL. How would I create a link to the logged in User's profile using the same route. Currently I have
<li>Profile</li>
But that seems incorrect and that there should be a way to do it using some of Laravels helpers.
You can use the route helper and pass the route name and an array of parameters needed:
<li>
<a href="{{ route('profile', [Auth::user()->username]) }}">
Profile
</a>
</li>
If you want to go all the way and use even more Laravel helpers, you can use the HTML facade and generate the entire link tag in one line:
<li>
{{ HTML::link(route('profile', [Auth::user()->username]), 'Profile') }}
<li>

Categories