简体   繁体   中英

Laravel - add part of session content into array

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)

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.

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

and

@php
@endphp

it translate to <?php ?>


so when ur 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM