Php Laravel创建具有多个选择的条目
我有两个表,Php Laravel创建具有多个选择的条目,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我有两个表,RecipesTable&IngredientsTable IngCreditStable: class IngredientsTable extends Migration { public function up() { Schema::create('ingredients', function (Blueprint $table) { $table->bigIncrements('id');
RecipesTable
&IngredientsTable
IngCreditStable:
class IngredientsTable extends Migration
{
public function up()
{
Schema::create('ingredients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ingredient_name');
});
}
}
配方表:
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');
});
}
}
假设在我的RecipesTable
表格条目中,我有一个食谱调用炸鸡
,我的IngredientsTable
有4个条目:1,鸡肉
,2,多莉
,3,盐
,4,面包屑
。
如何创建一个条目,将IngredientsTable
的多个条目关联到控制器中的RecipesTable
?例如,我的条目在JSON中应该如下所示:
配方:
现在,在我的控制器中,我有如下内容:
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.'));
}
}
并且它不工作。添加新迁移:
class RecipeIngredientRelTable extends Migration
{
public function up()
{
Schema::create('recipe_ingredient_rel', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigIncrements('recipe_id')->index();
$table->bigIncrements('ingredient_id')->index();
$table->foreign('recipe_id')->references('id')->on('recipes');
$table->foreign('ingredient_id')->references('id')->on('ingredient');
});
}
}
添加模型
class RecipeIngredientRel extends BaseModel
{
protected $fillable = [
'recipe_id',
'ingredient_id'
];
public function recipe(){
return parent::belongsTo(Recipe::class);
}
public function ingredient(){
return parent::belongsTo(Ingredient::class);
}
}
在您的配方模型中添加
public function ingredients(){
return $this->belongsToMany(Ingredient::class, 'recipe_ingredient_rel', 'recipe_id');
}
public function recipes(){
return $this->belongsToMany(Recipe::class, 'recipe_ingredient_rel', 'ingredient_id');
}
在您的配料模型中添加
public function ingredients(){
return $this->belongsToMany(Ingredient::class, 'recipe_ingredient_rel', 'recipe_id');
}
public function recipes(){
return $this->belongsToMany(Recipe::class, 'recipe_ingredient_rel', 'ingredient_id');
}
然后,在控制器中只需编写
Recipe::create([
'recipe_name' => $data['recipe_name']
])->attach($data['ingredient_id']);
它将在recipes表中创建名为的实体
配方配料表中的3个实体
recipe_id => {created_id} | ingredient_id => 1
recipe_id => {created_id} | ingredient_id => 2
recipe_id => {created_id} | ingredient_id => 2
然后,要检索包含配料的配方,只需使用
$recipe = Recipe::with('ingredients')->find({created_id});
它将为您提供集合,只需使用toArray()即可查看实际结果
更新
Recipe::create([
'recipe_name' => $data['recipe_name']
])->ingredients()->attach($data['ingredient_id']);
这一个应该能用我认为您必须更改迁移。因为食谱有很多成分一对多关系方法 配方迁移:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('recipe_name');
});
Schema::create('ingredients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('recipe_id');
$table->string('ingredient_name');
$table->foreign('recipe_id')
->references('id')
->on('recipes')
->onDelete('cascade');
});
成分迁移:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('recipe_name');
});
Schema::create('ingredients', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('recipe_id');
$table->string('ingredient_name');
$table->foreign('recipe_id')
->references('id')
->on('recipes')
->onDelete('cascade');
});
为每个配方和配料表创建一个模型,并添加关系: 配方模型
class Recipe
{
protected $table = 'recipes';
//fill the fillables here
public function ingredient(){
return $this->hasMany('App\Ingredient');
}
}
class Ingredient
{
protected $table = 'ingredients';
//fill the fillables here
public function ingredient(){
return $this->belongsTo('App\Recipe');
}
}
成分模型
class Recipe
{
protected $table = 'recipes';
//fill the fillables here
public function ingredient(){
return $this->hasMany('App\Ingredient');
}
}
class Ingredient
{
protected $table = 'ingredients';
//fill the fillables here
public function ingredient(){
return $this->belongsTo('App\Recipe');
}
}
现在,在您的问题中,如何添加: 首先创建配方:
$value = 'fried chicken';
$recipe = Recipe::create([
'recipe_name' => $value
]);
然后插入配方的成分:
$ingredients = ['flour', 'salt', 'oil', 'chicken'];
foreach($ingredients AS $value){
$recipe->ingredient()->create([
'recipe_id' => $recipe->id,
'ingredient_name' => $value
]);
}
展示炸鸡的配料:
//search the recipe
$recipe = Recipe:where('recipe_name', 'fried chicken')->first();
//display ingredients
return $recipe->ingredient;
注意:这只是为了回答问题,向下滚动查看其他方法
只需在控制器中执行以下操作:
public function createRecipe(Request $request )
{
$data = ([
'recipe_name' => 'request('name')',
'ingredient_id' => 'serialize(request('ingredient_id '))',
])
Recipe::create($data);
return redirect()->route('recipe.index')->withStatus(__('Recipe has been added.'));
}
}
在检索时,请使用
取消序列化
来获取这些格式的数据['1','3','4']
。我希望它可以帮助您。试试这个。您需要在这里使用多对多关系。这意味着你需要另一张桌子,将你的食谱与忘恩负义者联系起来。拉威尔支持这种关系。另请参见注意,您需要这样的表recipe\u ingradient\u relation
,其中您将有id
,recipe\u id
,ingradient\u id
也请参见,谢谢,我现在正在查看,我已经发布了一个答案,请尝试,并让我知道它是否有效:)@RobMkrtchyan嗨,谢谢您的更新,现在检查一下,对不起,我离开了一段时间。你将如何用同样的忘恩负义者创造另一个食谱?@RobMkrtchyan这是一对多的关系兄弟。是的,但他似乎需要多对多:)忘恩负义者可以为不同的食谱复制,因此,对于一对多,您将在ingradients中有许多重复的行table@RobMkrtchyan他可以像我说的那样使用其他方法:)但如果我是他,我会使用这种方法。我将尝试为他制作多对多,我将编辑此hi@suspended谢谢你的回答,是的,正如Rob所说,我想在我创建的下一个食谱中重复使用该配料。谢谢@Rob我正在尝试这个,当它工作时会让您知道hi@Rob我尝试了这个,它说完整性约束冲突:1452无法添加或更新子行:外键约束失败
创建条目时,您确定数据库中有所有的Ingradient吗?对于这个例子,你应该有id:1、id:3和id:4的idngedients是的,我有dbhi中的所有成分,这似乎可以解决问题,非常感谢你的帮助和耐心!