Neeraj Prajapati
Neeraj Prajapati

Reputation: 541

how to write normal view to blade.php file in laravel?

I am new in Laravel I want to write some php lines like :

<?php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
?>

in blade.php file, so please help me, I was writing like :

{{  $uri_segment = ""}}
{{ $uri_segment1 = Request::segment(1) }}
{{ $uri_segment2 = Request::segment(2) }}
{{ $inventory_array = ['premium', 'surplus', 'purchase'] }}
{{ $material_array = ['brand', 'style', 'pricegroup', 'grade', 'size'] }}
{{ $product_array = ['adhesive', 'silicone', 'caulk', 'adhesivebrand'] }}
{{ $suppliers_array = ['dropshipper', 'price'] }}
{{ $system_array = ['webstores', 'utilities'] }}

but it's given error enter image description here

please guide me, thanks!

Upvotes: 2

Views: 533

Answers (3)

azjezz
azjezz

Reputation: 3967

In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
    // code here ..
@endphp

While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template. https://laravel.com/docs/5.5/blade#php

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

The code you've shown will work:

<?php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
?>

However, you shouldn't do this in a Blade view. You should move the logic in controller or service class.

{{ $inventory_array = ['premium', 'surplus', 'purchase'] }} doesn't work because it's the same as doing this:

echo $inventory_array = ['premium', 'surplus', 'purchase'];

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

Use @php directive in your view:

In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
    $uri_segment = "";
    $uri_segment1 = Request::segment(1);
    $uri_segment2 = Request::segment(2);

    $inventory_array = array('premium', 'surplus', 'purchase');
    $material_array = array('brand', 'style', 'pricegroup', 'grade', 'size');
    $product_array = array('adhesive', 'silicone', 'caulk', 'adhesivebrand');
    $suppliers_array = array('dropshipper', 'price');
@endphp

Upvotes: 1

Related Questions