Brendan
Brendan

Reputation: 107

Laravel Datatables Referencing data from a table which needs to reference another table

I am having an issue where I am trying to pull data from a secondary reference table using Ajax.

I have the following tables in my Database


    Members
    |id|camp_id|membership|first_name|last_name|squadron|...
    
    
    flight
    |id|flight_number|flight_name|
    
    Huts
    |id|hut_name|room_number|
    
    Member_mapping
    |id|flight_id|hut_id|

In my Ajax query I have the following


    public function getMemberlist(Request $request)
        {
           if ($request->ajax()) {
    
            $camp = Campmapping::latest()->value('id');
    
              $members=Member::where('camp_id',$camp)->with('membermap')->get();
    
                return DataTables::of($members)
                    ->addColumn('action', function($row){
    
                        $btn = '<a href="'.action('MembersController@show', $row->id).'" target="_blank" title="View" class="btn btn-round btn-success"><i class="fa fa-info"></i></a>';
    
                        return $btn;
                    })
    
    
    
                ->make(true);
            }
        }

Now when running the code I get the following response


    {draw: 1, recordsTotal: 1, recordsFiltered: 1,…}
    data: [{id: 1, camp_id: 1, membership: "N18094", first_name: "Brendan", last_name: "Fox", gender: "M",…}]
    0: {id: 1, camp_id: 1, membership: "N18094", first_name: "Brendan", last_name: "Fox", gender: "M",…}
    action: "<a href=\"http://127.0.0.1:8000/members/1\" target=\"_blank\" title=\"View\" class=\"btn btn-round btn-success\"><i class=\"fa fa-info\"></i></a>"
    band: "0"
    camp_id: 1
    checked_in: "N"
    created_at: null
    duty_nco: null
    first_name: "Brendan"
    flight_id: 1
    form17: "0"
    gender: "M"
    id: 1
    last_name: "Fox"
    membermap: {id: 1, camp_id: 1, member_id: 1, flight_id: 1, hut_id: 1, created_at: null, updated_at: null}
        camp_id: 1
        created_at: null
        flight_id: 1
        hut_id: 1
        id: 1
        member_id: 1
        updated_at: null
    membership: "N18094"
    paid: "0"
    rank: "Gp Comr"
    unit_id: 32
    unitmap: {id: 32, unit: "NSW Group", created_at: "2022-07-12T10:23:47.000000Z",…}
    updated_at: null
    draw: 1
    input: {draw: "1",…}
    recordsFiltered: 1
    recordsTotal: 1

On my member model I have the following:


    public function unitmap()
        {
            return $this->hasOne('App\Unit', 'id', 'unit_id');
        }
    
        public function membermap()
        {
            return $this->hasOne('App\member_mapping', 'member_id', 'id');
        }

On my Member_mapping Model I have the following:


    public function member()
        {
            return $this->hasOne('App\Member', 'id', 'member_id');
        }
    
        public function flight()
        {
            return $this->hasOne('App\Flight', 'id', 'flight_id');
        }
    
        public function room()
        {
            return $this->hasOne('App\Room', 'id', 'room_id');
        }

The goal is to be able to grab the Hut Name, Room Name from the Rooms table and the Flight Name from the flight table. As you can see from my response I am getting the database records from the Member_mapping table under the membermap tag, but I need to go another level deeper for these.

Thanks

Upvotes: 0

Views: 104

Answers (1)

Brendan
Brendan

Reputation: 107

Not worry, I found a solution by adding the following line to my ajax request


->addColumn('flightname', function($row){
                    return $row->membermap->flight->flight_name;

This solved my issue

Upvotes: 1

Related Questions