【问题标题】:How can I take all records from two table with an Api in Laravel如何在 Laravel 中使用 Api 从两个表中获取所有记录
【发布时间】:2021-03-17 22:13:13
【问题描述】:

我是新来的,我对 laravel 中的 api 有疑问。 我想显示两个表中的所有记录。 表 a:航班; 表b:Details_flights;

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),但没有成功。

有人可以帮助我吗?

【问题讨论】:

    标签: json laravel api


    【解决方案1】:

    您可以将它们转换为数组,然后将它们合并到一个名为 data 的新数组中。下面的代码可以帮助你:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        可能有两种情况:

        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
                        ]
                    ]);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-15
          • 2020-02-04
          相关资源
          最近更新 更多