在Laravel 5.0中,PHPStorm不能识别我的Model类的方法 [英] PHPStorm is not recognizing methods of my Model class in Laravel 5.0

查看:1722
本文介绍了在Laravel 5.0中,PHPStorm不能识别我的Model类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 <?php命名空间应用程序; 

使用Carbon\\Carbon;
使用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 ());
}

/ **
*一个帖子由用户拥有。
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* /
public function user(){
return $ this-> belongsTo 'App\User');
}

}

这里是我的Controller类我使用它:

 <?php命名空间App \Http\Controller; 

使用App \Http\Requests;

使用App \Http\Requests\CreatePostRequest;
使用App \Post;
使用请求;
使用Illuminate\Support\Facades\Auth;
使用会话;

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参数文章$ article
* @internal param文章$ articles
* /
public function show(Post $ post)
{

return view('tolet.show',compact('post'));
}

public function create()
{
if(Auth :: guest()){
返回重定向('tolet.index') ;
}
return view('tolet.create');
}

/ **
* @param CreatePostRequest $ request
* @return \Illuminate\Http\RedirectResponse | \Illuminate\Routing\\ \\ Reeder
* /
public function store(CreatePostRequest $ request)
{
//验证

$ this-> createPost($ request) ;


// flash('your tolet has been created!') - > important();

返回重定向('tolet.index');
}

/ **
* @param Post $ post
* @return \Illuminate\View\View
* @internal param文章$ 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文章$ article
* @internal param $ id
* /
public function update (Post $ post,CreatePostRequest $ request)
{
$ post-> update($ request-> all());
return redirect('tolet.index');
}

/ **
*同步数据库中的标签列表
*
* @param Post $ post
* /


/ **
*保存新帖子
*
* @param CreatePostRequest $ request
* @return mixed
* /
private function createPost(CreatePostRequest $ request)
{
$ post = Auth :: user() - > posts() - > create($ request->所有());

return $ post;
}


}


解决方案由于方法其中最新找到 findOrFail 和其他不存在于 Model 类中,但在 Builder 并通过魔术方法加载,IDE无法检测到这些。



虽然广泛推荐 laravel-ide-helper 是伟大的,它也不帮助。有多个问题讨论和关于这个问题的解决方法,但都有自己的问题。



迄今为止发现的最佳解决方案,如果__magic方法存在于类中,则IMHO将降低严重性。 PhpStorm在检查设置中有这个确切的选项。



签入设置 - >检查 - > PHP - >未定义 - >未定义的方法这不会让您单击该方法,而只是禁用烦人的标记。 了解更多关于严重性的信息或查看更具表现力的SO答案


failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it?

here is my extended class (Post.php) here show error in latest and where 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('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');
    }

} 

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


}

解决方案

Since methods where, latest, find, findOrFail and others does not exist in Model class, but in Builder and are loaded via magic methods, the IDE can not detect these.

While the widely suggested laravel-ide-helper is great, it does not help also. There are multiple issues and discussions and workarounds on the subject, but all have its own problems.

Best solution I've found so far, IMHO is to downgrade severity if __magic methods are present in class. PhpStorm has this exact option in inspections settings.

Check in Settings -> Inspections -> PHP -> Undefined -> Undefined method This will not let you click on the method, but merely disables the annoying markup. Read more about severities or check this more expressive SO answer

这篇关于在Laravel 5.0中,PHPStorm不能识别我的Model类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