The use statement with non-compound name 'Session' has no effect - laravel

$ php artisan serve
ErrorException
The use statement with non-compound name 'Session' has no effect
at routes/web.php:6
3▕ use Illuminate\Support\Facades\Route;
4▕ use Illuminate\Http\Request;
5▕ use Illuminate\Foundation\Application;
➜ 6▕ use Session;
7▕

The error says that doing use Session in the root namespace does not actually do anything because Session is already in the root namespace.
In reality Session does not exist in the root namespace but is rather an alias defined in config/app.php. Bottom line is when you are in the root namespace already you don't need to do use Session.
While you don't need to do this, what I suggest you do is:
use Illuminate\Support\Facades\Session
Alternatively you can use the session helper function:
session()
and you can also retrieve the session singleton using the application container:
app()->make('session');
This is just because personally I don't like global aliases. Your opinion may differ.

Related

How to use ENV variables in Artisan Commands in Laravel 5.8?

I am creating my own artisan command and I want to use ENV variables, but when I use $_ENV['VariableName'] I get and error.
local.ERROR: Undefined index: VariableName
The same code works perfectly in a controller and error as this one is not being generated.
I am creating my commands with php artisan make:command CommandName
How can I start using ENV variables there? Thank you! I want to use the variables in a private function which is inside:
class CommandName extends Command but outside the public function handle()
Since the .env file is not pushed to the repository, the best approach is to use config files instead. So in the config directory, create your custom file for example: custom.php with the following content:
<?php
return [
'variable' => env('VARIABLE_NAME', 'DEFAULT_VALUE')
];
and in your .env you should put:
VARIABLE_NAME=something
Then to use it you use config('custom.variable');
You can use the Laravel Helper to access environment variables with something like this:
env('VariableName')
you can also specify a default value if the environment variable is not set
env('VariableName', 'myName')
Laravel Docs 5.8 Helpers

BadMethodCallException in Macroable.php line 74 : Method controller does not exist

I'm getting little bit issue when I follow Route Controller.
Web.php Code:
{"
Route::controller('/admin','adminController');
"}
adminController.php Code:
{"
<?php
namespace App\Http\Controllers;
class adminController extends Controller{
public function getDashboard(){
echo " Get Dashborad Method ";
}}
"}
When I hit http://localhost:8000/admin/dashboard
an error:
Display ("BadMethodCallException in Macroable.php line 74 : Method controller does not exist.")
Here is SnapShot:
Please take a look and let me know what is wrong with code.
Please also make sure you don't use any namespace inside your routes file.
E.g. If by mistake your IDE add's
use Illuminate\Routing\Route;
It could result in the same error mentioned above. Your route file (web.php or api.php) should not use the Illuminate\Routing\Route class.
EDIT:
Tested on Laravel 5.5
Faced the same problem recently. Laravel 5.3 does not support Route::controller() method. You need to change it to Route::get().
Please check how to use it here https://laravel.com/docs/5.3/routing#basic-routing.
I got this error while running artisan command. Finally, I solve by removing the,
use Illuminate\Routing\Route
in web.php file.
i have just solved this problem i was using
$table->int('TeachingGroup_id');
where i should use the full integer Not int only by solving this now migration done S

Artisan says class name not defined, even though it is?

I have created the following Artisan command in Laravel:
class draft_cron extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'draft_cron';
//etc
And in start/artisan.php I added:
Artisan::add(new draft_cron);
However, when I try to run the command, I get an error saying Class 'draft_cron' not found and pointing to the above line from start/artisan.php.
Any ideas what the problem might be?
2 things: 1 is what user RDelorier said. DraftCron instead of draft_cron. The other thing is, did you do composer dump-autoload?
Most likely culprit is PSR cant figure out where the class is, any particular reason you used snake_case over StudlyCase?

Importing namespaces in Laravel Tinker REPL

Laravel's Tinker REPL is useful for conveniently experimenting with models, however it does not seem possible to import a namespace, requiring the model namespace to be laboriously typed. For instance, this works:
$ php artisan tinker
[1] $list = new mysweetapp\Todolist;
[2] > echo get_class($list);
mysweetapp\Todolist
This does not:
$ php artisan tinker
[1] use mysweetapp\Todolist;
// false
[2] $list = new Todolist;
[3] echo get_class($list);
Boris\EvalWorker
Is there some way to import namespaces into Tinker or is it just not yet supported? Mind you I definitely want to use namespaces, I just don't want to repeatedly type in the namespace. :-)
Look at this https://softonsofa.com/tinker-like-a-boss-in-psysh/
You can't do this with use namespace in the repl itself, but here's what you may do:
// config/local/app.php
'aliases' => append_config([
'Todolist' => 'Mysweetapp\Todolist',
... // more
]),
Then in your local env you will be able to access your models without typing namespace, and if you're not in local env, then run tinker forcing it:
php artisan tinker --env=local

Custom artisan commands with multiple 'methods' (in the style of migrate:install etc)

I'm creating a custom artisan command (foo) for my Laravel 4 application. I can see from the userguide how to accept arguments and options, e.g.
php artisan foo argument --option
But what if I want to have a custom artisan command that has many 'methods', in the same style as some built-in artisan commands, e.g. migrate:install?. I want to make something like:
php artisan foo:baz argument --option
What is the recommended way to implement different methods based on the : colon separator? All I've found to try so far is to make an entirely new artisan command for each 'method'. Is there a more efficient way?
You're correct, you do need a new Artisan command (or rather a class) for each method. However all you have to do is register each file in app/Console/Kernel.php, if you decide to change the syntax later than Laravel will pick it up any changes to $signature automatically without you needing to rename any files.
For the Laravel example you mention, migrate, there's a directory with a separate file for each command name (including the one that has no colon):
/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations
migrate -> MigrateCommand.php
migrate:install -> InstallCommand.php
migrate:refresh -> RefreshCommand.php
migrate:reset -> ResetCommand.php
migrate:rollback -> RollbackCommand.php
migrate:status -> StatusCommand.php
If you have code you want to reuse (DRY) note that if you examine the above commands, some of them use traits, e.g. Illuminate\Console\ConfirmableTrait – which contains a confirmToProceed() method which, if it's running in production, will ask the user if they really want to continue.
NB: the Artisan syntax changed in 5.1, from $name (with a rather complicated way of specifying arguments and options) to the much simpler $signature, it's backwards compatible. More info
You just have to set the name:
protected $name = 'foo:baz';

Resources