2016-12-16 67 views
3

如果我在控制器,這條線會做到這一點。如何訪問視圖中的Laravel類?

use App\UBBUserSetting, App\UBBSetting;


但我在視圖中

我想包括在我看來,我的課(.blade.php

我不能

use App\UBBUserSetting, App\UBBSetting; 


    $ubb_user_settings = UBBUserSetting::where('cpe_mac','=',$cpe_mac)->get(); 
    $ubb_settings = UBBSetting::where('cpe_mac','=',$cpe_mac)->first(); 
    if($ubb_settings != null){ 
     $jump_bandwidth = $ubb_settings->jump_bandwidth; 
     $paid = $ubb_settings->price_per_jump; 
    } 

    $option2 = 0; 

    if($ubb_user_settings != null){ 
     $option1_selected = []; 
     $option2_selected = []; 

     $lines = count($ubb_user_settings); 

     foreach ($ubb_user_settings as $i => $ubb_user_setting) { 
      if($ubb_user_setting->option == 1){ 
       $option1_selected[$i]['d'] = $ubb_user_setting->created_at->format('d'); 
       $option1_selected[$i]['threshold'] = (int)$ubb_user_setting->volume_current_limit; 

      }else{ 

       $option2++; 
       $option2_selected[$i]['d'] = $ubb_user_setting->created_at->format('d'); 
       $option2_selected[$i]['threshold'] = (int)$ubb_user_setting->volume_current_limit; 
       $option2_selected[$i]['next_threshold'] = $threshold + ($jump_bandwidth * $option2); 

      } 
     } 

    } 

錯誤

enter image description here


一個如何去和這樣做?

任何提示?

+0

這不是利益與Laravel是如何打算的工作有衝突?你不應該將數據傳遞給視圖,而不是在視圖中運行代碼? –

回答

4

如果你想使用刀片意見任何PHP代碼,使用<?php ?>標籤,你可以使用完整的命名空間:

<?php 
    .... 
    $ubb_user_settings = \App\UBBUserSetting::where('cpe_mac','=',$cpe_mac)->get(); 
    .... 
?> 

但它是一個可怕的做法,從視圖運行查詢。你應該真的學習MVC模式。

+0

是的,他們在'<?php' – ihue

3

這應該是你的代碼:

$ubb_user_settings = \App\UBBUserSetting::where('cpe_mac','=',$cpe_mac)->get(); 
$ubb_settings = \App\UBBSetting::where('cpe_mac','=',$cpe_mac)->first(); 
if($ubb_settings != null){ 
    $jump_bandwidth = $ubb_settings->jump_bandwidth; 
    $paid = $ubb_settings->price_per_jump; 
} 

$option2 = 0; 

if($ubb_user_settings != null){ 
    $option1_selected = []; 
    $option2_selected = []; 

    $lines = count($ubb_user_settings); 

    foreach ($ubb_user_settings as $i => $ubb_user_setting) { 
     if($ubb_user_setting->option == 1){ 
      $option1_selected[$i]['d'] = $ubb_user_setting->created_at->format('d'); 
      $option1_selected[$i]['threshold'] = (int)$ubb_user_setting->volume_current_limit; 

     }else{ 

      $option2++; 
      $option2_selected[$i]['d'] = $ubb_user_setting->created_at->format('d'); 
      $option2_selected[$i]['threshold'] = (int)$ubb_user_setting->volume_current_limit; 
      $option2_selected[$i]['next_threshold'] = $threshold + ($jump_bandwidth * $option2); 

     } 
    } 

} 

您可以設置命名空間中的類之前,所以這將是App\UBBUserSetting::where()之前。由於您處於刀片視圖中,而不是類中,因此應該在類之前使用名稱空間。

希望這個作品!

+0

謝謝是的,它的工作原理。你們太棒了 !!!! – ihue

+0

@ihue謝謝!如果這解決了您的問題,您可以接受此答案,以便其他開發人員可以看到此作品! –

3

做獨立:

use App\UBBUserSetting; 
use App\UBBSetting; 
+0

啊,讓我試試看! – ihue

+0

是!讓我知道如果返回另一個錯誤 –