Reputation: 5107
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()]);
}
}
}
Upvotes: 1
Views: 1137
Reputation: 1816
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!
Upvotes: 2
Reputation: 29
<?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
Upvotes: -1