Sometimes You Need to Inject Pure PHP
Raw PHP in Blade Files: it’s not something that I advocate, but sometimes you just need to write some raw PHP in your Blade files.
Read the previous article on Including Subviews
Currently, the backend sends you the regular price of the product. You’ve asked for the backend team to send you just the final customer price since you don’t want to do any calculations in Blade and each customer has their own price. They’re busy and your boss is asking for the product demo today. What do you do? You use some raw PHP.
This example isn’t as obvious in our cases since we create the routes and those routes send information to the view. We, ourselves, could just modify the data that’s sent. Front-end developers normally don’t have that kind of access and instead rely on an API. If the API doesn’t send it back, they can’t modify it.
For this example, we’ll need a route and a view. Our route will send the product specs and price. Don’t worry about not having a login section, we’ll get to that sooner than later. Also, you won’t normally be sending information like this from the route. You’ll be doing it from the controller. We’ll get to all of that in due-time.
<?php
Route::get('/computers/apple/macbook-pro', function() {
return view('computers/show', [
'page_title' => 'Raw PHP',
'product_sku' => 'B0B3C5HNXJ',
'product_brand' => 'Apple',
'product_model' => 'MacBook Pro Laptop with M2 Chip',
'product_price' => 1299,
]);
});
The view will display the current price without any discount.
@include('subviews/header', ['title' => $page_title])
<ul>
<li>SKU: {{ $product_sku }}</li>
<li>Brand: {{ $product_brand }}</li>
<li>Model: {{ $product_model }}</li>
<li>Price: ${{ $product_price }}</li>
</ul>
@include('subviews/footer')
If you haven’t read my article on subviews, and the above information doesn’t make sense, take a moment to do so before continuing on.
For our demo, we just want to hardcode the discount and then remove it once backend gets their act together. To be obvious that we need to remove it, we’re going to create a new variable inside of our Blade file and set it to a specific discount.
To do this, we’ll need to surround our variable with raw PHP tags. Theoretically, we can just use the normal <?php ?>
tags, but Blade has directives for it: @php
and @endphp
.
@php
// @todo - remove this once backend gets their act together
$customer_discount = 20;
@endphp
A quick way to do discounts is:
Customer Price = $product_price * ((100 — $customer_discount)/100)
In the example above, we get:
$1299 * (80/100) = $1299 * .8 = $1039.2
We’ll also use the number_format
built in PHP function to change $1039.2
to $1039.20
.
@include('subviews/header', ['title' => $page_title])
@php
// @todo - remove this once backend gets their act together
$customer_discount = 20;
$discounted_price = $product_price * ((100 - $customer_discount)/100);
$discounted_price = number_format($discounted_price, 2, '.','');
@endphp
<ul>
<li>SKU: {{ $product_sku }}</li>
<li>Brand: {{ $product_brand }}</li>
<li>Model: {{ $product_model }}</li>
<li>Price: ${{ $discounted_price }}</li>
</ul>
@include('subviews/footer')
Refreshing our page, we get the desired outcome: http://0.0.0.0/computers/apple/macbook-pro
.
Pretty simple stuff. I can’t believe I managed to drag an entire article out of this one concept ????.
One last thing. Say that we just needed a simple variable. Just a one-liner. We can use the @php
directive without the closing @endphp
directive. Simply surround the code with parentheses.
@php($customer_discount = 20);
There are quite a few more Blade things that we need to cover, but they’re more advanced than where we’re at right now. We’ll touch on Layouts next and then move on to components.
Laravel Series
Continue your Laravel Learning.
Breaking Apart the Views
Laravel – P13: Including Subviews In Blade
Splitting apart the code is something that makes it easier to edit. How do you combine the files later? By including subviews into your Blade files.
Sometimes You Need to Inject Pure PHP
Laravel – P14: Raw PHP In Blade Files
Raw PHP in Blade Files: it’s not something that I advocate, but sometimes you just need to write some raw PHP in your Blade files.
Building Larger Applications with Layouts
Laravel – P15: Blade Layouts Template Inheritance
Creating Blade Layouts using Template Inheritance was widely used before components were brought in. They’re still used in conjunction with components.