PhpStorm autocomplete with \Nwidart\Modules - laravel

The problem would be that I use PhpStorm IDE. The Laravel project is managed by nwidart/laravel-modules.
However, PhpStorm does not handle basic Laravel functions. For example: findorfail(), for own models:
Method 'findorfail' not found in \Modules\Companies\Models\Companies_adresses
I did the following to enable autocomplete:
install https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facades
Enable PhpStorm plugins:
https://plugins.jetbrains.com/plugin/13441-laravel-idea
https://plugins.jetbrains.com/plugin/7532-laravel
It is very confusing because it does not give tips correctly. It does not list database columns, etc... PHPDoc still came to mind, but I don't know how to get started!
You have no ideas? I feel completely lost :(

I set up the Laravel IDE Helper but I had to add the following into all my models...or you could add to a new Model class that you then extend to all of your models. Either way, this will get PHPStorm to read the facade methods like that correctly:
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* Class Employee
*
* #mixin Builder
*/
class Employee extends Model
{
// blah blah blah
}
Make sure the PHPDoc bumps up against your model. The key bit is the #mixin line which gives PHP Storm a connection to all the facade methods for your model. You will still need the IDE Helper as well. But with this PHPStorm will suggest as you type and allow you to control-click to view the method itself from elsewhere in your project.

You also can generate advanced ide helper code with Laravel Idea plugin. main menu > Code > Laravel Idea code generation > Generate Eloquent Helper code.

Related

Undefined type 'Laravel\Cashier\Cashier'.intelephense(1009)

I am new to laravel I want to integrate subscription module in my project as I followed the following process till now.
composer require laravel/cashier
php artisan vendor:publish --tag="cashier-migrations"
Update User Model
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
.env
CASHIER_MODEL=App\Models\User
Need suggestions where I am going wrong.
I also had the same problem. I closed my IDE and restarted it and the error was gone. - sharad paudel
EDIT: I can confirm that restarting VS Code is a solution to this problem. I'm also using Laravel / Cashier, and I came across a similar situation. VS Code was giving me the red squiggly lines inside the actual Billable trait file: src/vendor/laravel/cashier/src/Billable.php
similar to the OP. It seems like it isn't an "error" so much as a minor confusion in VS Code - I had all the correct "use" import lines.
After restarting VS Code, all file associations seemed to be fine, and all the red squiggles were gone.
I have also seen this happen before, after installing something with "composer", a restart was needed to make VS Code happy. -dogatonic
Add Billable after using the comma (,); use HasApiTokens, HasFactory, Notifiable. Write it like
use HasApiTokens, HasFactory, Notifiable, Billable;

findOrFail() method not found by PhpStorm

PhpStorm does not found findOrFail() method. When I try to call it, there isn't auto-complete and the result is always fail. The user found is always on the message.
I try to use Laravel Ide helper and query()->findorfail but I didn't resolve.
The result:
in phpstorm try the Laravel plugin to generate the IDE classes.
please check your User model. It must extend Model class like this.
use Illuminate\Database\Eloquent\Model;
class Lead extends Model {
I guess your User model is missing this "extends Model".
As I know, PHPStorm never detects the findOrFail method even we already installed the Laravel plugin.
But you use it wrong because this method takes an id and returns a single model. If the method can't find any matching data, it will return an error.
You can change the method into:
User::findOrFail($user->id);

How to override any Model of vendor folder in laravel

I am using MongoDB as database so I need to change model of package tzsk\payu as using original is giving me following error
Call to a member function prepare() on null
I tried excluding original model and overriding using composer it doesn't work.
The only way would be if the package offered the option to use a custom model, like Passport is doing.
As far as I can see, there does not seem to be a way to do that. Thus you'd need to fork the package and edit the Model yourself.

laravel id-helpeClass 'App\Models\Eloquent' not found

i'm just started to use laravel ide-helper in phpstorm and wanted to document my models.
I've run the command php artisan ide-helper:models but comes up with the error:
'App\Models\Eloquent' not found
After reading it looks as if the error lies in what my models extend - my model class looks like this:
<?php namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
class Article extends Model { }
This is the standard set in the Laravel docs and created by the generators.
Is there a way to resolve this issue so I can generate the docs with the helper or do I leave as is.
Bit confused
Thanks

Class autoloading in Laravel 4.1

I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus
I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.
<?php (Stack.php)
class Stack extends Eloquent
{
}
$stacks = Stack::all();
However when I run this model I get the following error message.
Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4
Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.
Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?
I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)
Some things I have checked/tried to fix the problem.
Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).
Thanks in advance.
Make sure you are accessing the application from the correct 'entrance'.
Thus, accessing it from app/public/index.php.
The app/public/index.php file loads the autoloader.
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';
It's possible your is namespacing. Try adding the backslash before the class being extended.
class Stack extends \Eloquent
{
}
Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)
Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model
class Stack extends \Illuminate\Database\Eloquent\Model {}

Resources