在Laravel视图中传递数组值

时间:2018-03-12 14:55:02

标签: php mysql laravel eloquent relationships

你能帮我解决这个问题吗?我试图显示来自foreach数组的值。关系让我理解起来更复杂所以我选择在我的路上做。

显示如下错误

此集合实例上不存在属性[pic]。

0xfffffff8

我的观点是

$dataTagged = DB::table('tagging_tagged')
                ->where('tag_slug', '=',$slug)
                ->get();
foreach($dataTagged as $tagged){
$dataProductquantity[] = Productquantity::where('id','=',$tagged->taggable_id)->first();    
}

请帮忙。谢谢

2 个答案:

答案 0 :(得分:2)

另一种方式可能是

public function methodName($slug) { 
    // Obtaining the TaggingTagged that include the needed slug. 
    $dataTagged = DB::table('tagging_tagged')->where('tag_slug', '=',$slug)->get();
    // or using the Model to obtain the data
    // $dataTagged = TaggingTagged::where('tag_slug', '=',$slug)->get();

    // Then Iterate into the $dataTagged Collection, the result is a new Collection
    $dataProductquantity = $dataTagged->each(function ($tagged, $key) {
        return Productquantity::where('id','=',$tagged->taggable_id)->first();
    });

    // Passing the $dataProductquantity to the view. You must need replace "viewName"
    return view('viewName')->with(['dataProductquantity' => $dataProductquantity]);
}

答案 1 :(得分:1)

试试这个

$dataProductquantity = [];

foreach($dataTagged as $tagged){
   array_push($dataProductquantity,Productquantity::where('id','=',$tagged->taggable_id)->first());
}