如何 return json 数组中的对象
how to return an object inside an array in json
我有这样的饭菜模型
class meals extends Model
{
use HasFactory;
protected $appends =
[
'image_full_path',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'text','price','image_url','rating','discount','sub_catogry_id'
];
public function sub_catogry()
{
return $this->belongsTo('App\Models\sub_catogry');
}
public function order_meal_list()
{
return $this->hasMany(order_meal_list::class);
}
我有Oreder_List_Model
class order_meal_list extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'quntity', 'price','order_id','meals_id'
];
public function meals()
{
return $this->belongsTo('App\Models\meals','meals_id');
}
public function order()
{
return $this->belongsTo('App\Models\order');
}
}
我有这样的订单模型
class order extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
"id",
'name','address','phone','dateTime','user_id','total_price','payment_mthod_id','order_status'
];
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function meals_list()
{
return $this->HasMany('App\Models\order_meal_list',"order_id");
}
总结是我有订单,订单可能不止餐点,所以我制作了一个 order_meal_list table 来存储每个订单中的餐点列表,现在这些 table有餐号和订单号
我想return订单及其餐单,在列表中的每个项目旁边我想使用餐 ID 获取与其相关的餐对象
我尝试像这样使用
$orders = order::with("meals_list")->where('user_id','=', $request['user_id'])->get();
但对我来说只是 return 没有像这样的餐点的订单列表
[
[
{
"id": 11,
"name": "ali",
"address": "lap lap",
"phone": "105",
"dateTime": "02:50",
"total_price": "452.11356",
"toatal_discount": "82",
"user_id": "1",
"order_status": "1",
"payment_mthod_id": "0",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-11T09:02:16.000000Z",
"meals_list": [
{
"id": 11,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
},
{
"id": 12,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
}
]
},
我的问题是我想 return 对我的 api 做出回应,并且回应必须是 json 并且餐单对象中的餐点看起来像这样 json =18=]
[
{
"id": 11,
"name": "ali",
"address": "lap lap",
"phone": "105",
"dateTime": "02:50",
"total_price": "452.11356",
"toatal_discount": "82",
"user_id": "1",
"order_status": "1",
"payment_mthod_id": "0",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-11T09:02:16.000000Z",
"meals_list": [
{
"id": 11,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
},
{
"id": 12,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
"meal":[{
"id": 1,
"name": "Royal Casper",
"text": "Est qui ut in dolor aut perferendis exercitationem dolorem. Aut porro et nisi non
fugiat. Rerum non nisi ex officia cupiditate. Dolores ut quae ea.",
"price": "3890118.5463257",
"rating": 0.8,
"image_url": "3ea216eed15b143016c565c53ca22544.jpg",
"discount": 94,
"sub_catogry_id": "6",
"created_at": "2020-10-11T07:15:33.000000Z",
"updated_at": "2020-10-11T07:15:33.000000Z",
"image_full_path": "\/images\/3ea216eed15b143016c565c53ca22544.jpg",
"sub_catogry": {
"id": 6,
"name": "Prof. Spencer Kub I",
"catogry_id": "7",
"created_at": "2020-10-11T07:15:18.000000Z",
"updated_at": "2020-10-11T07:15:18.000000Z"
}]
}
]
},
改用此代码:
$orders = order::with("meals_list.meals")->where('user_id','=', $request['user_id'])->get();
这会将食物加载到。
我有这样的饭菜模型
class meals extends Model
{
use HasFactory;
protected $appends =
[
'image_full_path',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'text','price','image_url','rating','discount','sub_catogry_id'
];
public function sub_catogry()
{
return $this->belongsTo('App\Models\sub_catogry');
}
public function order_meal_list()
{
return $this->hasMany(order_meal_list::class);
}
我有Oreder_List_Model
class order_meal_list extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'quntity', 'price','order_id','meals_id'
];
public function meals()
{
return $this->belongsTo('App\Models\meals','meals_id');
}
public function order()
{
return $this->belongsTo('App\Models\order');
}
}
我有这样的订单模型
class order extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
"id",
'name','address','phone','dateTime','user_id','total_price','payment_mthod_id','order_status'
];
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function meals_list()
{
return $this->HasMany('App\Models\order_meal_list',"order_id");
}
总结是我有订单,订单可能不止餐点,所以我制作了一个 order_meal_list table 来存储每个订单中的餐点列表,现在这些 table有餐号和订单号
我想return订单及其餐单,在列表中的每个项目旁边我想使用餐 ID 获取与其相关的餐对象
我尝试像这样使用
$orders = order::with("meals_list")->where('user_id','=', $request['user_id'])->get();
但对我来说只是 return 没有像这样的餐点的订单列表
[
[
{
"id": 11,
"name": "ali",
"address": "lap lap",
"phone": "105",
"dateTime": "02:50",
"total_price": "452.11356",
"toatal_discount": "82",
"user_id": "1",
"order_status": "1",
"payment_mthod_id": "0",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-11T09:02:16.000000Z",
"meals_list": [
{
"id": 11,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
},
{
"id": 12,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
}
]
},
我的问题是我想 return 对我的 api 做出回应,并且回应必须是 json 并且餐单对象中的餐点看起来像这样 json =18=]
[
{
"id": 11,
"name": "ali",
"address": "lap lap",
"phone": "105",
"dateTime": "02:50",
"total_price": "452.11356",
"toatal_discount": "82",
"user_id": "1",
"order_status": "1",
"payment_mthod_id": "0",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-11T09:02:16.000000Z",
"meals_list": [
{
"id": 11,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
},
{
"id": 12,
"meals_id": "1",
"order_id": "11",
"quntity": 3,
"price": "226.05678",
"created_at": "2020-10-04T12:27:55.000000Z",
"updated_at": "2020-10-04T12:27:55.000000Z"
"meal":[{
"id": 1,
"name": "Royal Casper",
"text": "Est qui ut in dolor aut perferendis exercitationem dolorem. Aut porro et nisi non
fugiat. Rerum non nisi ex officia cupiditate. Dolores ut quae ea.",
"price": "3890118.5463257",
"rating": 0.8,
"image_url": "3ea216eed15b143016c565c53ca22544.jpg",
"discount": 94,
"sub_catogry_id": "6",
"created_at": "2020-10-11T07:15:33.000000Z",
"updated_at": "2020-10-11T07:15:33.000000Z",
"image_full_path": "\/images\/3ea216eed15b143016c565c53ca22544.jpg",
"sub_catogry": {
"id": 6,
"name": "Prof. Spencer Kub I",
"catogry_id": "7",
"created_at": "2020-10-11T07:15:18.000000Z",
"updated_at": "2020-10-11T07:15:18.000000Z"
}]
}
]
},
改用此代码:
$orders = order::with("meals_list.meals")->where('user_id','=', $request['user_id'])->get();
这会将食物加载到。