Call to undefined method App\User::hasRole() - laravel

I'm using spatie/laravel-permission to handle roles. I installed the package through composer did the migrations, and added the service provider in my config/app.php file, created some roles and everything fine, but when i'm going to use the blade directive #hasanyrole for example:
#hasanyrole('Profesional')
#include('calendar.modal_appointment', ['order_details' => $order_details])
#endhasanyrole
It throws this error:
Call to undefined method App\User::hasAnyRole()

Had to add use Spatie\Permission\Traits\HasRoles; and use HasRoles; on my User model that fixed the problem

Related

Datattables Laravel 5.4 - FatalThrowableError Call to undefined function App\Http\Controllers\Datatables()

I'm using datatables with laravel 5.4, but when I use the datatables()->query():
in providers: Yajra\Datatables\DatatablesServiceProvider::class,
in aliases: 'Datatables' => Yajra\Datatables\Facades\Datatables::class,
My code:
return Datatables()->query( /**query**/ )
return this error in my console:
FatalThrowableError
App\Http\Controllers\Datatables()
I've tried datatables() and Datatables()
Someone with the same problem?
This error happens because Laravel cannot find your package Datatables.
Laravel in controllers, if you are not mapping your classes correctly will always seek the called classes at this point App\Http\Controllers.
Just put use DataTables; or use \Yajra\Datatables\Datatables; at the start of your controller and it shall be fixed. documentation
When you install any package through composer, composer uses namespace to find the package and route functions and classes to it.
you can read more about the namespaces from php.
and you can check this tutorial for namespaces.
Also, Laravel follows the PSR 4 when it comes to autoloading, you can check it from here

PHPUnit Error: Call to undefined method Tests\Unit\ExampleTest::visit()

This is the test case I've written and when i try to run it with this command vendor/bin/phpunit on linux it gives me the error "Call to undefined method Tests\Unit\ExampleTest::visit()"
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->visit('/Login')
->type('KP123#gmail.com','email')
->type('123456','password')
->press('Login')
->seePageIs('/home')
->see('Katy Perry');
}
}
I've tried running composer update and it still could not work. Had Anyone experienced this issue before?
The visit() method looks like it's part of the SeleniumTestCase. So my guess is you either extend from the wrong base class. I assume it should look something like this:
class ExampleTest extends \PHPUnit_Extensions_Selenium2TestCase
{
// your test method
}
edit: I just noticed the Laravel tag, so it's probably more something like Illuminate\Foundation\Testing\TestCase. If this gives you errors that it can't find the class you have to set the bootstrap file in your phpunit.xml to the vendor/autoload.php or another appropriate bootstrap file where the file is registered in the autoloader.
I might have come in late onto this question but here is my 2 cents. I have been trying to use the same methods with my test case which led to a dismal failure.
I the realised that I could actually make use of the TestResponse class which is found on the Foundation namespace that wraps many methods around the response object.
$this->get('/url/to/visit'); //Then you can make your assertions on the returned response.
In my case I had authentication turned on for the application therefore it always complained about the header view where we display the username of the currently logged in user. If you run into that situation then use the Auth Facade like shown below:
\Auth::loginUsingId(1);
You should now be able to assert that your response has some text in it. Hope this helps. By the way I am on Laravel 5.4
If you are using new version of Laravel you must use Laravel Dusk to use similar methods to $this->visit or see. It was excluded from Laravel around version 6.
Instead you can use, but it has limited options:
$this->get('/Login')
composer require laravel/browser-kit-testing --dev
Try this
if you are using > Laravel 8 try :
$this->get('/Login')
instead of visit function, and assertSeeText instead of see

Call to undefined method Illuminate\View\View::make()

I am using Laravel 5.4.16 While using make method of View class i am getting undefined method error.
public function Index()
{
return View::make('stats');
}
Try to add this to the top of the class:
use View;
If it doesn't work then it looks like you didn't install the project. You need to run composer install or composer update command which will download and install all dependencies into the vendor directory.
Another thing to check is config/app.php should have this line:
'View' => Illuminate\Support\Facades\View::class,
Alternatively, you could use helper:
return view('stats');
But it will work only if porject is installed properly.
return view('path.to.your.directory');
You are receiving this error because make() method doesn't exist anywhere in your code. If you want to go from controller to view you can use simply:
public function Index() {
return view('stats');
}
It will go to your status blade view by accessing your url request.
Just use Facade instead. Check how it works and then check what is wrong with View first. And I hope you are not using another template engine as Twig for example, cause if yes then you have definitely use Facade or check how the View factory is working, I think it requires template engine in the constructor ...
Just use use Illuminate\Support\Facades\View;
You can try to use it:
return view('stats');

What is the proper way to get ready a 3rd party package in Laravel 5 (5.4)?

I try to migrate an existing project from laravel 4 to laravel 5.
For that I installed a fresh laravel project and import code in it.
I installed a required package in laravel 5:
composer require jenssegers/agent
When I call Agent class to use, laravel gives following error:
Class 'App\Http\Controllers\Agent' not found
What is "use ..." line to add at the top of controller? Or any other solutions?
Not: use Agent; results in Class 'Agent' not found error
Since the Agent is a facade, you should use full namespace:
$agent = \Agent::....;
Or add use clause to the top of your controller:
use Agent;
You need to add facades in app/config/app.php
Laravel (optional)
Add the service provider in app/config/app.php:
'Jenssegers\Agent\AgentServiceProvider',
And add the Agent alias to app/config/app.php:
'Agent' => 'Jenssegers\Agent\Facades\Agent',
Source: https://github.com/jenssegers/agent

Laravel JWTAuth Custom User Model

I'm developing web using Laravel and JWTAuth with default user model at
App\User
Now i'm changin the path of my models to
App\Models
App\Models\User
and i change the jwt config to this
'user' => 'App\Models\User',
but when i try to access my login page, it's show error like this
FatalErrorException in EloquentUserProvider.php line 126:
Class 'App\User' not found
what happening with this? i've changing the setting to App\Models but it's still load to App\ directory?
I re-serve the php artisan, and do composer dump-autoload
It's the same error.
I've resolved my error,
there's one config called auth.php that must updated too
Thanks

Resources