findOrFail() method not found by PhpStorm - laravel

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);

Related

How to attach middleware to an existing named route from a package in laravel 5?

I'm trying to extend an existing application without modifying its source code.
The application has a named route called wizard-add.
Is there a way to register \MyPackage\MyMiddleware with the existing route?
I tried attaching it via Route::getRoutes()->getByName('wizard-add')->middleware(\MyPackage\MyMiddleware::class); but since packages are registered before the routes are read, Route::getRoutes() returns an empty collection.
Thank you!
Since I didn't find a way to solve this, I extended the app's controllers and put my logic inside.
namespace MyPackage\Controllers;
use App\Controllers\WizardController;
class MyPackageWizardController extends WizardController { ... }
and then in my service provider's register() method:
$this->app->bind(WizardController::class, MyPackageWizardController::class);
So everytime the app attempts to instantiate WizardController, it instantiates MyPackageWizardController instead. I don't know if there are any drawbacks but so far this has been working perfectly for me.

Laravel 7 Api incomplete?

I started with Laravel 7 a few weeks ago. It happened to me multiple times that after reading about a topic on the Laravel website, I wanted to check the details of a function, for example:
Illuminate\Support\Facades\Route::group()
So I went to the Laravel API, and could find the Route facade, but not the group function.
What am I doing wrong? Where do you check for example the exact signature of a function?
Thanks!
The method group in Route::group() is inherited from another class, RegistrarGroup.
See the docblock method in the source file, vendor/laravel/framework/src/Illuminate/Support/Facades/Route.php:
#method static \Illuminate\Routing\Router|\Illuminate\Routing\RouteRegistrar group(\Closure|string|array $attributes, \Closure|string $routes)
so, this is what you look for in the API documentation:
https://laravel.com/api/7.x/Illuminate/Contracts/Routing/Registrar.html#method_group
That is because a Facade, by definition, is only an 'interface' to the methods exponed by another object, so you will not find the actual methods available by visiting the facade code.
Usually you can find the actual class that a facade resolves to (when not mocked) by checking the docblock in the source code and navigate to that class.
A very useful tool to overcome this problem and provide autocompletion (and inspection) for facades on your IDE is the package https://github.com/barryvdh/laravel-ide-helper

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.

PhpStorm autocomplete with \Nwidart\Modules

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.

Laravel Auditing AuditableTransitionException Error on Morphmap on transitionTo()

I am able to successfully record changes to classes, and return what has been changed (not including many-to-many). However, I am unable to revert back any change using the built-in new transitionTo() method.
I get the following error on all classes:
Expected Auditable type App\XYZ, got XYZ instead
I have all of my morphable classes (which are all I am using for tracking audits) attached correctly within AppServiceProvider in a morphmap like so:
\Illuminate\Database\Eloquent\Relations\Relation::morphMap([
'Employee' => \App\Employee::class,
];
All classes work correctly with all other Laravel morphTo methods.
The auditable code looks like it is tripping the error in line 467 of the Auditable class:
if (!$this instanceof $audit->auditable_type) {}
It doesn't appear to be looking to the map for any of the morphed classes. Or, I could be totally missing something of course!
Any help on how to get this to work using the auditing method -- has anyone gotten this to work with standard morph classes? (It will of course revert the class manually by looping on the old fields and saving the object).
Using Laravel 5.5 and the latest version (5.0) of Laravel-Auditing.
Sent a note to the developer - this was in fact a bug. Vendor code was needed to include morphMapped objects.
Developer at Laravel Auditing responded within an hour and had a fix a few hours later. I can confirm this is now functioning as expected. Outstanding support.

Resources