Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think this can help you;

$results = DB::table('employees')
               ->join('groups', 'employees.group_id', '=', 'groups.id')
               ->select('employees.*', 'groups.name')
               ->get();
Last updated 8 years ago.
0

Thank you dorgelesn for the answer.

But i would like to understund, is it possibile to get that in relational way ?

Like i have 2 tables, employees and groups, from the table employees il will get all the data, and from the groups i will get only the name.

Employees Groups id id group_id name first_name description .... ....

Thank you in advance

0

If you have set up an Eloquent relationship like this in your Employee.php model:

public function group()
{
    return $this->belongsTo('Group');
}

You should then be able to do this in your Employee Controller

$employees = Employee::all();

foreach ($employees as $employee)
{
   $group_name = $employee->group->name;
}

Not sure if that helps or not...

Cheers

Last updated 8 years ago.
0

Thanks for your help, i figure it out in this way, amybe this can help someone else

$employees = $this->employeesRepository->with('groups')->all() ->map(function ($q) { return [ 'id' => $q->id, 'group_name' => $q->roles->name, 'employee => $q->employee, ]; });

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Jimmy19 jimmy19 Joined 14 Feb 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.