Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent ORM Code Hinting in PhpStorm

So I'm just starting off with Laravel (using v5) and Eloquent. I'm working on getting some basic APIs up and running and noticing that a lot of working methods don't show up in PhpStorm's code hinting

So I have this model:

namespace Project\Models;  use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;  class User extends Model      implements AuthenticatableContract, CanResetPasswordContract { } 

And in one of my controllers I try to do

User::query()->orderBy('id', 'desc'); 

User::query() creates a Eloquent Builder object and orderBy() behave properly and without error. However, PhpStorm does not show orderBy() (or take(), skip(), and I'm sure others) when I type User::query()-> and gives warnings when I actually do use it.

I am using Laravel IDE Helper which has helped immensely with bringing code hints to the Facades, but not to the models/builders it would seem.

Does anyone have a solution to this?

like image 800
Josh Avatar asked Apr 03 '15 21:04

Josh


People also ask

Is PhpStorm good for Laravel?

PhpStorm provides full support of the Laravel Blade template engine. It highlights various Blade syntax constructs, as well as any HTML, JavaScript and CSS code inside the templates. Besides syntax highlighting, PhpStorm provides several other Blade-specific features.

What is eloquent ORM in PHP?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

How does eloquent ORM work?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Before getting started, be sure to configure a database connection in config/database.

Is Laravel eloquent ORM?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database.


2 Answers

Add in model PHPDoc@mixin

/**  * Class News  * @property int $id  * @property string $created_at  * @property string $updated_at  * @mixin \Eloquent  * @package App  */ class News extends Model {  } 

In PHPStorm works

like image 44
Денис Чорний Avatar answered Oct 08 '22 13:10

Денис Чорний


For future Googlers, and perhaps OP as well if you are still sticking to Laravel.

The laravel-ide-helper package solves this issue for you quite elegantly, with what I believe is a relatively new feature; generated model PHPDocs.

You can generate a separate file for all PHPDocs with this command:

php artisan ide-helper:models 

The generated metadata will look something like this for each class:

namespace App { /**  * App\Post  *  * @property integer $id  * @property integer $author_id  * @property string $title  * @property string $text  * @property \Carbon\Carbon $created_at  * @property \Carbon\Carbon $updated_at  * @property-read \User $author  * @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments  */ class Post {} } 

This caused issues for me in PHPStorm however, where the software was complaining about multiple class definitions. Luckily an option is readily available for writing directly to the model files:

php artisan ide-helper:models -W 

There are a few more options and settings available if you need to tweak the behavior, but this is the gist of it.

like image 58
Erik Johansson Avatar answered Oct 08 '22 13:10

Erik Johansson