1

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()]);
        }
    }
}
Al.G.
  • 4,327
  • 6
  • 31
  • 56
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • where in your code is your try block located? A Controller? a Class? – idelara Nov 03 '17 at 20:11
  • Sorry, the class of my controller. I updated with the whole class. But yes, this is the controller for that view – Geoff_S Nov 03 '17 at 20:12
  • Ok... Another problem... what route are you accessing, and what method in your controller is bound to such route? – idelara Nov 03 '17 at 20:15
  • The route, called routes.shop.php is ```/** * Build Order Form */ Route::post('build', 'Shop\BuildOrderFormController@store') ->name('shop.order.build');``` and the method is ```store``` – Geoff_S Nov 03 '17 at 20:19
  • Is that the route executing when DISPLAYING the form, or when SUBMITTING the form? – idelara Nov 03 '17 at 20:21
  • Also, what is the name of that view you are trying to render data to? Is it `Shop.Order.show` as well? – idelara Nov 03 '17 at 20:26

2 Answers2

2

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!

idelara
  • 1,786
  • 4
  • 24
  • 48
-1
<?php 

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

 ?>

The functionName should have the 'static' keyword e.g

Controller function:

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

I saw the code from this link How to call a controller function inside a view in laravel 5

for example:

Route:

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

MiscController:

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

blade template view:

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

OUTPUT:

error hello world