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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应