里面的值包括

Value inside include

我想在 include 中声明值,以便稍后我可以在文件本身中调用它

首先我这样做

@include('components.protected-email', ['key' => 'emails[0]'])

并在文件本身中

@php

$split = explode('@', $key);
$first = $split[0];
$second = $split[1];

@endphp
<a href="" data-first="{{ $first }}" data-second="{{ $second }}" class="js-combaine-email"></a>

但是我得到了错误

ErrorException Undefined offset: 1 (View: D:\wamp64\www\test\resources\views\components\protected-email.blade.php)

您正在使用 字符串 作为您的 $key 参数。您的字符串不包含 @ 字符,因此 $split = explode('@', $key); 将导致 $split[0] 包含完整的字符串,因为它无法分解。 $split[1] 不存在所以你得到

"ErrorException 未定义的偏移量:1..."

假设你$emails[0]定义了某处变化

@include('components.protected-email', ['key' => 'emails[0]'])

@include('components.protected-email', ['key' => $emails[0]])

您可能还想在分配之前检查 $split[1] 是否存在