2017-04-13 124 views
1

我一直在學習laravel,現在我第一次使用雄辯。但即時通訊出現錯誤。它不能識別代表查詢的變量。我在網上搜索瞭解決方案,但無法解決問題。我使用laravel版本5.4。誰能幫忙?Laravel雄辯 - 未定義變量

錯誤: 「未定義變量:篇」(在teste7.blade)

的代碼:

Route::get('teste7', '[email protected]'); 
Route::get('/article/{article}', ['as' => 'article', 'uses' => '[email protected]']); 

控制器

use App\article; 

    function teste7(){ 
    $articles=article::pagination(4); 
    return view('teste7'); 
} 
function article($article){ 
    return view($article); 
} 

teste7

{{ $articles->total() }} articles 
<b>In this page ({{ $articles->count() }} articles)</b> 
<ul> 
@foreach ($articles as $article) 
<li> <a href="{{route('article', ['artigo' => $article->Name])}}">{{ 
$article->Name }}</a></li> 
@endforeach 
</ul> 
{{ $articles->links() }} 

回答

0

您必須使用第二個參數將數據傳遞到視圖中。您可以在documentation here中找到更多信息。

function teste7() { 
    $articles = article::pagination(4); 
    return view('teste7', ['articles' => $articles]); 
} 
0
class CentralController extends Controller 
{ 
    public function teste7() 
    { 
     $articles = article::pagination(4); 

     // returns view 'teste7.blade.php' and passes the $articles variable along 
     return view('teste7', compact('articles')); 
    } 
} 

在你article()功能,你就不能正確返回視圖。在teste7()函數中,您將返回一個視圖('teste7.blade.php'),但不會將任何變量傳遞給視圖。

將變量從控制器傳遞到視圖是非常重要的。否則,他們將而不是從視圖內可訪問。

+0

好的。現在它說「調用未定義的方法Illuminate \ Database \ Query \ Builder :: pagination()」。這是什麼意思?是分頁()只適用於「用戶」模式? – Adato