Laravel - 使用 blade 模板在 php 标签中 Foreach 值

Laravel - Foreach value in php tags with blade template

我对此有疑问,让我解释一下。 在控制器中,我得到了用户。

$users = User::where('steamid','!=','')->orderBy('time','DESC')->get();

之后,在 Blade 模板中,我需要使用 PHP 函数从 steam id 中获取 steam 配置文件 link。

我有一个功能:

function SteamName($steamid){
    $xml = simplexml_load_file("http://steamcommunity.com/profiles/$steamid/?xml=1");//link to user xmla
    $username = $xml->steamID;
    return $username;
}

我有一个 foreach 每个用户:

@foreach($users as $user)
//so, i need to do like this;
<?php print SteamName($user->steamid) ?>
@endforeach

将你的函数放在用户模型中

function SteamName($steamid){
    $xml = simplexml_load_file("http://steamcommunity.com/profiles/$steamid/?xml=1");//link to user xmla
    $username = $xml->steamID;
    return $username;
}

从您的 blade 调用函数 :

@foreach($users as $user)
{{ $user->SteamName($user->steamid) }}
@endforeach

你也可以试试这个

@foreach ($users as $user)
{{$user->steamid}}
@endforeach