matchish / laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout
MIT License
700 stars 113 forks source link

php artisan scout:import error? Cannot use Encore\Admin\Form as Form because the name is already in use on line 21 #263

Closed aoeng closed 9 months ago

aoeng commented 10 months ago

laravel 10.31 scout 10.5 laravel-scout-elasticsearch 7.4.0 encore/laravel-admin 1.8.19

//php artisan scout:import 
[2023-11-10 16:02:19] local.ERROR: Cannot use Encore\Admin\Form as Form because the name is already in use on line 21 {"exception":"[object] (PhpParser\\Error(code: 0): Cannot use Encore\\Admin\\Form as Form because the name is already in use on line 21 at /www/wwwroot/Ticket/vendor/nikic/php-parser/lib/PhpParser/NameContext.php:71)
[stacktrace]
#0 /www/wwwroot/Ticket/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NameResolver.php(171): PhpParser\\NameContext->addAlias()
#1 /www/wwwroot/Ticket/vendor/nikic/php-parser/lib/PhpParser/NodeVisitor/NameResolver.php(63): PhpParser\\NodeVisitor\\NameResolver->addAlias()
#2 /www/wwwroot/Ticket/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(200): PhpParser\\NodeVisitor\\NameResolver->enterNode()
#3 /www/wwwroot/Ticket/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(91): PhpParser\\NodeTraverser->traverseArray()
#4 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Searchable/SearchableListFactory.php(134): PhpParser\\NodeTraverser->traverse()
#5 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Searchable/SearchableListFactory.php(105): Matchish\\ScoutElasticSearch\\Searchable\\SearchableListFactory->getStmts()
#6 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Searchable/SearchableListFactory.php(91): Matchish\\ScoutElasticSearch\\Searchable\\SearchableListFactory->getProjectClasses()
#7 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Searchable/SearchableListFactory.php(80): Matchish\\ScoutElasticSearch\\Searchable\\SearchableListFactory->getSearchableClasses()
#8 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Searchable/SearchableListFactory.php(68): Matchish\\ScoutElasticSearch\\Searchable\\SearchableListFactory->find()
#9 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Console/Commands/ImportCommand.php(43): Matchish\\ScoutElasticSearch\\Searchable\\SearchableListFactory->make()
#10 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Conditionable/Traits/Conditionable.php(34): Matchish\\ScoutElasticSearch\\Console\\Commands\\ImportCommand->Matchish\\ScoutElasticSearch\\Console\\Commands\\{closure}()
#11 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(531): Illuminate\\Support\\Collection->when()
#12 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Console/Commands/ImportCommand.php(44): Illuminate\\Support\\Collection->whenEmpty()
#13 /www/wwwroot/Ticket/vendor/matchish/laravel-scout-elasticsearch/src/Console/Commands/ImportCommand.php(32): Matchish\\ScoutElasticSearch\\Console\\Commands\\ImportCommand->searchableList()
#14 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Matchish\\ScoutElasticSearch\\Console\\Commands\\ImportCommand->handle()
#15 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#16 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#17 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod()
#18 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\\Container\\BoundMethod::call()
#19 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Console/Command.php(211): Illuminate\\Container\\Container->call()
#20 /www/wwwroot/Ticket/vendor/symfony/console/Command/Command.php(326): Illuminate\\Console\\Command->execute()
#21 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Console/Command.php(181): Symfony\\Component\\Console\\Command\\Command->run()
#22 /www/wwwroot/Ticket/vendor/symfony/console/Application.php(1081): Illuminate\\Console\\Command->run()
#23 /www/wwwroot/Ticket/vendor/symfony/console/Application.php(320): Symfony\\Component\\Console\\Application->doRunCommand()
#24 /www/wwwroot/Ticket/vendor/symfony/console/Application.php(174): Symfony\\Component\\Console\\Application->doRun()
#25 /www/wwwroot/Ticket/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(201): Symfony\\Component\\Console\\Application->run()
#26 /www/wwwroot/Ticket/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle()
#27 {main}
"} 

The system runs properly on the local server(win11), but an error occurs when the system runs on the server(ubuntu)! How to fix it?

aoeng commented 10 months ago

I change driver to anouther "algolia",but still so.The configuration file doesn't seem to be working

hkulekci commented 10 months ago

I guess one of your model using this Form class name some how. Find the line in your models and could you change "Encore\Admin\Form as Form" part as "Encore\Admin\Form as AdminForm" and use "AdminForm" instead of "Form" in that file.

aoeng commented 10 months ago

我猜你的模型之一使用这个 Form 类名称是如何的。找到模型中的行,您是否可以将“Encore\Admin\Form as Form”部分更改为“Encore\Admin\Form as AdminForm”,并在该文件中使用“AdminForm”而不是“Form”。

NO!Are there any other solutions?thx

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Scout\Searchable;
use Overtrue\LaravelPinyin\Facades\Pinyin;

class Concert extends Model
{
    use Searchable;

    public function searchableAs(): string
    {
        return 'concerts_index';
    }

    public function toSearchableArray(): array
    {
        $this->loadMissing(['people']);

        return [
            'name'          => $this->name,
            'name_pinyin'   => implode(' ', Pinyin::convert($this->name)),
            'people'        => $this->people->pluck('name')->implode(','),
            'people_pinyin' => $this->people->pluck('name')->transform(function ($name) {
                return [implode(' ', Pinyin::convert($name)), Pinyin::abbr($name)];
            })->toArray()
        ];
    }

    public function shouldBeSearchable(): bool
    {
        return $this->enabled;
    }
}
hkulekci commented 10 months ago

I don't know, we miss something. Okay. Let's look at the problem again from scratch. The problem is caused by the fact that there are 2 Form classes in the same namespace. And You changed one of the names. But somehow, more is needed to solve the problem. You may need to give more clues to understand the problem. And I checked the codebase of this library. There is no class as Form. According to my understanding, the issue is beyond the scope of the library.

If you can give more information, we can reconsider the issue.

aoeng commented 9 months ago

yes composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": ["laravel", "framework"],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "aoeng/laravel-admin-advertisement": "^1.0",
        "aoeng/laravel-admin-article": "^1.0",
        "aoeng/laravel-admin-filesystem": "^1.2",
        "aoeng/laravel-admin-version": "^1.0",
        "encore/laravel-admin": "1.*",
        "guzzlehttp/guzzle": "^7.2",
        "iidestiny/laravel-filesystem-oss": "^3.3",
        "laravel-admin-ext/latlong": "1.x",
        "laravel/framework": "^10.10",
        "laravel/sanctum": "^3.2",
        "laravel/scout": "*",
        "laravel/tinker": "^2.8",
        "leonis/easysms-notification-channel": "^2.0",
        "matchish/laravel-scout-elasticsearch": "^7.4",
        "overtrue/laravel-pinyin": "^5.0",
        "overtrue/laravel-socialite": "^4.0",
        "propaganistas/laravel-phone": "^5.0"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "knuckleswtf/scribe": "^4.25",
        "laravel-lang/common": "^4.0",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.18",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.1",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

models just one

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Scout\Searchable;
use Overtrue\LaravelPinyin\Facades\Pinyin;

class Concert extends Model
{
    use Searchable;

    public function searchableAs(): string
    {
        return 'concerts_index';
    }

    public function toSearchableArray(): array
    {
        $this->loadMissing(['people']);

        return [
            'name'          => $this->name,
            'name_pinyin'   => implode(' ', Pinyin::convert($this->name)),
            'people'        => $this->people->pluck('name')->implode(','),
            'people_pinyin' => $this->people->pluck('name')->transform(function ($name) {
                return [implode(' ', Pinyin::convert($name)), Pinyin::abbr($name)];
            })->toArray()
        ];
    }

    public function shouldBeSearchable(): bool
    {
        return $this->enabled;
    }
}

scout.php

 'driver' => env('SCOUT_DRIVER', 'null'),

.env ,but change auouth one ,still so,config like not working.

SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine

system ubuntu,It is working in windows. Can you repeat the question? Do you need anything else?

aoeng commented 9 months ago
  info('Stmts: ' ,[$stmts]);
  return $nodeTraverser->traverse($stmts);

It seems to print out all my php code What happened?

hkulekci commented 9 months ago

I just tried with your sharing :

Importing [App\Models\Concert]
Switching to the new index
5/5 [⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬] 100%

 [OK] All [App\Models\Concert] records have been imported.                                                              

Everything works fine for me. I guess there should be a People model somewhere. And that model uses something related to Form.

It seems to print out all my php code What happened?

This library loops on your project file to find the Models which are Searchable. And import them. You can check the Matchish\ScoutElasticSearch\Console\Commands\ImportCommand as well.

aoeng commented 9 months ago

change app/Admin/bootstap.php

use Encore\Admin\Form as AdminForm;
use Encore\Admin\Grid as AdminGrid;

It looks good.

aoeng commented 9 months ago

Search all files This design is a little anti-human,Is it that I give you something, and you import it?

hkulekci commented 9 months ago

I am afraid I could not understand exactly what you mean but you can feel free to raise a PR with better solution as well. That part just looping to find classes which are using Searchable trait to be able import it into ES.