laravel 如何使用eloquent返回基于数据库关系的json响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24600013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to return a json response based on database relationship using eloquent
提问by Sven
I'm quite new to Laravel. I'm figuring out to work with Eloquent.
我对 Laravel 很陌生。我正在考虑与 Eloquent 合作。
Let's say I have 2 tables: categories and products. These two table have a one-to-many relationship. 1 category can have many products.
假设我有 2 个表:类别和产品。这两个表是一对多的关系。1个类别可以有很多产品。
I would like to return a JSON response which consists of an array of categories where each category has an array of products. It should look like this:
我想返回一个 JSON 响应,其中包含一个类别数组,其中每个类别都有一个产品数组。它应该是这样的:
[
{"name":"Category 1", "products": [
{"name":"Product 1","price":"2.50"},
{"name":"Product 2","price":"2.50"}
]
},
{"name":"Category 2", "products": [
{"name":"Product 1","price":"1.95"},
{"name":"Product 2","price":"2.15"}
]
}
]
Models:
楷模:
Category.php
分类.php
<?php
class Category extends Eloquent{
protected $table = 'categories';
public function products(){
return $this->hasMany('Product');
}
}
Product.php
产品.php
<?php
class Product extends Eloquent {
protected $table = 'products';
public function categories(){
return $this->belongsTo('Category');
}
}
Database tables:
数据库表:
create_categories_table:
创建_类别_表:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
public function up()
{
Schema::create('categories', function(Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
...
}
create_products_table:
创建_产品_表:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration {
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('price');
$table->unsignedInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
...
}
Controller:
控制器:
class ApiController extends BaseController {
类 ApiController 扩展了 BaseController {
public function getIndex(){
$categories = Category::all();
return Response::json(array('data' => $categories));
}
}
}
I know how to return all of the categories or products or only the name of a category but I cannot seem to find the solution for the example of json response I mentioned above.
我知道如何返回所有类别或产品或仅返回一个类别的名称,但我似乎无法找到我上面提到的 json 响应示例的解决方案。
If anyone knows this it would be really helpful. Thanks in advance.
如果有人知道这一点,那将非常有帮助。提前致谢。
回答by Joel Hinz
If I understood your question correctly, you could simply do this:
如果我正确理解你的问题,你可以简单地这样做:
public function getIndex(){
$categories = Category::with('Products')->get();
return Response::json(array('data' => $categories));
}
The with()
eager loads the relationship so you get pretty much what you want.
在with()
急于负荷的关系,所以你得到相当多,你想要什么。
By the way, you'll probably want to change the name of this method
顺便说一句,您可能想要更改此方法的名称
public function categories(){
return $this->belongsTo('Category');
}
to category()
instead, since every product can only belong to one category.
以category()
代替,因为每一个产品只能属于一个类别。