Laravel PhpStorm autocomplete - laravel

I am having problems with autocompletion in PhpStorm using Laravel. I have set up the IDE accordingly from this guide:
https://confluence.jetbrains.com/display/PhpStorm/Laravel+Development+using+PhpStorm
I cannot autocomplete basic functions like Input::only or Input::has. The closest answer I can find is in this thread:
Laravel Intellisense / autocomplete with PhpStorm
However, Input is already added as alias in config/app.php - still not working.
Anyone experienced same problem and/or know a solution to this?
EDIT:
Sorry for not providing code example - it's only been some Laravel trial and error, but here goes:
I have the route:
Route::post('/login', 'LoginController#authenticate');
In the action of the controller I have tried the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class LoginController extends Controller
{
public function authenticate(Request $request) {
Input::get(); // <-- autocompletes
Input::has(); // <-- NO autocomplete
$request->only(); // <-- autocompletes
$request->validate(); // <-- NO autocomplete
}
}

use laravel ide-helper package
it can provide accurate autocompletion. Generation is done based on the files in your project.
phpstrom support auto-complete best thing is that

Related

How to use old Laravel routing style in Laravel 8

I just installed Laravel 8 and in this version, I have to type my routes like this:
Route::get('/admin/panel', [App\Http\Controllers\Admin\PanelController::class, 'index']);
But I got used to Laravel 5 routes which looked like this:
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/panel', 'Admin/PanelController#index');
});
So how can I use this Laravel 5 routing inside Laravel 8 version?
If you're wanting to continue using the "older" way of defining a route (i.e. Controller#action) then you can do so but you need to alter the RouteServiceProvider to include the App\Http\Controllers namespace.
This is pretty straight forward and is with the more recent versions of Laravel 8 a simple case of uncommenting the following line:
protected $namespace = 'App\\Http\\Controllers';
If the version of Laravel 8 you're using doesn't have this line in the RouteServiceProvider file, you could upgrade your Laravel version or manually add it. If you manually add the line, you will also need to update the Route definitions in the boot method to use the $namespace property. Again, this is very straight forward, just add the following to the web and api definitions:
->namespace($this->namespace)
So for example:
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Then you should be good to go.
You can still use it to some extend (e.g. grouping and prefixing a route) but because you're using a FQCN the namespace is redundant. The major advantage using the "new" routing over of the "old" one is refactoring. When you move or rename your controller with an IDE that supports refactoring like PhpStorm you wouldn't need to manually change the name and group namespace of your controller.
In Laravel 5 you were able to use this route notation too but it wasn't outlined in the documentation.
To achieve a similar feeling, import the namespace and use just your class names and get rid of the namespace-group in the definition.
use App\Http\Controllers\Admin\PanelController;
Route::prefix('admin')->group(function () {
Route::get('panel', [PanelController::class, 'index']);
});
If you just cannot live with the new way of defining routes, uncomment the $namespace property in app/Providers/RouteServiceProvider.php and you're back to the old way.

Error when trying to view page, following error: Action Facade\Ignition\Http\Controllers\ShareReportController not defined

