in ,

Mokhoa oa ho boloka boleng ba li-Checkbox tse ngata sebakeng sa polokelo ea litaba ho Laravel?

Ke hloka ho boloka boleng ba lebokose la li-checkbox ho database ho Laravel. Joang ho etsa?

Mokhoa oa ho boloka boleng ba li-Checkbox tse ngata sebakeng sa polokelo ea litaba ho Laravel?
Mokhoa oa ho boloka boleng ba li-Checkbox tse ngata sebakeng sa polokelo ea litaba ho Laravel?

Ho na le mekhoa e mengata ea ho boloka boleng ba li-checkbox tse ngata sebakeng sa polokelo ea litaba ho Laravel. Mona ke tse ling tsa likhetho:

  1. Sebelisa sebaka sa mofuta oa "boolean". : lebokose le leng le le leng la ho hlahloba le ka bolokoa e le "'nete" kapa "lehata" sebakeng sa mofuta oa boolean polokelong ea hau.
  2. Sebelisa sebaka sa mofuta oa "text". : o ka boloka boleng ba li-checkbox tsohle sebakeng sa mofuta oa mongolo, o arola boleng bo bong le bo bong ka comma. Mohlala, haeba mosebelisi a hlahlobile mabokose a "litholoana" le "meroho", o ka boloka tlhahisoleseling ena e le "litholoana, meroho" tšimong ea mofuta oa mongolo.
  3. Sebelisa tafole ea likamano : haeba u na le li-checkbox tse 'maloa tse hokahaneng le rekoto e tšoanang, u ka sebelisa tafole ea kamano ho boloka data ena. Mohlala, haeba u na le tafole ea "lihlahisoa" e nang le kholomo ea "categories", u ka theha tafole ea "product_categories" e hokahanyang likarolo tse khethiloeng ho sehlahisoa ka seng.
  4. Sebelisa sebaka sa mofuta oa "tafole". : Haeba u sebelisa database e tšehetsang li-arrays (joalo ka PostgreSQL), u ka boloka boleng ba li-checkbox tse ngata sebakeng sa mefuta e mengata. Sena se tla u lumella ho boloka litekanyetso tse ngata tšimong e le 'ngoe ntle le ho sebelisa tafole ea likamano.

Boloka boleng ba li-checkbox tse ngata sebakeng sa polokelo ea litaba u sebelisa sehlopha

Ho Laravel, o ka boloka boleng ba li-checkbox tse ngata sebakeng sa polokelo ea litaba ka ho sebelisa lethathamo ho boloka boleng lebaleng le le leng. Mona ke mohlala oa kamoo u ka e etsang:

1. Etsa phalliso ho kenya sebaka tafoleng ea hau ea polokelo ea boitsebiso ho boloka boleng ba lebokose la ho hlahloba. Ka mohlala, haeba u batla ho boloka litekanyetso sebakeng se bitsoang "options", u ka sebelisa ho falla ho latelang:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddOptionsToTableName extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table_name', function (Blueprint $table) {
            $table->text('options')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('table_name', function (Blueprint $table) {
            $table->dropColumn('options');
        });
    }
}

2. Ka foromo ea hau, etsa li-checkbox bakeng sa khetho ka 'ngoe eo u batlang ho e boloka. Ka mohlala :

<input type="checkbox" name="options[]" value="option1"> Option 1
<input type="checkbox" name="options[]" value="option2"> Option 2
<input type="checkbox" name="options[]" value="option3"> Option 3

3. Ka mokhoa oa ts'ebetso ea tlhahiso ea hau, fumana boleng ba li-checkbox tse khethiloeng 'me u li boloke sebakeng sa polokelo ea litaba. Ka mohlala :

$options = $request->input('options');

$model = new Model();
$model->options = json_encode($options);
$model->save();

Sena se boloka boleng ba li-checkbox tse khethiloeng sebakeng sa "options" joalo ka sehlopha se kentsoeng sa JSON. U ka khona ho khutlisa le ho bonts'a boleng bo khethiloeng ka ho hlophisa sehlopha sa JSON ha u fumana rekoto ho database.

