Home > OS >  Check select for data output
Check select for data output

Time:06-02

There is a select that outputs data from tables. When creating a post, I need the data that he selected in select not to be repeated anymore, that is, for the second time he could not select the same values that he had already selected

Select in create.blade

            <select  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>

CreateController

    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);
        }

When you selected the values in select. it is stored in the Server table under the title value

        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();
         });

And the values that are output to select are taken from the 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);
    }

CodePudding user response:

I was able to do this with such a terrible method, in invoke I added $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>

CodePudding user response:

You may pass all the Guild records from controller to the view

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'));
}

In the view you can filter out the records

<select  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>

Another option would be to filter out the Guild records within the controller itself

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'));
}

Then in view it's a normal @foreach

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