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?
3 Answers 3
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();
});
1 Comment
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.
1 Comment
Updated version for Laravel 5.5
Create a file in your rood directory with name like _ide_helper_custom.php and copy next code into it:
<?php
namespace {
exit("This file should not be included, only analyzed by your IDE");
}
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 spatialIndex()
* @method Fluent default($value)
* @method Fluent onUpdate($value)
* @method Fluent onDelete($value)
* @method Fluent references($value)
* @method Fluent on($value)
* @method Fluent charset($value)
* @method Fluent collation($value)
* @method Fluent comment($value)
* @method Fluent autoIncrement()
* @method Fluent storedAs($value)
* @method Fluent useCurrent()
* @method Fluent virtualAs($value)
*/
class Fluent {
}
}
This will force PHP Storm to index the file and correctly suggest methods for chaining. Tested in PHP Storm 2017.3 but should work in all previous and hopefully future versions of IDE.
$table->integer('user_id')->unsigned();