Laravel 5.2.2 and Entrust error call to undefined method - laravel

When I am using latest Laravel 5.2.2 and Entrust ("zizaco/entrust": "5.2.x-dev") I face this error and not sure how to solve this:
Call to undefined method Zizaco\Entrust\EntrustServiceProvider::hasRole()
I tested this code on HomeController.php
use Entrust;
class HomeController extends Controller
{
public function index()
{
if (Entrust::hasRole('admin')) {
echo "string";
}
return view('home');
}
}
This is my config/app.php service provider
Zizaco\Entrust\EntrustServiceProvider::class
config/app.php facade alias
'Entrust' => Zizaco\Entrust\EntrustFacade::class
I also already generate the model needed
Did I miss something here?

I have same issue, here are the steps I have taken to solve the issue
In your .env file change to cache array
CACHE_DRIVER=array
and dont forget to run
php artisan config:cache

It seems all the steps is correct, and I just need to clear the cache with php artisan config:cache
And if you face an error like below
BadMethodCallException in vendor\laravel\framework\src\Illuminate\Cache\Repository.php line 380:
This cache store does not support tagging.
You need to change in .env this line to array
CACHE_DRIVER=array

Try this:
Open environment file of your laravel change CACHE_DRIVER=file to CACHE_DRIVER=array and save.
Now try your CLI command.

Laravel drivers does not support tagging. To solve this, go to your .env file and change
Cache_driver=file
to
Cache_driver=array
and run
php artisan config:cache

Related

dump($_ENV) not working in laravel version 7x

Here the code
Controller :
public function index()
{
dump($_ENV);
}
Route :
Route::get('/', 'HomeController#index');
I can't display env file in the screen
Try:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
chek:
{{ env('APP.ENV') }}
your doing things in wrong way try this
env('APP_ENV');
getenv('APP_ENV');
if your using cache then you can access the value from config like this .
config('app.env');
or using facede
Config::get('app.env');
If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes "E" in the string.

Laravel after upgrade, .env variable Undefined index

I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Try using the env helper instead, env('API_DOMAIN')
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env() helper outside of config files, because when you cache your config, env() will return null.

Service Provider not booting in Laravel 5.8

I've created a new service provider to observe a model (App\Providers\EloquentEventServiceProvider.php), like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Staff;
use App\Oberservers\StaffObserver;
class EloquentEventServiceProvider extends ServiceProvider
{
public function boot()
{
Staff::observe(StaffObserver::class);
}
}
I've also added it to the config file (config\app.php):
return [
...
'providers' => [
...
App\Providers\EloquentEventServiceProvider::class,
...
]
...
]
The observer methods aren't working though. If I move Staff::observe(StaffObserver::class); to the AppServiceProvider class, it works just fine. So clearly this is an issue with getting my service provider to boot. I've tried php artisan config:clear, php artisan clear-compiled, composer update and composer dump but none have work. Any help is greatly aprpeciated.
your Oberservers name is wrong, as mentioned in the laravel doc observers laravel doc it should be Observers which means all of your observers should be within App\Observers instead of App\Oberservers.
so here we have 2 solutions :
1- if you want to keep the namespace App\Oberservers, you should run these 2 commands below because autoloading of the files may not work properly because we created a new folder Oberservers:
# Autoloading of files
composer dump
# Configure the cache
php artisan config:cache
2- the second solution is to just rename your actual Oberservers folder to Observers, in that way the autoloading of files will work well.

Laravel db:seed does not work without --class parameter

Running php artisan db:seed does not work for some reason on my Laravel 5.6 project.
The command runs (quietly) even without a database
Does not return any error on the terminal
However, when I run php artisan db:seed --class=ClassNameTableSeeder it works. What could be the cause of such a weird behavior?
NB : Similar to questions like 39521913 but not a duplicate.
This is because by default DatabaseSeeder does nothing. Original code in fresh Laravel project looks like this:
public function run()
{
// $this->call(UsersTableSeeder::class);
}
So to run any database seeder, you should uncomment this line and put valid class name, so for example:
$this->call(ClassNameTableSeeder1::class);
$this->call(ClassNameTableSeeder2::class);
and so on to run seeders for each class you put here.

Laravel - accessing .env variables

I tried to get the environment variable from the .env in my root with
Route::get('/test', function () {
return "value is". getenv('APP_ENV');
});
and
Route::get('/test', function () {
return "it is". env('APP_ENV');
});
It is in .env
APP_NAME=Laravel
APP_ENV=local
How can I get access to it?
With Laravel, you should avoid environmental variables outside of your configuration files.
In your config files, you can use environmental variables, example in config/app.php:
'env' => env('APP_ENV', 'production'),
Then you can access this using the config helper: config('app.env').
This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached.
Route::get('/test', function () {
return "it is".config('app.name');
});
use env('ENVKEY')
Don't forget to clear cache sometime it cause because of cache.
php artisan config:clear
php artisan cache:clear
composer dump-autoload
For more info look at the doc
just run this commands in cmd.
php artisan config:cache
then
php artisan config:clear
then
php artisan cache:clear
As per the Laravel Documentation on Environment Configuration,
All of the variables listed in this file will be loaded into the $_ENV
PHP super-global when your application receives a request. You may use
the env helper to retrieve values from these variables.
So, it is possible to access the variable as
$_ENV['envKey'];
laravel provides a global helper function for this kind of task
$val = config('app.something');
you can also set new values with the following method
config(['app.something' => 'cat']);
reference
for your particular task it would be
$val = config('app.env');
or to determine the env globally
$environment = App::environment();
i hope this helps, have a nice one!
App::environment()
try this please

Resources