2016-10-19 145 views
1

我想檢索所有列值。以下是儀表板控制器代碼,其默認運行指數法Laravel不從數據庫檢索值

<?php 

namespace App\Http\Controllers; 

use App\questions; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 
use App\Http\Requests; 

class Dashboard extends Controller 
{ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 
    public function fetchQuestions() { 
     return questions::where('people_id','=',Auth::id())->pluck('question'); 
    } 
    public function index(){ 
     $listQuestions=$this->fetchQuestions(); 
     return view('forms.question',compact('listQuestions')); 
    } 
} 

查看文件

<ul> 
@foreach ($listQuestions as $question) 
    <li>{{ $question }}</li> 
@endforeach 
</ul> 

它不顯示任何錯誤或任何結果要麼

回答

0

添加 - >所有()採摘後

return questions::where('people_id','=',Auth::id())->pluck('question')->all(); 

雖然你可以在一種方法中做到這一點:

public function index(){ 
    $listQuestions=questions::where('people_id','=',Auth::id())->pluck('question')->all(); 
    return view('forms.question',['listQuestions'=>$listQuestions]); 
} 
+0

仍然沒有工作 –

+0

當你DD($ listQuestions) –

+0

我得到這個'數組你明白了什麼:1 [▼0 => 「確定上真」]'它的工作原理,但值未通過或顯示在頁面 –

1

你可以試試嗎?

public function fetchQuestions() { 
     return questions::where('people_id','=',\Auth::guard('yourguardname')->user()->id)->get()->pluck('people_id','question'); 
    } 
+0

仍然無法正常工作 –

+0

@Jaskaran:對不起,我寫了第一個名字,而不是我寫的糾正它\ Auth :: guard('yourguardname') - > user() - > id。 –

+0

@RonakDattani謝謝,它爲我工作 –