2016-08-16 15 views
0

私はそれに問題があるので、説明しましょう。 コントローラでは、私はユーザーを取得します。Laravel - ブレードテンプレートを使用したPHPタグのForeach値

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

その後、Bladeテンプレートで、私は蒸気IDから蒸気プロファイルリンクを取得するためにPHP関数を使用する必要があります。

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

を試すことができますか?あなたが得たエラーと最終結果を期待するものを記述してください。 – TheFallen

+0

エラーは表示されませんが、この{{$ user-> steamid}}をPHPタグの関数で使用する必要があります。だから、私はこのように使用することはできません –

答えて

0

関数を呼び出すあなたのブレードからユーザーモデル

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

の内側にあなたの関数を入れて:私はすべてのユーザーのためにforeachを持って

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

は私が機能を持っています:

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

ありがとう!それを私が直した。あなたを愛してます。 –

+0

補遺:あなたのユースケースから判断すると、Laravelのモデルアクセサーを調べることもできます。 (https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators)...ロジックを定義するメソッドgetSteamUsernameAttribute()を作成することができます。出力は '$ user-> steam_profile'として利用可能になります。 –

0

また、正確な問題である何この

@foreach ($users as $user) 
{{$user->steamid}} 
@endforeach 
関連する問題