沃梦达 / 编程问答 / php问题 / 正文

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

PHPStorm is not recognizing methods of my Model class in Laravel 5.0(PHPStorm 无法识别 Laravel 5.0 中我的模型类的方法)

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

问题描述

向数据库中插入数据失败,IDE (phpStrom) 中未找到所有查询类和模型类的方法,我该如何解决?

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?

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

here is my extended class (Post.php) here show error in latest and where method:

<?php namespace App;

use CarbonCarbon;
use IlluminateDatabaseEloquentModel;

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 IlluminateDatabaseEloquentRelationsBelongsTo
     */
    public function user(){
        return $this->belongsTo('AppUser');
    }

} 

这是我使用它的控制器类:

and Here is my Controller class where i use it :

<?php namespace AppHttpControllers;

use AppHttpRequests;

use AppHttpRequestsCreatePostRequest;
use AppPost;
use Request;
use IlluminateSupportFacadesAuth;
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 IlluminateViewView
     * @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 IlluminateHttpRedirectResponse|IlluminateRoutingRedirector
     */
    public function store(CreatePostRequest $request)
    {
        //validation

        $this->createPost($request);


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

        return redirect('tolet.index');
    }

    /**
     * @param Post $post
     * @return IlluminateViewView
     * @internal param Articles $article
     */
    public function edit(Post $post)
    {
        return view('tolet.edit', compact('post'));
    }


    /**
     * @param Post $post
     * @param CreatePostRequest $request
     * @return IlluminateHttpRedirectResponse|IlluminateRoutingRedirector
     * @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;
    }


}

推荐答案

如果你想要一个扩展 Model 的类来识别 Eloquent 方法,只需在该类的 PHPDoc 顶部注释中添加以下内容:

If you want a class extending Model to recognize Eloquent methods, just add the following in the top PHPDoc comment on the class:

@mixin Eloquent

示例:

<?php namespace App;

use CarbonCarbon;
use Eloquent;
use IlluminateDatabaseEloquentModel;

/**
 * Post
 *
 * @mixin Eloquent
 */
class Post extends Model {

编辑 Laravel 6+

use IlluminateDatabaseEloquentBuilder;

/**
 * @mixin Builder
 */

注意:你们中的大多数人可能正在为 Laravel 使用 ide-helper,因此这个 @mixin 属性为模型类自动生成.

Note: Most of you probably are using ide-helper for Laravel, therefore this @mixin attribute is automatically generated for model Classes.

这篇关于PHPStorm 无法识别 Laravel 5.0 中我的模型类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHPStorm 无法识别 Laravel 5.0 中我的模型类的方法

基础教程推荐