Frenchmassacre
Frenchmassacre

Reputation: 462

Variable in PHP but not in Blade?

I've got something weird that I don't understand..

I've dumbed it down to try and figure out whats wrong but.. well I don't get it..

<?php 
    $x = "Type1";
    var_dump($x); 
?>

    {{$x}}

I've declared a variable in PHP and dumped it to check it's been assigned (I get the right value). But when I try to give this variable out using blade.. I get nothing, it tells me the variable is undefined..

Am I being dumb or missing something?

Help would be appreciated..

EDIT:

So after talking to Abdulkareem Mohammed I tried to dumb it down further and was left with exactly what I have above, I've noticed that the issue is therefore somehow due to the fact that this code is included on another page.

@include('Customer.cust_sections') 

Replacing the include tag with the above code works, even though they should be equivalent (at least to my understanding)

EDIT EDIT:

Turns out... I was just an idiot who forgot the ".blade" in the filename.. there goes 2h of my life on 6 letters..

Upvotes: 4

Views: 1482

Answers (4)

Jigneshsinh Rathod
Jigneshsinh Rathod

Reputation: 1028

first of all check your blade file extension.

it must have yourfilename.blade.php.

do you miss .blade?

Upvotes: 2

Abdulkareem Mohammed
Abdulkareem Mohammed

Reputation: 139

It should run. I tried copying your code to test it. Please show the wide document to see if there is any thing else which is make an issue.

Upvotes: 0

aryos
aryos

Reputation: 82

Another trick is using @php in the blade template. So your code would be:

@php 
  $x = "Type1";
  //var_dump($x); 
@endphp

{{ $x }}

I've tried it in one blade template, it passes the variable, but it fails on another. I'm rather confused, too.

Upvotes: 0

Spholt
Spholt

Reputation: 4032

In order to assign a variable to the blade template your either need to define it within the blade template like so:

{{ $x = "test" }}
{{ $x }}

Or you need to define it in your controller and then pass it to the view

$x = "test";

return $response->view('my-blade-template', [
    'x' => $x,
]);

I would recommend keeping as much of your logic and assignment of variables in a controller as possible!

Upvotes: 0

Related Questions