dongzhiyong8577 2017-11-03 19:58
浏览 55
已采纳

将函数的数据放入刀片,laravel

I have following code in a function in my laravel controller:

try {
    return json_encode(FormBuilder::BuildOrderForm($dealer, $aa, $form));
} catch (OPSException $e) {
    return json_encode(['error' => $e->getUserMessage()]);
} catch (Exception $e) {
    return json_encode(['error' => $e->getMessage()]);
}

This is returning the data I need in a JSON object. I'm trying to recreate this to build the data within html code in my laravel blade. what's the proper way to put the above FormBuilder::BuildOrderForm($dealer, $aa, $form) into my blade here:

@section('content')

<h3 style="font-size: 26px; padding: 10px 0;"> {{ <!-- This is where I need the data --> }} </h3>
<p class="uk-text-muted" style="font-size: 20px;" ></p>

<div class="uk-grid">
    <div class="uk-width-2-10">
        <ul style="margin: 0; padding: 0; list-style-type: none; float: left; width: 100%;">

        </ul>
    </div>
@endsection

The whole class in controller:

class OrderController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $ordering_access = AttributesList::with('attr_info')
            ->where('attr_id', AttributesList::attrId('ordering_toggle'))
            ->where('data', $this->dealer)
            ->count();

        if ($ordering_access > 0) {
            \Session::flash('error_message', "You are not allowed to order. Please contact your CSR!");
            $attributeid = 100000;
        } else {
            $attributeid = 6;
        }
        Access::Check($attributeid);
        $this->dealer = Access::getAttrValue('dealer_num');
    }

    public function index()
    {
        $forms = FormBuilder::Orderforms($this->dealer);
        return view('Shop.Order.index')->with('forms', $forms);
    }


    public function show($id)
    {
        $cart_num = Access::getAttrValue('cart_num');
        if (!$cart_num) {
            $cart = 0;
        } else {
            $find_cart = Cart::find($cart_num);
            if ($find_cart  && $find_cart->orderform == $id) {
                $cart = $cart_num;
            } else {
                $cart = 0;
            }
        }
        return view('Shop.Order.show')
            ->with('dealer', $this->dealer)
            ->with('cart', $cart)
            ->with('form', $id);
    }

    public function store(Request $request) {
        $this->validate($request, [
            'dealer' => 'required|numeric',
            'form' => 'required',
        ]);

        // check to make sure this dealer can order from this formid
        $dealer = Input::get('dealer');
        $form = Input::get('form');

        $aa = FormBuilder::getcompanyfromform($form) + 0;

        $comp = CustomerDataNew::getcomp($dealer);

        if (!in_array($aa, $comp)) {
            return "Error: Dealer can't use this form";
        }

        try {
            return json_encode(FormBuilder::BuildOrderForm($dealer, $aa, $form));
        } catch (OPSException $e) {
            return json_encode(['error' => $e->getUserMessage()]);
        } catch (Exception $e) {
            return json_encode(['error' => $e->getMessage()]);
        }
    }
}
  • 写回答

2条回答 默认 最新

  • doubiao7410 2017-11-03 20:39
    关注

    You are just returning the encoded data, therefore you must be seeing a bunch of JSON (or errors if you are catching any exceptions).

    In order to display any data, you have to do the same thing you are doing in your show() method, that is, return a view and render any data inside that particular view or partial.

    Try the following:

    public function store(Request $request) {
    
    ...
    
        try {
    
            $data = json_encode(FormBuilder::BuildOrderForm($dealer, $aa, $form));
            return view('Shop.Order.show')->with('data', $data);
    
        } catch (OPSException $e) {
            return $this->returnFromException($e->getUserMessage());
        } catch (Exception $e) {
            return $this->returnFromException($e->getMessage());
        }
    }
    
    public function returnFromException($error)
    {
        $error = json_encode(['error' => $error]);
        return view('Shop.Order.show')->with('error', $error);
    }
    

    Now, in your view, using Blade you can display the data like so:

    @section('content')
    
        @if(isset($error))    
            <h3 style="font-size: 26px; padding: 10px 0;">{{ dd($error) }} </h3>
        @else if(isset($data))
            {{ dd($data) }}
        @endif
        <p class="uk-text-muted" style="font-size: 20px;" ></p>
    
        <div class="uk-grid">
            <div class="uk-width-2-10">
                <ul style="margin: 0; padding: 0; list-style-type: none; float: left; width: 100%;">
    
                </ul>
            </div>
        </div>
    
    @endsection
    

    Note that you will have to get rid of the dd()s in the Blade view, since it with halt the HTML rendering. You will have to use something like @foreach() to render an array or JSON.

    I hope this helps!

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料