He maha nga huarahi hei penapena i nga uara o nga pouakataki maha i roto i te papaa raraunga i Laravel. Anei etahi whiringa:
- Whakamahia he mara momo "boolean". : ka taea te tiaki ia pouakataki hei "pono" he "heka" ranei i roto i te mara momo boolean i roto i to papaunga raraunga.
- Whakamahia he mara momo "kuputuhi". : ka taea e koe te penapena i nga uara o nga pouakataki kua takina ki roto i te mara momo tuhinga, ka wehea ia uara me te piko. Hei tauira, ki te tirohia e te kaiwhakamahi nga pouaka "hua" me te "huawhenua", ka taea e koe te tiaki i enei korero hei "hua, huawhenua" ki te mara momo kupu.
- Whakamahia he ripanga hononga : mena he maha nga pouakataki e hono ana ki te rekoata kotahi, ka taea e koe te whakamahi i te ripanga hononga hei penapena i enei raraunga. Hei tauira, mena kei a koe he ripanga "hua" me te "waahanga" tīwae, ka taea e koe te hanga ripanga "product_categories" e hono ana i nga waahanga kua tohua ki ia hua.
- Whakamahia he momo mara "tepu". : Mēnā kei te whakamahi koe i te pātengi raraunga e tautoko ana i ngā huānga (pēnei i te PostgreSQL), ka taea e koe te penapena i nga uara o nga pouakataki maha i roto i te mara momo huānga. Ma tenei ka taea e koe te penapena i nga uara maha ki te mara kotahi me te kore e whakamahi i te ripanga hononga.
Whakaorangia nga uara pouakataki maha ki roto i te papaa raraunga ma te whakamahi i te raupapa
I roto i te Laravel, ka taea e koe te penapena i nga uara pouakataki maha i roto i te papaa raraunga ma te whakamahi i te huinga hei penapena i nga uara ki te mara kotahi. Anei tetahi tauira o te pehea e taea ai e koe:
1. Waihangahia he heke ki te taapiri i tetahi mara ki to ripanga raraunga hei penapena i nga uara pouakataki. Hei tauira, mena kei te pirangi koe ki te penapena i nga uara ki roto i te mara e kiia nei ko "kowhiringa", ka taea e koe te whakamahi i te hekenga e whai ake nei:
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. I to puka, hanga pouakataki mo ia whiringa e hiahia ana koe ki te penapena. Hei tauira:
<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. I roto i to arorau tukatuka tuku puka, tikina nga uara o nga pouakataki kua tohua ka penapena ki roto i te papaa raraunga. Hei tauira:
$options = $request->input('options');
$model = new Model();
$model->options = json_encode($options);
$model->save();
Ka penapena nga uara o nga pouakataki kua tohua ki te mara "kwhiringa" hei huānga whakawaehere JSON. Ka taea e koe te tiki me te whakaatu i nga uara kua tohua ma te wetewete i te huinga JSON ka tikina e koe te rekoata mai i te papaaarangi.
$model = Model::find($id);
$options = json_decode($model->options);
Tauira waehere hei penapena i nga uara pouakataki maha i Laravel
Anei etahi tauira tauira mo te penapena i nga uara o nga pouakataki maha i roto i te papaunga raraunga i Laravel:
Whakamahia he mara momo "boolean".
Waihangahia te "subscription_newsletter" tīwae boolean i te ripanga "kaiwhakamahi":
Schema::table('utilisateurs', function (Blueprint $table) {
$table->boolean('abonnement_newsletter')->default(0);
});
Te rehitatanga o te ohaurunga a te kaiwhakamahi ki te panui i te wa e tuku ana te puka:
$utilisateur = new Utilisateur;
$utilisateur->abonnement_newsletter = $request->input('abonnement_newsletter');
$utilisateur->save();
Whakamahia he mara momo "kuputuhi".
Waihangahia te "options_séléctionées" tīwae o te momo kuputuhi i te ripanga "survey":
Schema::table('sondage', function (Blueprint $table) {
$table->text('options_sélectionnées');
});
Te tiaki i nga whiringa i tohua e te kaiwhakamahi i te wa e tuku ana te puka:
$sondage = new Sondage;
$sondage->options_sélectionnées = implode(',', $request->input('options'));
$sondage->save();
Whakamahia he ripanga hononga
Waihangahia te ripanga "categories_products" me nga pou "id_product" me "id_category":
Schema::create('catégories_produits', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('id_produit');
$table->integer('id_catégorie');
$table->timestamps();
});
Te tiaki i nga waahanga kua tohua e te kaiwhakamahi i te wa e tuku ana te puka:
$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();
}
Whakamahia he momo mara "tepu".
Waihangahia te pou "options_selected" o te momo ripanga i te ripanga "poll" (mehemea ka whakamahi koe i te PostgreSQL):
Schema::table('sondage', function (Blueprint $table) {
$table->jsonb('options_sélectionnées');
});
Te tiaki i nga whiringa i tohua e te kaiwhakamahi i te wa e tuku ana te puka:
$sondage = new Sondage;
$sondage->options_sélectionnées = $request->input('options');
$sondage->save();
He mea nui kia mohio koe, ahakoa he aha, ka hiahia koe ki te whakarite kia tika te whakamana me te horoi i o uara pouakataki i mua i te penapena ki to papaunga raraunga. Ka taea e koe te whakamahi i nga kaiwhakahaere whakamana a Laravel me nga whiriwhiringa raraunga mo tenei.