PHPStorm does not recognize methods of my Model class in Laravel 5.0

the data could not be inserted into the database, and the entire query class and model class method were not found in the IDE (phpStrom), how can I solve it?

here is my extended class (Post.php) here shows the error in the latter and where is the method:

<?php namespace App; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $fillable=[ 'title', 'description', 'location', 'contact', 'type', 'published_at' ]; protected $date=['published_at']; public function setPublishedAtAttribute($date) { $this->attributes['published_at'] = Carbon::createFromFormat('Ym-d', $date); } /** * @param $query */ public function scopePublished($query) { $query->where('published_at', '<=', Carbon::now()); } public function scopeUnPublished($query) { $query->where('published_at', '>=', Carbon::now()); } /** * An post is owned by a user. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user(){ return $this->belongsTo('App\User'); } } 

and here is my controller class where I use it:

 <?php namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Requests\CreatePostRequest; use App\Post; use Request; use Illuminate\Support\Facades\Auth; use Session; class PostsController extends Controller { // public function __construct() { $this->middleware('auth'); } public function index() { //return \Auth::user()->name; $posts = Post::latest('published_at')->published()->get(); $latest= Post::latest()->first(); return view('tolet.index', compact('posts','latest')); } /** * @param Post $post * @return \Illuminate\View\View * @internal param Articles $article * @internal param Articles $articles */ public function show(Post $post) { return view('tolet.show', compact('post')); } public function create() { if (Auth::guest()) { return redirect('tolet.index'); } return view('tolet.create'); } /** * @param CreatePostRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function store(CreatePostRequest $request) { //validation $this->createPost($request); // flash('Your tolet has been created!')->important(); return redirect('tolet.index'); } /** * @param Post $post * @return \Illuminate\View\View * @internal param Articles $article */ public function edit(Post $post) { return view('tolet.edit', compact('post')); } /** * @param Post $post * @param CreatePostRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector * @internal param Articles $article * @internal param $id */ public function update(Post $post, CreatePostRequest $request) { $post->update($request->all()); return redirect('tolet.index'); } /** * sync up the list of tags in the database * * @param Post $post */ /** * save a new post * * @param CreatePostRequest $request * @return mixed */ private function createPost(CreatePostRequest $request) { $post = Auth::user()->posts()->create($request->all()); return $post; } } 
+16
php phpstorm eloquent laravel model
source share
10 answers

If you want a class that extends Model to recognize Eloquent methods, just add the following class to the top PHPDoc comment:

 @mixin Eloquent 

