-2

I am looking to move a part of a session into a variable. The blade echo works fine outside the php tags. Inside the pgh tags I get an error about '{'.

Question: How can I move the mentioned session content into a PHP variable? If possible I would like to stay with using blade echo.

My code:

{{ session('payment.request_payment_id') }} // This works.

@php
  $test = {{ session('payment.request_payment_id') }} // This shows error
@endphp

Error:

syntax error, unexpected '{' (View: Xxx.blade.php)

Toolbox
  • 2,333
  • 12
  • 26
  • I know the question is very basic, but please enlight me why you downvote. Even after seing the answer it is not obvious due to that normally in PHP the session needs to be started and the "dot notation" is part of Laravel syntax. – Toolbox Aug 25 '20 at 05:14
  • 1
    your question has nothing to do with the session, it has to do with an error you get from bad Blade syntax ... no one is going to explain to you the entire framework especially when your question has nothing to do with anything else but not knowing how to run a PHP statement in your Blade file, so stop – lagbox Aug 25 '20 at 05:16

2 Answers2

4

Its all PHP between these directives:

@php
    $test = session('payment.request_payment_id');
@endphp

Update:

If you really think you need to also be echoing this as your question is asking:

{{ $test = session('payment.request_payment_id') }}

Everything inside {{ ... }} is PHP.

lagbox
  • 48,571
  • 8
  • 72
  • 83
1

here {{ }} it means in blade <?php echo ?>

and

@php
@endphp

it translate to <?php ?>


so when u r trying to

@php
  $test = {{ session('payment.request_payment_id') }} // This shows error
@endphp

it convert in to

<?php
$test = <?php echo session('payment.request_payment_id') ?>
?>

which is wrong and above answer given by lagbox is correct use that

Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33