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:
- 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.
- 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.
- 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.
- 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.
Lethathamo la tse kahare
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.