我是laravel的新手,我正在为三个表创建json响应,我正在尝试最多两个表,但是我无法在编码中添加第三个表,请在我的函数中添加一些编码。表是
我现在要将json嵌套到第二个表中。
我创建的功能:
public function fetchCategory()
{
$tableIds = DB::select( DB::raw("SELECT * FROM table_category_type"));
$jsonResult = array();
for($i = 0;$i < count($tableIds);$i++)
{
$jsonResult[$i]["category_id"] = $tableIds[$i]->category_id;
$jsonResult[$i]["category_type"] = $tableIds[$i]->category_type;
$id = $tableIds[$i]->category_id;
$jsonResult[$i]["main_category"] = DB::select( DB::raw("SELECT * FROM table_main_category WHERE category_type_id = $id"));
}
return Response::json(array(
'success' => '1',
'data' => $jsonResult),
200
);
}
Json从我的代码中获得了第二个表嵌套在第一张表中,但我想在第一个表中嵌套第三张表。
{
"success": "1",
"data": [
{
"category_id": 1,
"category_type": "Study",
"main_category": []
},
{
"category_id": 2,
"category_type": "Sports",
"main_category": [
{
"main_category_id": 1,
"category_type_id": 2,
"category_name": "Popular Sports"
},
{
"main_category_id": 2,
"category_type_id": 2,
"category_name": "Team Sports"
},
{
"main_category_id": 3,
"category_type_id": 2,
"category_name": "Racquet Sports"
},
{
"main_category_id": 4,
"category_type_id": 2,
"category_name": "Fitness"
},
{
"main_category_id": 5,
"category_type_id": 2,
"category_name": "Recreation"
}
]
},
{
"category_id": 3,
"category_type": "Other",
"main_category": []
}
]
}
我现在将main_category嵌套在category_type中,我希望嵌套在main_category中的sub_category plz需要更改编码。我将三个表嵌套在一对多关系中。由于我是Laravel的新手,所以我不明白如何以嵌套关系发送ID
答案 0 :(得分:1)
由于您是Laravel的新手,所以您可能不知道Eloquent是什么。 如果我正确理解您的问题,我们有3个表,我们需要通过关系将它们连接起来,这样我们就可以获得带有数据的每个项目关系对象。
所以我们有3个表:
注意:表的名称为复数形式,模型的名称为单数形式。像这样用于表的 category_types 和用于模型类的 CategoryType 。
好,因此首先需要创建迁移:
php artisan make:migration create_category_types_table
public function up()
{
Schema::create('category_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 255)->nullable();
$table->timestamps();
});
}
php artisan make:migration create_main_categories_table
public function up()
{
Schema::create('main_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('category_type_id')->index();
$table->string('name', 255)->nullable();
$table->timestamps();
});
}
php artisan make:migration create_sub_categories_table
public function up()
{
Schema::create('sub_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('category_type_id')->index();
$table->bigInteger('main_category_id')->index();
$table->string('name', 255)->nullable();
$table->timestamps();
});
}
因此,在此迁移功能中,我们具有自动递增和名称
请注意,您只能在此表中写入名称,而不能将category_name和category_type_id写入 您始终知道此属性属于partuclar表
好的,接下来您可以创建和配置我们的模型。对于CategoryTypes模型,我们应该 为main_categories和sub_categories创建2个关系,如下所示:
php artisan make:model CategoryType
php artisan make:model MainCategory
php artisan make:model SubCategory
这是我们的类别类型模型代码:
class CategoryType extends Model {
protected $fillable = ['name'];
protected $dates = [
'created_at',
'updated_at'
];
public function mainCategories()
{
return $this->hasMany(MainCategory::class);
}
public function subCategories() {
return $this->hasMany(SubCategory::class);
}
}
在定义CategoryType模型实境时,我们现在可以检索控制器中所需的数据:
public function fetchCategory()
{
$categories = CategoryType::with(['mainCategories', 'subCategories'])->get();
return response()->json($categories, 200);
}
还有我们的json:
[
{
"id": 1,
"name": "Books",
"created_at": "2019-05-19 13:24:51",
"updated_at": "2019-05-19 13:24:51",
"main_categories": [
{
"id": 1,
"category_type_id": 1,
"name": "Si-Fi",
"created_at": "2019-05-19 13:26:07",
"updated_at": "2019-05-19 13:26:08"
},
{
"id": 2,
"category_type_id": 1,
"name": "Biography ",
"created_at": "2019-05-19 13:26:33",
"updated_at": "2019-05-19 13:26:34"
},
{
"id": 3,
"category_type_id": 1,
"name": "Tall tale ",
"created_at": "2019-05-19 13:26:57",
"updated_at": "2019-05-19 13:26:58"
},
{
"id": 4,
"category_type_id": 1,
"name": "Short story",
"created_at": "2019-05-19 13:27:07",
"updated_at": "2019-05-19 13:27:07"
},
{
"id": 5,
"category_type_id": 1,
"name": "Fantasy",
"created_at": "2019-05-19 13:27:17",
"updated_at": "2019-05-19 13:27:18"
}
],
"sub_categories": [
{
"id": 1,
"category_type_id": 1,
"main_category_id": 1,
"name": "Space exploration",
"created_at": "2019-05-19 13:42:35",
"updated_at": "2019-05-19 13:42:35"
},
{
"id": 2,
"category_type_id": 1,
"main_category_id": 2,
"name": "Historical biography",
"created_at": "2019-05-19 13:42:36",
"updated_at": "2019-05-19 13:42:36"
},
{
"id": 3,
"category_type_id": 1,
"main_category_id": 5,
"name": "The Lord of the Rings",
"created_at": "2019-05-19 13:42:37",
"updated_at": "2019-05-19 13:42:37"
}
]
},
{
"id": 2,
"name": "Sports",
"created_at": "2019-05-19 13:24:57",
"updated_at": "2019-05-19 13:24:57",
"main_categories": [
{
"id": 6,
"category_type_id": 2,
"name": "Popular Sports",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 7,
"category_type_id": 2,
"name": "Team Sports",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 8,
"category_type_id": 2,
"name": "Racquet Sports",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 9,
"category_type_id": 2,
"name": "Fitness",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 10,
"category_type_id": 2,
"name": "Recreation",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
}
],
"sub_categories": [
{
"id": 4,
"category_type_id": 2,
"main_category_id": 6,
"name": "Football",
"created_at": "2019-05-19 13:42:37",
"updated_at": "2019-05-19 13:42:37"
},
{
"id": 5,
"category_type_id": 2,
"main_category_id": 6,
"name": "Basketball",
"created_at": "2019-05-19 13:42:37",
"updated_at": "2019-05-19 13:42:37"
}
]
},
{
"id": 3,
"name": "Study",
"created_at": "2019-05-19 13:25:24",
"updated_at": "2019-05-19 13:25:25",
"main_categories": [
{
"id": 11,
"category_type_id": 3,
"name": "Web Development",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 12,
"category_type_id": 3,
"name": "Sofware Development",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 13,
"category_type_id": 3,
"name": "Mangement",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 14,
"category_type_id": 3,
"name": "Tourism",
"created_at": "2019-05-19 13:27:35",
"updated_at": "2019-05-19 13:27:36"
},
{
"id": 16,
"category_type_id": 3,
"name": "Geography",
"created_at": "2019-05-19 13:33:36",
"updated_at": "2019-05-19 13:33:37"
}
],
"sub_categories": [
{
"id": 6,
"category_type_id": 3,
"main_category_id": 11,
"name": "PHP",
"created_at": "2019-05-19 13:42:29",
"updated_at": "2019-05-19 13:42:30"
},
{
"id": 7,
"category_type_id": 3,
"main_category_id": 11,
"name": "Ruby",
"created_at": "2019-05-19 13:42:31",
"updated_at": "2019-05-19 13:42:32"
},
{
"id": 8,
"category_type_id": 3,
"main_category_id": 11,
"name": "Java",
"created_at": "2019-05-19 13:42:33",
"updated_at": "2019-05-19 13:42:33"
}
]
}
]
希望这对您有所帮助。并且请阅读Laravel文档以获取更多信息