I want to combine two data search results into one array, I use array_merge
but there is an array_merge()
error:
Argument # 1 is not an array
How to turn $vendor
's eloquent results into an array and combine it with $plucked
?
$vendor = Vendor::find($id);
$vendor_detail = VendorDetail::where('vendor_id',$id)->get();
$plucked = $vendor_detail->pluck('vendor_profile_value','vendor_profile_name');
$coba = array_merge($vendor,$plucked);
$plucked
already an array I think the problem here is that $vendor
is not yet an array
You could do it like this:
$vendor = Vendor::find($id);
$vendor_details = VendorDetail
::select('vendor_profile_value', 'vendor_profile_name')
->where('vendor_id', $id)
->get()
->toArray();
$coba = array_merge($vendor,$vendor_details);
The get()
method execute the query returning a Collection instance, in which you can call the toArray()
method.
As far as I can see, you could make use ofrelationships and eager loading .
If you have aone-to-many relationship defined like this in your Vendor
model:
public function details()
{
return $this->hasMany(VendorDetails::class);
}
Then, you could eager load the relationship like this:
$vendor = Vendor::with('details')->find($id);
// ^^^^^^^^^^^^^^
You could even just load the wanted fields:
$vendor = Vendor::with('details:vendor_profile_value,vendor_profile_name')
->find($id);
Then, your object will have a new attribute called "details" containing the related objects (or a collection of the limited selected fields).
您可以将 $vendor 转换为如下所示的数组。
$vendor = Vendor::find($id)->toArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.