如何将雄辩的结果放入数组Laravel

时间:2019-12-09 04:13:53

标签: php laravel laravel-5 eloquent

我想将两个数据搜索结果合并到一个数组中,我使用array_merge,但有一个array_merge()错误:

  

参数1不是数组

如何将$vendor的雄辩结果转换为数组并将其与$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已经是数组 我认为这里的问题是$vendor还不是数组

2 个答案:

答案 0 :(得分:1)

您可以这样做:

$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);

get()方法执行查询,返回一个Collection实例,您可以在其中调用toArray()方法。


旁注

据我所知,您可以使用relationshipseager loading

如果您的Vendor模型中有这样定义的one-to-many relationship

public function details()
{
    return $this->hasMany(VendorDetails::class);
}

然后,您可以eager load这样的关系:

$vendor = Vendor::with('details')->find($id);
//                ^^^^^^^^^^^^^^

您甚至可以只加载所需的字段:

$vendor = Vendor::with('details:vendor_profile_value,vendor_profile_name')
                ->find($id);

然后,您的对象将具有一个名为“详细信息”的新属性,其中包含相关对象(或有限的选定字段的集合)。

答案 1 :(得分:0)

您可以将$ vendor转换为如下所示的数组。

    I+00000.313: enable_attach(('127.0.0.1', 9000), './NilLogs', True)

    I+00000.313: Won't trace filenames starting with: [
                     "/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd"
                 ]
                 Won't trace filenames ending with: [
                     "ptvsd_launcher.py"
                 ]

    I+00000.313: enable_attach() spawning adapter: ['/Users/username/Repo/MyAppClientDirectory/3P/BUILD_PY/3.6.5/macosx10.6/Python.framework/Versions/3.6/bin/python', '/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/adapter', '--host', '127.0.0.1', '--port', '9000', '--for-enable-attach', '--log-dir', './NilLogs']

    I+00000.456: line 104 : b''

    I+00000.456: line 108 : ''

    E+00000.457: enable_attach() failed:

                 Traceback (most recent call last):
                   File "/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/server/api.py", line 71, in debug
                     return func(start_patterns, end_patterns)
                   File "/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/server/api.py", line 111, in enable_attach
                     connection_details = json.JSONDecoder().decode(line)
                   File "/Users/username/Repo/MyAppClientDirectory/Output/MAC64/Debug/MyApp360.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.7/json/decoder.py", line 337, in decode
                     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
                   File "/Users/username/Repo/MyAppClientDirectory/Output/MAC64/Debug/MyApp360.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.7/json/decoder.py", line 355, in raw_decode
                     raise JSONDecodeError("Expecting value", s, err.value) from None
                 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

                 Stack where logged:
                   File "<string>", line 2, in <module>
                   File "/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/__init__.py", line 81, in enable_attach
                     return api.enable_attach(address, log_dir)
                   File "/Users/username/.vscode/extensions/ms-python.python-2019.11.50794/pythonFiles/lib/python/new_ptvsd/wheels/ptvsd/server/api.py", line 73, in debug
                     raise log.exception("{0}() failed:", func.__name__)