belongsToMany
如何在controller中利用一个多对多关联的中间表调用另一个表的数据
飞翔的小猪 回复了问题 • 4 人关注 • 2 个回复 • 5165 次浏览 • 2015-07-16 16:39
多对多关联查询问题,起别名
突破 回复了问题 • 5 人关注 • 4 个回复 • 6402 次浏览 • 2016-11-26 11:02
自己写一个函数处理一下数据即可。
1. 如果喜欢封装在model里
{{{
public static function infoReformat($obj) {
return [
"id"=> $obj->id,... 显示全部 »
1. 如果喜欢封装在model里
{{{
public static function infoReformat($obj) {
return [
"id"=> $obj->id,... 显示全部 »
自己写一个函数处理一下数据即可。
1. 如果喜欢封装在model里
{{{
public static function infoReformat($obj) {
return [
"id"=> $obj->id,
"name"=> $obj->name,
"phone"=> $obj->phone,
"address"=> $obj->address,
"class_id"=> $obj->img->id,
"class_name"=> $obj->img->origin_name,
"image_path"=> $obj->img->local_path,
];
}
}}}
接着在Controller里这样用 return Response::json(YourModel::infoReformat($obj));
2. 如果喜欢封装在Controller里
{{{
public function infoReformat($obj) {
return Response::json([
"id"=> $obj->id,
"name"=> $obj->name,
"phone"=> $obj->phone,
"address"=> $obj->address,
"class_id"=> $obj->img->id,
"class_name"=> $obj->img->origin_name,
"image_path"=> $obj->img->local_path,
]);
}
}}}
1. 如果喜欢封装在model里
{{{
public static function infoReformat($obj) {
return [
"id"=> $obj->id,
"name"=> $obj->name,
"phone"=> $obj->phone,
"address"=> $obj->address,
"class_id"=> $obj->img->id,
"class_name"=> $obj->img->origin_name,
"image_path"=> $obj->img->local_path,
];
}
}}}
接着在Controller里这样用 return Response::json(YourModel::infoReformat($obj));
2. 如果喜欢封装在Controller里
{{{
public function infoReformat($obj) {
return Response::json([
"id"=> $obj->id,
"name"=> $obj->name,
"phone"=> $obj->phone,
"address"=> $obj->address,
"class_id"=> $obj->img->id,
"class_name"=> $obj->img->origin_name,
"image_path"=> $obj->img->local_path,
]);
}
}}}