Yajra数据表中的Foreach Laravel [英] Foreach in Yajra DataTable Laravel

查看:120
本文介绍了Yajra数据表中的Foreach Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据表中放入一个foreach循环,但无法正常工作,
P.S.如果我删除了foreach,一切都可以正常运行,则此处附加的
是我的代码

I'm trying to put a foreach loop inside my datatable but it wont work, P.S. if I remove the foreach everything works fine already, attached here is my code

$Product = Product::query();
$colors   = Color::all();

return Datatables::eloquent($Product)

    ->addColumn('category_name', function($row) {

        $category = Category::select('name')->where('id', $row->category_id )->pluck('name')->toArray();
        return $category;

    })

    ->addColumn('add_color', function($row) {

        $return = 
            '<form class="form-inline" method="post" action="/procurement/add-product" style="max-width: 170px;">
            <input type="hidden" name= "product_id" value="' . $row->id . '">

            <div class="form-group">
            <select name="color_id" class="form-control" required autofocus>
                    '.foreach ($colors as $color){.' 
                    <option value="test">test</option>'.}.'
            </select>
            </div>';

        return $return;

});


推荐答案

您需要在回报之外执行foreach。然后您也不需要使用或将 $ color 变量导入数据表中。

You need to perform foreach outside your return. and then you also need no use or import the $color variable inside your data table. something like this ..

$Product = Product::query();
$colors   = Color::all();

return Datatables::eloquent($Product)

->addColumn('category_name', function($row) {

    $category = Category::select('name')->where('id', $row->category_id )->pluck('name')->toArray();
    return $category;

})

->addColumn('add_color', function($row) use ($colors)  {
        $options = '';
        foreach ($colors as $color) {
            $options .= '<option value="test">$color</option>';
        }

    $return = 
        '<form class="form-inline" method="post" action="/procurement/add-product" style="max-width: 170px;">
        <input type="hidden" name= "product_id" value="' . $row->id . '">

        <div class="form-group">
        <select name="color_id" class="form-control" required autofocus>
        </select>
        </div>';

    return $return;

});

这篇关于Yajra数据表中的Foreach Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