Laravel annotation for Models in PhpStorm

Asked
Active3 hr before
Viewed126 times

5 Answers

annotationmodelslaravel
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Find centralized, trusted content and collaborate around the technologies you use most., Does the stock price drop if one of the largest shareholders of the company sells all their shares? ,I am creating a project based on Laravel framework and I am using PhpStorm for it. By default, Laravel has the following settings if we enable Laravel plugin in PhpStorm.

Now based on these settings if I create a model and try to define all the relations and other properties in the following way

< ? php
/**
 * Class User
 *
 * @package App\Http\Entities
 * @property int $id
 * @property mixed $posts
 */
class User extends Model {

   /**
    * The table associated with the model
    *
    * @var string
    */
   protected $table = 'users';

   /**
    * Indicates if model should be timestampped
    *
    * @var bool
    */
   public $timestamps = false;

   /**
    * The attributes are not mass assignable
    *
    * @var array
    */
   protected $guarded = ['type'];

   /**
    * Get all the posts for a user
    *
    * @return \Illuminate\Database\Eloquent\Relations\HasMany
    */
   public
   function posts() {
      return $this - > hasMany('App\Http\Entities\Post');
   }

}
load more v
88%

The Laravel IDE Helper may have to be run after changing or adding services, controllers, models and views. Alternatively, set up File Watchers in PhpStorm to automatically regenerate this file when, for example, composer.json is updated.,You can also install the Laravel generators Composer package to add various Laravel generators for models, views, controllers, and much more.,The Laravel plugin also adds a marker to the editor gutter, which lets you navigate to the parent section.,Laravel plugin (free) and Laravel IDE helper tool.

Add Laravel IDE helper as a ServiceProvider into the application. In the config/app.php file, add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class under the providers element:

ServiceProvider

Add Laravel IDE helper as a ServiceProvider into the application. In the config/app.php file, add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class under the providers element:

Barryvdh\ LaravelIdeHelper\ IdeHelperServiceProvider::class

Add Laravel IDE helper as a ServiceProvider into the application. In the config/app.php file, add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class under the providers element:

providers
load more v
72%

Automatic PHPDocs for models Model Directories Ignore Models Model Hooks ,Automatic PHPDocs generation for Laravel Fluent methods,Usage Automatic PHPDoc generation for Laravel Facades Automatic PHPDocs for models Model Directories Ignore Models Model Hooks Automatic PHPDocs generation for Laravel Fluent methods Auto-completion for factory builders PhpStorm Meta for Container instances ,Automatic PHPDoc generation for Laravel Facades

* support
for all possible `Model::$casts`
types

   *
   add testing
for casts

   *
   update changelog

   *
   update changelog(again ? )
load more v
65%

My class. The annotations will help PhpStorm to recognize those methods.,File path : project_path\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php,after that methods are recognized.,and then phpstorm recognized!!

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

}
load more v
75%

I also install laravel plugin in PhpStorm but still not work too, what can I do now?,I ran "php artisan ide-helper:models" -> Yes, it wrote some line in my models file and still not work I ran again "php artisan ide-helper:models" -> No, it created a new file called _ide_helper_models.php file but still not work.,I access file _ide_helper_models.php and add this function into class Article it work xD,Ok here is the solution, assuming you have installed the helper already:

Here is my code.

< ? php

namespace AppHttpControllers;

use AppArticle;
use AppMenu;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateHttpRequestst;
use IlluminateSupportFacadesInput;

class PagesController extends Controller {
   public
   function index() {
      $article = Article::latest('published_at') - > get();
      $data = array(
         'articles' => $article
      );
      return view('pages.index', compact($data));
   }

   public
   function contact() {
      return view('pages.contact');
   }

   public
   function about() {
      return view('pages.about');
   }
}
load more v