如何使用 Laravel 中的 Api 获取两个表中的所有记录

问题描述 投票:0回答:3

我是新来的,我在 laravel 中的 api 有问题。 我想显示两个表中的所有记录。 表a:航班; 表b:详细信息_航班;

class FilterController extends Controller
{
    public function product(Request $request) {

        $flights= Flight::all();
        $details = Flights_detail::all();

        if(empty($flights)) {
            return response()->json([
                'success' => true,
                'length' => 0,
                'error' => 'No fly found',
                'results' => []
            ]);
        }else {
            return response()->json([
                'success' => true,
                'length' => $flights->count(),
                'results' => $flights
            ]);
        }
    }
}

我尝试使用 array_merge(table A, table B) 但没有成功。

有人可以帮助我吗?

php json laravel api
3个回答
0
投票

您可以将它们转换为数组,然后将它们合并到一个新数组中,例如称为

data
。下面的代码可能会帮助你:

$data=[
   'flights' => $flights->toArray(),
   'details' => $details->toArray(),
];

return response()->json([
            'success' => true,
            'length' => $flights->count(),
            'results' => $data
        ]);

0
投票

你可以尝试这个https://stackoverflow.com/a/30524136/1529662 您的 $flights 和 $details 变量是集合。如果你想转换,你应该使用 toArray() 方法。


0
投票

可能有两种情况:

  1. 航班记录 hasOne/hasMany FlightDetail 记录(相关)

    • flight_details表将有一个外键flight_id
  2. Flight 和 FlightDetail 不相关

    • flight_details表没有外键flight_id

场景1

//Models

class Flight extends Model
{
    public function details()
    {
        return $this->hasMany(FlightDetail::class, 'flight_id', 'id');
    }
}


class FlightDetail extends Model
{
    public function flight()
    {
        return $this->belongsTo(Flight::class, 'flight_id', 'id');
    }
}


class FilterController extends Controller
{
    public function product(Request $request) {

        $flights= Flight::with('details`)->get();
        if(empty($flights)) {
            return response()->json([
                'success' => true,
                'length' => 0,
                'error' => 'No fly found',
                'results' => []
            ]);
        }else {
            return response()->json([
                'success' => true,
                'length' => $flights->count(),
                'results' => $flights
            ]);
        }
    }
}

场景2

class FilterController extends Controller
{
    public function product(Request $request) {

        $flights= Flight::all();
        $details = Flights_detail::all();

        if(empty($flights)) {
            return response()->json([
                'success' => true,
                'length' => 0,
                'error' => 'No fly found',
                'results' => []
            ]);
        }else {
            return response()->json([
                'success' => true,
                'length' => [
                    'flights' => $flights->count(), 
                    'flightDetails' => $details->count()
                ],
                'results' => [
                    'flights' => $flights, 
                    'flightDetails' => $details
                ]
            ]);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.