Laravel将列作为数组列表

时间:2015-10-02 03:36:08

标签: php laravel eloquent

使用Eloquent laravel orm进行查询时,如何迭代某列?

import sys 
new_path = "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages" # or wherever your pip install drops things
sys.path.append(new_path)
import pandas

public function getProductAPI(Request $request) { $id = JWTAuth::parseToken()->authenticate()->id; $product = Product::where('user_id',$id)->get()->toArray(); foreach ($product->description as $desc) { dd($desc); } dd($product); return response()->json($product); } $product->description都返回错误$product["description"]Undefined index: description

编辑:

这是dd($ product)的输出:

Trying to get property of non-object

我无法专门获取修改列。

谢谢!

3 个答案:

答案 0 :(得分:2)

您不需要使用toArray();

检查:

public function getProductAPI(Request $request)
{

    $id = JWTAuth::parseToken()->authenticate()->id;

    $product = Product::where('user_id',$id)->get();

    foreach ($product as $desc) { 

        echo dd($desc->description);
    }
    echo dd($product);


    return response()->json($product);
}

答案 1 :(得分:0)

您的$ product变量是一个数组。试试$ product [0] [' description']。 希望对你有所帮助。

答案 2 :(得分:0)

on function getProductAPI: {

$uniq_id = JWTAuth::parseToken()->authenticate()->id;

$product = Product::where('user_id',$uniq_id)->get()->toArray();

foreach ($product as $desc) { 

    dd($desc->description);
}
echo dd($product);


return response()->json($product);

}