Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStorm is not recognizing methods of my Model class in Laravel 5.0 [duplicate]

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


}
like image 689
Osman Goni Nahid Avatar asked Aug 05 '15 10:08

Osman Goni Nahid


4 Answers

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

Example:

<?php namespace App;

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

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

Edit Laravel 6+ (Last tested on v8.75)

use Illuminate\Database\Eloquent\Builder;

/**
 * @mixin Builder
 */

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

like image 193
Tomáš Staník Avatar answered Nov 03 '22 10:11

Tomáš Staník


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

like image 23
ruuter Avatar answered Nov 03 '22 08:11

ruuter


For anyone who came here for a solution, what worked for me is the solution in this StackOverflow:

PhpStorm laravel 5 method not found

specifically when I ran:

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

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

...

php artisan ide-helper:models

and answered "yes"

after that methods are recognized.

like image 33
Amr El Massry Avatar answered Nov 03 '22 10:11

Amr El Massry


My class. The annotations will help PhpStorm to recognize those 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',
    ];
}
like image 9
Patrioticcow Avatar answered Nov 03 '22 10:11

Patrioticcow