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?
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();
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With