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

时间:2017-11-03 19:58:06

标签: php laravel laravel-blade

我在laravel控制器的函数中有以下代码:

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()]);
}

这是在JSON对象中返回我需要的数据。我正在尝试重新创建这个以在我的laravel刀片中的html代码中构建数据。将上述FormBuilder::BuildOrderForm($dealer, $aa, $form)置于我的刀片中的正确方法是什么:

@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

控制器中的全班:

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 个答案:

答案 0 :(得分:2)

您只是返回编码数据,因此您必须看到一堆JSON(如果您捕获任何异常,则会出错)。

为了显示任何数据,您必须在show()方法中执行相同的操作,即返回视图并在特定视图内部或部分呈现任何数据。

尝试以下方法:

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);
}

现在,在您看来,使用Blade可以显示如下数据:

@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

请注意,您必须在Blade视图中删除dd(),因为它会停止HTML呈现。您将不得不使用@foreach()之类的东西来渲染数组或JSON。

我希望这有帮助!

干杯!

答案 1 :(得分:-1)

<?php 

use App\Http\Controllers\ControllerName;
echo ControllerName::functionName();

 ?>

functionName应该具有&#39;静态&#39;关键字例如

控制器功能:

public static function functionName() {
return "Hello World!";
}

我看到了此链接的代码How to call a controller function inside a view in laravel 5

例如:

路线:

Route::get('checkTheTest', ['as' => 'checkrequest', 'uses' => 'MiscController@myTest']);

MiscController:

 public static function requesting($request){
        return 'error ' .$request;
    }

刀片模板视图:

<?php
 use App\Http\Controllers\MiscController;
 echo MiscController::requesting('hello world'); ?> 

输出:

error hello world