I am getting the error below when trying to access the route, but the controller is needed to load the data:
(1/1) InvalidArgumentException
Action Facade\Ignition\Http\Controllers\ShareReportController not defined.
I am using Tenancy/Multi-Tenant package and I have configured it to use routes/tenants.php to load routes specifically for tenants. If I do the following in the tenants.php file, it returns the proper response.
Route::get('/test', function() {
return 'Test success';
});
though when I try to do the same, but loading the data from a controller such as this:
Route::get('/testt', 'TenantController#testt');
It will show the error:
(1/1) InvalidArgumentException
Action Facade\Ignition\Http\Controllers\ShareReportController not defined.
If i try to put the same code in web.php routes, then it works perfectly. What could be the problem? Is it something in my code? Can it be because of the multi-tenant package i'm using? How would i go about further debugging this?
Can you see if your routes are cached and try clearing that cache. Just clear project route-cache using route:clear
The fix was to group the routes in tenants.php with the web middleware and a namespace:
Route::middleware('web')->namespace('App\Http\Controllers')->group(function() {
//Routes
});
Try composer dump-autoload -o
it helped for me.
After some minutes trying to fix I found the solution.
You don't need to group the routes if you've done in RoutesServiceProvider or in a custom Provider.
Just go to config/tenancy.php and go to routes -> path, Remove the base_path() function and let the string:
'path' => base_path('routes/tenants/tenants.php'),
to
'path' => 'routes/tenants/tenants.php',
And this error should be fixed.
I had a similar error after install laravel/passport 8.1 in Laravel 6.2:
Action Facade\Ignition\Http\Controllers\ExecuteSolutionController not defined.
Fixed it runnig composer update. The result was:
Updating facade/ignition (1.13.0 => 1.13.1):
For people finding this via Google: I had a similar error with Laravel 6.5. I had messed up my AppServiceProvider with an incomplete Git merge:
<?php
namespace App\Providers;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
<<<<<<< HEAD
Blade::if(
'iscurrentroute',
function ($route) {
$route = Str::replaceFirst(Request::getSchemeAndHttpHost().'/', '', $route);
return Request::is($route);
}
);
=======
Blade::component('components.sortable', 'sortable');
>>>>>>> feature/WLI-58-bedrijf-beheren
}
}
Deleting the "='s", "<'s", and ">'s", and the double use of Blade fixed it for me.
on server side I went to /stoage folder and cleared cashs. E.g. views folder inside that /storage. Then additionally changed all entire folders' and files' permissions to can read and write.
Then pages started show up as expected
I was using the Ignition error pages in Laravel and I have to say that I much prefer the whoops package.
I had the same error reported in this question and by chance changing the error package installed in my app to the whoops one showed me the real error my app was having and I was then instantly able to resolve it. So it seemed that ignition wasn't exactly the cause but it was hindering me resolving another issue.

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

How to get PhpStorm to autocomplete facades in blade-files

When using #if (Auth::check()), PhpStorm doesn't recognize the Auth.
How do I tell PhpStorm that Auth is \Illuminate\Support\Facades\Auth?
Tested:
#php
use Illuminate\Support\Facades\Auth;
/** #var \Illuminate\Support\Facades\Auth Auth */
class Auth extends \Illuminate\Support\Facades\Auth {}
#endphp
#use(\Illuminate\Support\Facades\Auth)
neither worked, still get "Undefined Class Auth"
Edit 1:
the class Auth extends \Illuminate\Support\Facades\Auth {} line works if it's in another file, for example, the "_ide_helper.php", having it inside the blade file doesn't work.
IDE won't recognize methods accessed via the facade. laravel-ide-helper is a popular package that solves this problem. It generates a custom helper file that the IDE understands. This is not a complete solution but it covers most of laravel classes and helps with autocompletion. Here are your options.
Download and drop the latest _ide_helper.php file for laravel into your project from https://gist.github.com/barryvdh/5227822
Install the laravel-ide-helper package and let it generate a helper file on the fly. https://github.com/barryvdh/laravel-ide-helper
I'd personally suggest installing the package.
To get the right class use #if (\Auth::check())!

Laravel 5 Can't Use User Class

I'm new to Laravel. I just installed Laravel 5 and have been going through some video tutorials in order to learn how it works.
I want to use the User class as it seems like it is very useful. In my web.php (routes) file, I have:
use app\Models\User;
Route::get('/users', function (){
$users = User::all(); //select * from users
return $users;
});
I get the following error upon loading that route in my browser:
I have a User.php file placed in my app/Models/ directory, and also changed in my auth.php file for it to reference this.
There are countless questions like this online and I try the fixes but I can't seem to get it to work. Any ideas?
Thanks!
Remove Models from use statement. It's not directory... Model is in namespace App so use:
use App\User;
Namespace and Directory is completely different things...
Or if you want to use in directory app/Models go to User.php model file and change namespace from App to App\Models

Resources