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

2021-12-26 00:00:00 model php laravel phpstorm eloquent

向数据库中插入数据失败,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.

相关文章