laravel4-datatables-package icon indicating copy to clipboard operation
laravel4-datatables-package copied to clipboard

Merging data from multiple table into 1 data table

Open bczm8703 opened this issue 11 years ago • 5 comments

Hi. I would like to check whether is it possible to merge data from more than 1 table in a datatable. Eg:

  • leave table having column: Employee Name, Leave Type, start_date, end_date, status
  • Off table having column: Employee Name, Off Type, start_date, end_date, status
  • Mc table having column: Employee Name, Reason, date, status

Therefore is it possible to get all the record from this 3 tables and put them into 1 datatable?

bczm8703 avatar Dec 05 '14 02:12 bczm8703

You mean how to do a SQL JOIN? DB::table('leave')->select([..yourcolumns..])->join('off'..... What code do you have so far?

MarkVaughn avatar Dec 05 '14 16:12 MarkVaughn

I am able tl extract the individual data from their respective table eg: Leave::select('status', 'start_date', 'end_date', 'type');

I cant join them with sql as their column name are different... is there anyway to merge the data of different eloquent into 1?

bczm8703 avatar Dec 05 '14 16:12 bczm8703

Honestly I don't really understand what you mean by merge. If you use JOIN, you can SELECT with aliases e:g: 'off.start_date AS off_start_date',' leave.start_date AS leave_start_date' etc....

Mark Vaughn Mobile: +1 (770) 533-2274

On Fri, Dec 5, 2014 at 11:50 AM, bczm8703 [email protected] wrote:

I am able tl extract the individual data from their respective table eg: Leave::select('status', 'start_date', 'end_date', 'type');

I cant join them with sql as their column name are different... is there anyway to merge the data of different eloquent into 1?

— Reply to this email directly or view it on GitHub https://github.com/bllim/laravel4-datatables-package/issues/178#issuecomment-65817678 .

MarkVaughn avatar Dec 05 '14 19:12 MarkVaughn

sorry for not being clear with my question. what i meant is; for example i have 3 tables in my database. Leave table, MC table and Off table.

if i wanted to use a data table for each of this table i would do this;

  • $leaves = Leave::where('status', 'pending')->select('employee_name', 'leave_type', 'leave_start_date', 'leave_end_date', 'status')->orderBy('created_at', 'desc');
  • $offs = Off::where('status', 'pending')->select('employee_name', 'off_start_date', 'off_end_date', 'status')->orderBy('created_at', 'desc');
  • $mcs = Mc::where('status', 'pending')->select('employee_name', 'mc_date', 'reason', 'status')->orderBy('created_at', 'desc');

My question now is, is it possible for $leaves, $offs and $mcs to be place into 1 data table only?

bczm8703 avatar Dec 08 '14 01:12 bczm8703

Yes, it is possible. Look at example 6 of the readme. In that, the profiles table is being joined with the user table and it displays data from both.

phazei avatar Dec 08 '14 02:12 phazei