检查 select 以获取数据输出

Check select for data output

有一个select输出来自table的数据。在创建post时,我需要他select在select中编辑的数据不再重复,即第二次他不能select相同他已经 selected

的价值观

Select 在 create.blade

            <select class="select-css" name="title" required="">
                <option selected="" value="" disabled="">Выберите сервер</option>

                @foreach($users as $user)
                    @foreach($user->guilds as $guild)
                        <option value="{{$guild->id}}">{{$guild->name}}</option>
                    @endforeach
                @endforeach
            </select>

创建控制器

    public function __invoke()
    {
        $users = User::with('guilds')->get();
        $categories = Category::all();
        $languages = Language::all();
        $server = Server::all();
        $guild = User::with('guilds')->where('id', Auth::user()->id)->get();

        return view('main.server.create', compact('categories', 'languages','users'));
    }

StoreController

        try{
            $data = $request->validated();
            $categoryIds = $data['category_ids'];
            unset($data['category_ids']);
            $data['user_id'] = Auth::user()->id;

            $server = Server::firstOrCreate($data);
            $server->categories()->attach($categoryIds);

        } catch (Exception $exception) {
            abort(404);
        }

当您 select 编辑 select 中的值时。它存储在服务器 table 下的标题值

        Schema::create('servers', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id();
            $table->string('user_id');
            $table->string('title');
            $table->longText('content');
            $table->string('description');
            $table->string('invite');

            $table->unsignedBigInteger('icon_id')->nullable();
            $table->unsignedBigInteger('language_id')->nullable();
            $table->unsignedBigInteger('category_id')->nullable();

            $table->index('icon_id','server_icon_idx');
            $table->index('language_id','server_language_idx');
            $table->index('category_id','server_category_idx');

            $table->foreign('icon_id','server_icon_fk')->on('icons')->references('id');
            $table->foreign('language_id','server_language_fk')->on('languages')->references('id');
            $table->foreign('category_id','server_category_fk')->on('categories')->references('id');

            $table->timestamps();
         });

并且输出到 select 的值取自 UserGuild

        Schema::create('user_guilds', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id();

            $table->unsignedBigInteger('user_id')->nullable();
            $table->unsignedBigInteger('guild_id')->nullable();

            $table->index('user_id','user_guild_user_idx');
            $table->index('guild_id','user_guild_guild_idx');

            $table->foreign('user_id','user_guild_user_fk')->on('users')->references('id');
            $table->foreign('guild_id','user_guild_guild_fk')->on('guilds')->references('id');


            $table->timestamps();
        });
class UserGuild extends Model
{
    public function user(){
        return $this->hasMany(User::class);
    }
    public function guild(){
        return $this->belongsToMany(Guild::class);
    }

您可以将所有 Guild 记录从控制器传递到视图

public function __invoke()
{
    $user = User::with('guilds')->find(auth()->id());
    $categories = Category::all();
    $languages = Language::all();
    //$server = Server::all();
    $guilds = Guild::all();

    return view('main.server.create', compact('categories', 'languages','user', 'guilds'));
}

在视图中您可以筛选出记录

<select class="select-css" name="title" required="">
    <option selected="" value="" disabled="">Выберите сервер</option>
        @foreach($guilds as $guild)
            @if(!$user->guilds->contains('id', $guild->id)
                <option value="{{$guild->id}}">{{$guild->name}}</option>
            @endif
        @endforeach
</select>

另一种选择是过滤掉控制器本身的 Guild 记录

public function __invoke()
{
    $user = User::with('guilds')->find(auth()->id());
    $categories = Category::all();
    $languages = Language::all();
    //$server = Server::all();
    $guilds = Guild::whereNotIn('id', $user->guilds->pluck('id')->toArray())->get();

    return view('main.server.create', compact('categories', 'languages','user', 'guilds'));
}

那么在视图中是正常的@foreach

<select class="select-css" name="title" required="">
    <option selected="" value="" disabled="">Выберите сервер</option>
        @foreach($guilds as $guild)
            <option value="{{$guild->id}}">{{$guild->name}}</option>
        @endforeach
</select>

我能够用如此糟糕的方法做到这一点,在 invoke 我添加了 $servers = Server::all();

                @foreach($users as $user)
                    @foreach($user->guilds as $guild)
                             <option value="{{$guild->id}}"
                                 @foreach($servers as $server)
                                     @if($guild->id == $server->title)
                                        disabled
                                     @endif
                                 @endforeach
                             >{{$guild->name}}</option>
                        @endforeach
                    @endforeach
           </select>