$model = Model::find($id);
$options = json_decode($model->options);

Mehlala ea likhoutu tsa ho boloka boleng ba li-checkbox tse ngata ho Laravel

Mona ke mehlala ea likhoutu tsa ho boloka boleng ba li-checkbox tse ngata sebakeng sa database sa Laravel:

Sebelisa sebaka sa mofuta oa "boolean".

Ho theha kholomo ea boolean ea "subscription_newsletter" tafoleng ea "users":

Schema::table('utilisateurs', function (Blueprint $table) {
    $table->boolean('abonnement_newsletter')->default(0);
});

Ngoliso ea ngoliso ea mosebelisi ho koranta ha o fana ka foromo:

$utilisateur = new Utilisateur;
$utilisateur->abonnement_newsletter = $request->input('abonnement_newsletter');
$utilisateur->save();

Sebelisa sebaka sa mofuta oa "text".

Ntlafatso ea kholomo ea "options_sélectionées" ea mofuta oa mongolo tafoleng ea "survey":

Schema::table('sondage', function (Blueprint $table) {
    $table->text('options_sélectionnées');
});

Ho boloka likhetho tse khethiloeng ke mosebelisi ha o romella foromo:

$sondage = new Sondage;
$sondage->options_sélectionnées = implode(',', $request->input('options'));
$sondage->save();

Sebelisa tafole ea likamano

Ho etsoa ha tafole "categories_products" e nang le likholomo "id_product" le "id_category":

Schema::create('catégories_produits', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('id_produit');
    $table->integer('id_catégorie');
    $table->timestamps();
});

Ho boloka mekhahlelo e khethiloeng ke mosebelisi ha a fana ka foromo:

$produit = new Produit;
$produit->save();

foreach ($request->input('catégories') as $catégorie) {
    $catégorie_produit = new CatégorieProduit;
    $catégorie_produit->id_produit = $produit->id;
    $catégorie_produit->id_catégorie = $catégorie;
    $catégorie_produit->save();
}

Sebelisa sebaka sa mofuta oa "tafole".

Ho theha kholomo "options_selected" ea mofuta oa tafole tafoleng "poll" (haeba u sebelisa PostgreSQL):

Schema::table('sondage', function (Blueprint $table) {
    $table->jsonb('options_sélectionnées');
});

Ho boloka likhetho tse khethiloeng ke mosebelisi ha o romella foromo:

$sondage = new Sondage;
$sondage->options_sélectionnées = $request->input('options');
$sondage->save();

Ho bohlokoa ho hlokomela hore, maemong afe kapa afe, o tla hloka ho etsa bonnete ba hore boleng ba lebokose la hau la litekete bo netefalitsoe le ho hloekisoa pele u bolokoa polokelong ea hau. U ka sebelisa lilaoli tsa netefatso tsa Laravel le li-filters tsa data bakeng sa sena.

[Kakaretso: 1 Bolela: 5]

ngotsoeng ke Anton Gildebrand

Anton ke moqapi ea felletseng ea nang le tjantjello ea ho arolelana malebela le litharollo tsa khoutu le basebetsi mmoho le eena le sechaba sa bahlahisi. Ka semelo se tiileng sa mahlale a morao-rao le a morao-rao, Anton o tseba lipuo le meralo e fapaneng ea mananeo. Ke setho se mafolofolo sa liforamo tsa bahlahisi ba inthaneteng mme o fana ka maikutlo le tharollo khafetsa ho thusa ba bang ho rarolla mathata a mananeo. Ka nako ea hae ea nakoana, Anton o thabela ho lula a tseba mekhoa le mahlale a morao-rao tšimong le ho leka lisebelisoa le meralo e mecha.

Leave ka tlhaloso

Aterese ea hau ea lengolo tsoibila e ke ke ea phatlalatsoa. masimo hlokahala di tšoauoa *

U nahana eng?