Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpstorm 9 EAP Method "unique" not found in class Illuminate\Support\Fluent

I am working on a website project and I'am using Laravel 5 and PHPStorm 9 EAP.

I created a migration and use this code $table->string('name')->unique(); and the IDE highlighted the unique() and show a message Method "unique" not found in class Illuminate\Support\Fluent.

Here is my migration:

class CreateProductsTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name')->unique();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('products');
}

}

How can I fix this problem?

like image 708
Yaey kay Avatar asked Nov 30 '22 10:11

Yaey kay


2 Answers

Calling $table->integer('user_id') returns a new instance of Illuminate\Support\Fluent. But the Fluent class does not provide an unique() method. Instead it's using PHP magic method __call. That's why PHPStorm is complaining.

Option one is to tell PHPStorm that unique() is a valid method. For this we can add some PHPDoc in vendor/laravel/framework/src/Illuminate/Support/Fluent.php and tell PHPStorm that unique() is a valid method:

/**
 * @method unique
 */
class Fluent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable 
{
    // ...
}

However I would not suggest modifying files in the /vendor/ folder. Instead create a bug report/pull request on Laravel. Someone already created a pull request for this (https://github.com/illuminate/support/pull/25).

Option two is to modify your create method and try to avoid the magic methods on the Fluent interface:

Schema::create('products', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('name');
    $table->unique('name');
    $table->timestamps();
});
like image 114
maxwilms Avatar answered Dec 04 '22 03:12

maxwilms


Personally I use a helper PHP class files (like _ide_helper.php) to tell PHPStorm about the methods.

For the Fluent I created the following file to be indexed by the PHPStorm, but not included in actual php files:

<?php

namespace Illuminate\Support;

/**
 * @method Fluent first()
 * @method Fluent after($column)
 * @method Fluent change()
 * @method Fluent nullable()
 * @method Fluent unsigned()
 * @method Fluent unique()
 * @method Fluent index()
 * @method Fluent primary()
 * @method Fluent default($value)
 * @method Fluent onUpdate($value)
 * @method Fluent onDelete($value)
 * @method Fluent references($value)
 * @method Fluent on($value)
 */
class Fluent {}

As a side note, you should then disable warnings about multiply-defined classes, however if you are using IDE Helper module, you should have them already disabled anyway.

like image 45
Skydev Avatar answered Dec 04 '22 03:12

Skydev