I think this can help you;
$results = DB::table('employees')
->join('groups', 'employees.group_id', '=', 'groups.id')
->select('employees.*', 'groups.name')
->get();
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
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
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, ]; });
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community