Example:

 <?php namespace App; use Carbon\Carbon; use Eloquent; use Illuminate\Database\Eloquent\Model; /** * Post * * @mixin Eloquent */ class Post extends Model { 

Note: Most of you probably use ide-helper for Laravel, so this @mixin attribute @mixin automatically created for model classes.

+31
source share

Since the where , latest , find , findOrFail and other methods do not exist in the Model class, but are loaded in the Builder using magic methods, the IDE cannot detect them.

While the widely suggested laravel-ide-helper is excellent, it does not help . There are several issues and discussions and workarounds, but all have their own problems.

The best solution I've found so far, IMHO, is to lower the severity level if __magic methods are present in the class . PhpStorm has this exact option in the scan settings.

Check-in Settings -> Inspections -> PHP -> Undefined -> Undefined method This will not allow you to click on this method, but just turns off annoying markup. Learn more about seriousness or check out this more expressive answer SO <

+24
source share

For those who came here for a solution, a solution worked for me in this Karu:

PhpStorm laravel 5 method not found

especially when I was running:

Edit: to use this command, you need to install ide-helper , run:

 composer require --dev barryvdh/laravel-ide-helper 

...

 php artisan ide-helper:models 

and answered yes

after that, the methods are recognized.

+9
source share

My class. Annotations will help PhpStorm recognize these methods.

 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Query\Builder; /** * @method static Builder where($column, $operator = null, $value = null, $boolean = 'and') * @method static Builder create(array $attributes = []) * @method public Builder update(array $values) */ class User extends Model { protected $table = 'users'; protected $fillable = [ 'email', 'name', 'password', ]; } 
+5
source share

A bit annoying to add to all your models, but you can add this method to your docblock models. This will make it work correctly in PHPStorm.

 /* * @method static \Illuminate\Database\Query\Builder|\App\MyModelName where($field, $value) */ 
+2
source share

I am new to laravel and all these problems with models and phpstorm are very strange. This is a big flaw. Solutions like adding @mixin Eloquent or working php artisan ide-helper: models didn't work for me. PHPStorm does not find Eloquent or Reddish. ide-helper: models do not add all the useful static methods. So I came up with my own base model, which contains php doc with all the relevant model methods:

 <?php namespace App; use Closure; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Query\Builder as QueryBuilder; /** * Class BaseModel * @package App * @method EloquentModel|Collection|null static $this find($id, $columns = ['*']) Find a model by its primary key. * @method EloquentModel|EloquentBuilder|null first($columns = ['*']) Execute the query and get the first result. * @method EloquentModel|EloquentBuilder firstOrFail($columns = ['*']) Execute the query and get the first result or throw an exception. * @method Collection|EloquentBuilder[] get($columns = ['*']) Execute the query as a "select" statement. * @method mixed value($column) Get a single column value from the first result of a query. * @method mixed pluck($column) Get a single column value from the first result of a query. * @method void chunk($count, callable $callback) Chunk the results of the query. * @method \Illuminate\Support\Collection lists($column, $key = null) Get an array with the values of a given column. * @method LengthAwarePaginator paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) Paginate the given query. * @method Paginator simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page') Paginate the given query into a simple paginator. * @method int increment($column, $amount = 1, array $extra = []) Increment a column value by a given amount. * @method int decrement($column, $amount = 1, array $extra = []) Decrement a column value by a given amount. * @method void onDelete(Closure $callback) Register a replacement for the default delete function. * @method EloquentModel[] getModels($columns = ['*']) Get the hydrated models without eager loading. * @method array eagerLoadRelations(array $models) Eager load the relationships for the models. * @method array loadRelation(array $models, $name, Closure $constraints) Eagerly load the relationship on a set of models. * @method static EloquentBuilder where($column, $operator = null, $value = null, $boolean = 'and') Add a basic where clause to the query. * @method EloquentBuilder orWhere($column, $operator = null, $value = null) Add an "or where" clause to the query. * @method EloquentBuilder has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null) Add a relationship count condition to the query. * @method static EloquentBuilder find($value) * @method static EloquentBuilder orderBy($column, $direction = 'asc') * @method static EloquentBuilder select($columns = ['*']) * * * @method static QueryBuilder whereRaw($sql, array $bindings = []) * @method static QueryBuilder whereBetween($column, array $values) * @method static QueryBuilder whereNotBetween($column, array $values) * @method static QueryBuilder whereNested(Closure $callback) * @method static QueryBuilder addNestedWhereQuery($query) * @method static QueryBuilder whereExists(Closure $callback) * @method static QueryBuilder whereNotExists(Closure $callback) * @method static QueryBuilder whereIn($column, $values) * @method static QueryBuilder whereNotIn($column, $values) * @method static QueryBuilder whereNull($column) * @method static QueryBuilder whereNotNull($column) * @method static QueryBuilder orWhereRaw($sql, array $bindings = []) * @method static QueryBuilder orWhereBetween($column, array $values) * @method static QueryBuilder orWhereNotBetween($column, array $values) * @method static QueryBuilder orWhereExists(Closure $callback) * @method static QueryBuilder orWhereNotExists(Closure $callback) * @method static QueryBuilder orWhereIn($column, $values) * @method static QueryBuilder orWhereNotIn($column, $values) * @method static QueryBuilder orWhereNull($column) * @method static QueryBuilder orWhereNotNull($column) * @method static QueryBuilder whereDate($column, $operator, $value) * @method static QueryBuilder whereDay($column, $operator, $value) * @method static QueryBuilder whereMonth($column, $operator, $value) * @method static QueryBuilder whereYear($column, $operator, $value) */ abstract class BaseModel extends Model { } 

Then my own models expand this model:

 <?php namespace Modules\Shop\Entities; use App\BaseModel; class MyEntity extends BaseModel 

And then everything works. BaseModel is now not complete, feel free to add additional static methods, adding them on demand.

+1
source share

You can add @mixin QueryBuilder in phpdoc Model Class

File Path: project_path\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php

+1
source share

I found a solution that worked and was simple after trying Barry's _ide_help.php solution. Laracast video showing solution: https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15 - In the first comment, you can find Barry links.

After I added this, it did not work for me, but I still mention it for completion.

Then I tried this:

In my model, I added use Eloquent; up. (I added Eloquent by auto completion instead of input).

Then, above my class, I typed "/ ** hit ENTER", which automatically generated PHP documents in recently generated PHP documents, I added @mixin Eloquent at the bottom.

As a last step, I pressed Ctrl + Alt + Y (default settings), which synchronizes (File-> Synchronize) in PhpStorm.

This fixed the warnings, and the my :: find method in my controller was found, and autocomplete worked.

For example, below my class:

 namespace App; use Illuminate\Database\Eloquent\Model; <br> use Eloquent; /** * Class Student * @package App * @mixin Eloquent */ class Student extends Model <br> { } 
+1
source share

Just so that this question can be "answered", you need laravel ide-helper. Follow these instructions and everything should work for you.

0
source share

Consent and +1 @rutter . I would add that this problem is constantly on my face, as I concentrate on Laravel projects.

Barry Laravel-IDE Git is great for "stitching" methods, it cannot really capture every problem, this happens a lot with the scope of the Laravel provider package (which is later called through other classes / methods ..)

I throw the ball there, but if intelliJ creates a compiler that can try / catch the given magic methods (after installing them) just click on one button (return bool and the route to the method is successful) well ... that would be great.

Disabling seriousness is a cool way to handle this (worked out above), but you are still stuck without a simple combination of the method, or even letting you know that it exists.

Structurally, I would suggest that they force this sync button to mean something (other than updating).

0
source share

All Articles