jeudi 5 septembre 2019

Laravel creating entry with multiple selection

I have 2 table, RecipesTable & IngredientsTable.

IngredientsTable:

class IngredientsTable extends Migration
{
    public function up()
    {
        Schema::create('ingredients', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('ingredient_name');
        });
    }
}

RecipesTable :

class RecipesTable extends Migration
{
    public function up()
    {
        Schema::create('recipes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('recipe_name');
            $table->unsignedBigInteger('ingredient_id');
            $table->foreign('ingredient_id')->references('id')->on('ingredient');
        });
    }
}

Let say in my RecipesTable table entry I have a recipe call Fried Chicken and my IngredientsTable I have 4 entry : 1, Chicken, 2, Dory, 3, Salt, 4, Breadcrumb. How can I create an entry that associate multiple entry of IngredientsTable into RecipesTable in my Controller? for example my entry should look like this in JSON : Recipe :

{
    id: 1
    name : 'Friend Chicken'
    ingredient_id ; ['1', '3', '4']
}

Right now in my Controller I have something like this :

public function createRecipe(Request $request )
    {
            $data = ([
                'recipe_name' => 'Fried Chicken',
                'ingredient_id' => ['1', '3', '4'],
                ])

            Recipe::create($data);
            return redirect()->route('recipe.index')->withStatus(__('Recipe has been added.'));
        }        
    }

and its not working.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire