1

i am new here, i have a problem with an api in laravel. I want to show all the records from two tables. Table a : Flights; Table 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
            ]);
        }
    }
}

I tried with array_merge(table A, table B) but it didn't work.

Someone can help me?

Ghost88
  • 23
  • 5

3 Answers3

0

You can convert them to array, then merge them in a new array called for example data. Below code may help you:

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

return response()->json([
            'success' => true,
            'length' => $flights->count(),
            'results' => $data
        ]);
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
0

You can try this https://stackoverflow.com/a/30524136/1529662 And your $flights and $details variables are collection. If you want to convert, you should use toArray() method.

Raci
  • 150
  • 2
  • 5
0

There can be two scenarios:

  1. Flight record hasOne/hasMany FlightDetail records (related)

    • flight_details table will have a foreign key flight_id
  2. Flight and FlightDetail are not related

    • flight_details table doesn't have foreign key flight_id

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

Scenario 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
                ]
            ]);
        }
    }
}
Donkarnash
  • 12,433
  • 5
  • 26
  • 37