Laravel 5: How to get config value in artisan console command - laravel-5

I get config values in Laravel like this:
$value = config('app.name');
If I try the same in a method, which is called from an artisan command, then I get the following error message:
[ErrorException] Trying to get property of non-object
Is it possible to get config values from an artisan console command?
I have found a solution for Laravel 4, but there is no such method in Laravel 5.

config() helper use application instance and it can be not available in the console. Try this solution Config::get('app.name')

Related

Call to undefined function App\Http\Controllers\Frontend\Auth\access()

After inserting credentials for login, I got this below error in Laravel 6.16.1
Call to undefined function App\Http\Controllers\Frontend\Auth\access()
What that error is saying is that Laravel did not find the method you presume to have declared inside class App\Http\Controllers\Frontend\Auth. Your login method is not named access.
If it is so still, you probably want to run php artisan config:cache and composer du

Error while migrating from Laravel tinker console (sqlite)

Consider
laravel new bug
Then adding in .env
DB_CONNECTION=sqlite
DB_DATABASE=/home/me/Code/bug/storage/database.sqlite
creating database
touch /home/me/Code/bug/storage/database.sqlite
migrating
php artisan migrate && php artisan migrate:fresh
Now trying to migrate programmatically in tinker
php artisan tinker
>> Artisan::call('migrate');
First time resulting in => 0 (as expected ?) But second time:
>>> Artisan::call('migrate:fresh')
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]:
General error: 17 database schema has changed (SQL: select * from
sqlite_master where type = 'table' and name = migrations)'
Artisan::call('migrate')
Third time:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
Any ideas whats going on here? Can you reproduce?
Update using postgres. The second time running Artisan::call('migrate:fresh') I get:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
UPDATE 2: Issue filed here: https://github.com/laravel/framework/issues/22997
https://github.com/laravel/tinker/issues/37
I don't think if this is a laravel bug, but I found your problem:
Calling a non existing command like Artisan::call('migrate:fresh') will crash. The reason is a typo: tinker does not accept fresh, only refresh. Calling the command via tinker will empty the sqllite file so you can't execute any commands until you migrate without tinker.
Here are my steps:
php artisan migrate # sqllite file is filled with content
php artisan tinker
> Artisan:call('refresh')
> Artisan::call('fresh') # now the sqllite file is empty
> Artisan::call('migrate') # will crash
php artisan migrate
> Artisan::('refresh') # works
so I think it's a tinker bug, not a laravel one. Tinker is doing some kind of snapshot (If you change a Model you have to restart tinker to call it there), maybe the error handling is not implemented is the best way to catch all errors.

using laravel 5.5 and get error to create my controller?

I am using laravel 5.5 and I get an error when I create my controller.
$ php artisan make:controller MyDatatablesController
I want to create new controller but I get this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Your command is perfect their is some error in your project check your recent changes in your project
You're having an error on your own code. Probably it's a code not always executed. Try executing the command in verbose mode to have more information about the error
php artisan make:controller MyDatatablesController -vvv

Exception: trim epects parameter 1 to be string, array given

Hello i recently tried to implement JWT authentication in my app by following this tutorial i found online:
cookie-free-authentication-with-json-web-tokens-an-example-in-laravel-and-angularjs
However when trying to install the following package:
barryvdh/laravel-cors 0.4.x#dev
I got an error exception when trying to run php artisan clear-compiled
and later when i tried to run php artisan vendor:publish
This is the error:
[ErrorException]
trim() expects parameter 1 to be string, array given
Edit: I just removed the package and tried running composer update, had the same error again.
Okay i found the error, it had to do with my routes file:
Route::any(['{url?}'], function($url) {
return view('website/index');
})->where(['url' => '[-a-zA-Z0-9/]+']);
I removed the square brackets and now it works, must have accidentally put it there:
Route::any('{url?}', function($url) {
return view('website/index');
})->where(['url' => '[-a-zA-Z0-9/]+']);
Not gonna delete though, somebody might find this useful lol xD xD

Errors while running Artisan in Laravel 4

Whenever i try to run any php artisan commands from the command line, I get the following error(s):
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","me
ssage":"Call to a member function connection() on a non-object","file":"C:\\wamp
\\www\\cms\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Mod
el.php","line":2649}}{"error":{"type":"Symfony\\Component\\Debug\\Exception\\Fat
alErrorException","message":"Call to a member function connection() on a non-obj
ect","file":"C:\\wamp\\www\\cms\\vendor\\laravel\\framework\\src\\Illuminate\\Da
tabase\\Eloquent\\Model.php","line":2649}}
I have specified the Laravel version as 4.1.* in my composer.json but since i can't run artisan, i don't know the exact version.
This problem didn't use to happen before. And the website seems to be running fine, despite of the errors occuring in php artisan. Composer commands also work fine, as far as i can tell.
Any ideas why it is happening now?
This error seems to have happened, because for the mail configuration in app/config/mail.php, I made it to retrieve the values from the database instead of specifying the configuration options directly in that file.
So, I had to check if the function for getting the value is called from the web or from the command line. If the function is called from the command line, I simply returned an empty string.
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
return '';
}

Resources