PHPStorm无法识别Laravel 5.0中我的Model类的方法

Osm*_*hid 17 php model phpstorm laravel eloquent

无法将数据插入数据库,并且找不到IDE(phpStrom)中显示的所有查询类和Model类的方法如何解决?

这是我的扩展类(Post.php)这里显示最新的错误和where方法:

<?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('Y-m-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');
    }

} 
Run Code Online (Sandbox Code Playgroud)

这是我使用它的Controller类:

<?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;
    }


}
Run Code Online (Sandbox Code Playgroud)

Tom*_*ník 33

如果要扩展Model类以识别Eloquent方法,只需添加上面的类PHPDoc注释

@mixin Eloquent
Run Code Online (Sandbox Code Playgroud)

<?php namespace App;

use Carbon\Carbon;
use Eloquent;
use Illuminate\Database\Eloquent\Model;

/**
 * Post
 *
 * @mixin Eloquent
 */
class Post extends Model {
Run Code Online (Sandbox Code Playgroud)

  • 这并不能完全解决我的问题,因为它仍然无法识别静态方法:使用 `Post::where` 会给我以下检查警告:`非静态方法 'where' 不应被静态调用,但类有“__magic”方法。` (8认同)
  • 就我而言,我使用了@mixin Builder并导入了Builder,就像使用Illuminate \ Database \ Eloquent \ Builder;一样。 (5认同)
  • 在 Laravel 6/7 中,你需要更具体:`@mixin \Illuminate\Database\Eloquent\Builder` (4认同)

ruu*_*ter 26

由于方法where,latest,find,findOrFail和其他人不存在Model阶级,但Builder并通过魔术方法被加载,IDE无法检测到这些.

虽然广泛建议的laravel-ide-helper很棒,但它也没有帮助.关于这个问题有很多问题,讨论和解决方法,但都有自己的问题.

到目前为止我发现的最佳解决方案,如果课堂上有__magic方法,恕我直言会降低严重程度.PhpStorm在检查设置中有这个确切的选项.

签入Settings -> Inspections -> PHP -> Undefined -> Undefined method这不会让您单击该方法,但只是禁用恼人的标记.阅读更多关于严重性或检查这个更具表现力的SO答案

  • 另一个选项是`$ model =(new ModelClass)-&gt;`,将显示完整完成 (2认同)

小智 10

对于任何来这里寻求解决方案的人,对我有用的是此StackOverflow中的解决方案:

找不到PhpStorm Laravel 5方法

特别是当我跑步时:

编辑:要使用此命令,您必须安装ide-helper,运行:

composer require --dev barryvdh/laravel-ide-helper 
Run Code Online (Sandbox Code Playgroud)

...

php artisan ide-helper:models
Run Code Online (Sandbox Code Playgroud)

回答“是”

在那之后,方法被认可。

  • 不起作用。php artisan 没有“ide-helper”类别。 (2认同)
  • @Jess 此答案假设您已将此库添加到您的项目中:https://github.com/barryvdh/laravel-ide-helper (2认同)

Pat*_*cow 7

我的课。注释将帮助PhpStorm识别那些方法。

<?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',
    ];
}
Run Code Online (Sandbox Code Playgroud)

  • @YousefAltaf我认为这是自我解释 (2认同)
  • @Patrioticcow 不,不是。你做了什么?你改变了什么?这如何特别适用于 OP 问题?这里没有任何信息可以让我推断出我自己的代码,因为您还没有说明您的解决方案实际上做了什么。 (2认同)

Raw*_*ner 6

我是 laravel 的新手,模型和 phpstorm 的所有这些问题都非常奇怪。这是一个很大的缺点。添加@mixin Eloquent 或运行 php artisan ide-helper:models 等解决方案对我不起作用。PHPStorm 找不到 Eloquent 或 \Eloquent。ide-helper:models 不会添加所有可用的静态方法。所以我提供了一个自己的基础模型,其中包含一个包含所有相关模型方法的 php 文档:

<?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's value from the first result of a query.
 * @method mixed pluck($column) Get a single column's 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's value by a given amount.
 * @method int decrement($column, $amount = 1, array $extra = []) Decrement a column's 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
{

}
Run Code Online (Sandbox Code Playgroud)

然后我自己的模型扩展了这个模型:

<?php

 namespace Modules\Shop\Entities;

 use App\BaseModel;


 class MyEntity extends BaseModel
Run Code Online (Sandbox Code Playgroud)

然后一切正常。BaseModel 现在还没有完成,可以随意添加更多的静态方法,我按需添加。


Mic*_*sky 5

添加到所有模型中有点烦人,但您可以将方法添加到模型文档块中。这将使它在 PHPStorm 中正常工作。

/*
 * @method static \Illuminate\Database\Query\Builder|\App\MyModelName where($field, $value)
 */
Run Code Online (Sandbox Code Playgroud)


小智 5

在尝试了_ide_help.phpBarry的解决方案后,我找到了一个有效且简单的解决方案。显示解决方案的 Laracast 视频:https ://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/15 -- 在第一条评论中,您可以找到 Barry 的链接。

添加它后它对我不起作用,但为了完成,我仍然提到它。

然后我尝试了这个:

在我的模型中,我use Eloquent;在顶部添加。(我通过自动完成而不是键入的方式添加了 Eloquent)。

然后在我的课程上方输入“/** hit ENTER”,它会在我@mixin Eloquent在下面添加的新生成的 PHP 文档中自动生成 PHP 文档。

作为最后一步,我点击了Ctrl+ Alt+ Y(默认设置),它是 PhpStorm 中的同步(文件->同步)。

这修复了警告,并在我的控制器中找到了我的 ::find 方法并且自动完成正在工作。

下面以我的班级为例:

namespace App;

use Illuminate\Database\Eloquent\Model; <br>
use Eloquent;


/** 
 * Class Student
 * @package App 
 * @mixin Eloquent 
 */ 
class Student extends Model <br>
{

}
Run Code Online (Sandbox Code Playgroud)


小智 5

我已经用这种方法解决了。

Baryvdh 为 Laravel 提供了强大的 IDE 支持:

https://github.com/barryvdh/laravel-ide-helper

安装后,您只需在控制台中调用:

php artisan ide-helper:generate
Run Code Online (Sandbox Code Playgroud)

它在 _ide_helper.php 文件中生成所有 Facede 快捷方式(您必须从 git 中排除)

PhpStorm 还有一些特别之处:

php artisan ide-helper:meta
Run Code Online (Sandbox Code Playgroud)


meg*_*tur 5

Laravel 8.x:

添加@mixin到类注释:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Post
 *
 * @mixin Builder
 */
class Post extends Model {
Run Code Online (Sandbox Code Playgroud)


Dyl*_*uth -1

为了让这个问题得到“回答”,你需要 laravel ide-helper。按照这些说明进行操作,一切都会适合您。


归档时间:

查看次数:

17069 次

最近记录:

6 年,7 月 前