4

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?

YvesLeBorg
9,0898 gold badges39 silver badges50 bronze badges
asked Mar 12, 2015 at 7:47
4
  • Try PHPStorm 8. PHPStorm 9 is just EAP, not the stable version. see confluence.jetbrains.com/display/PhpStorm/… Commented Mar 12, 2015 at 12:05
  • 1
    I already tried it but getting the same highted message Commented Mar 12, 2015 at 12:43
  • Same issue here, when I do $table->integer('user_id')->unsigned(); Commented Mar 16, 2015 at 17:15
  • 1
    You should report it to youtrack.jetbrains.com if it's a bug. Commented Mar 17, 2015 at 21:53

3 Answers 3

13
+50

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();
});
answered Mar 18, 2015 at 9:25
Sign up to request clarification or add additional context in comments.

1 Comment

Option 1: It's 2017 now and problem still not solved by Laravel's developers. Option 2: feels like "Use your finger to clean your teeth. There is no need in tootbrush" =) Look for other answers in this thread - they will allow you to use magic at a cost of 1 extra file in your project and 1 minute to add it. Good luck
5

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.

answered Sep 27, 2015 at 18:44

1 Comment

This one works perfectly up to PHP Storm 2017.3. Create file in root folder of your project an call it "_ide_helper_custom.php" and that will do the trick
3

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.

answered Dec 12, 2017 at 13:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.