Rian Zaman
Rian Zaman

Reputation: 429

How to merge two queried results in Laravel Eloquent

I have a single form which I am using to create and edit information. I am using Form::model to show all the data in the edit form which are queried from the database. Now I had to add another form part whose data is being stored in a different table. But I need to show those data during edit in the same form. I have tried to put two parameter in the form:model which did not work or I am doing it wrong.

{!! Form::model([$employee_data,$edu_info],['route'=>['employee.update',$employee_data->id],'method'=>'put','files'=> true]) !!}

Then I tried to merge the queried data in my controller like this:

public function edit($id)
{
    $edit_info['title'] = 'Edit User';
    $edit_info['country'] = Country::all();
    $employee_basic = Employee::withTrashed()->where('id',$id)->first();
    $employee_edu = EmployeeEdu::withTrashed()->where('employee_id',$id)->first();
    $employee_all_data = $employee_basic->merge($employee_edu);
    $employee_all_data->all();
    $edit_info['employee_data'] = $employee_all_data;
    return view('admin.employee.edit',$edit_info);
}

This did not work either. I get the following error:

Call to undefined method Illuminate\Database\Query\Builder::merge()

How can I achieve my intended result?

EDIT: I tried to use ->get() instead of ->first(), In this case I do not get the error but my merge does not work as when I dd($employee_all_data) it gives me only the value of $employee_edu.

Upvotes: 0

Views: 1261

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

Try this:

$employee_all_data = $employee_basic->toArray() + $employee_edu->toArray();

Upvotes: 2

Related Questions