2015-05-13 6 views
10

अरे दोस्तों मैं लार्वेल के लिए नया हूं और मैं टेबल 'छात्र' के सभी रिकॉर्ड्स को एक चर में स्टोर करने की कोशिश कर रहा हूं और फिर उस चर को एक दृश्य में पास कर रहा हूं ताकि मैं उन्हें प्रदर्शित कर सकूं।लैरवेल में देखने के लिए नियंत्रक से डेटा पास करना

मैं एक नियंत्रक है - ProfileController और कहा कि अंदर एक समारोह:

public function showstudents() 
    { 
    $students = DB::table('student')->get(); 
    return View::make("user/regprofile")->with('students',$students); 
    } 

मेरी नजर में मैं इस कोड

<html> 
    <head></head> 
    <body> Hi {{Auth::user()->fullname}} 
    @foreach ($students as $student) 
    {{$student->name}} 

    @endforeach 


    @stop 

    </body> 
    </html> 

मैं इस त्रुटि मिल रही है: अपरिभाषित चर: छात्रों (देखें: regprofile.blade.php)

उत्तर

11

आप इस एक कोशिश दे सकते हैं,

return View::make("user/regprofile", compact('students')); OR 
return View::make("user/regprofile")->with(array('students'=>$students)); 

करते समय, आपको कई चर कुछ इस तरह सेट कर सकते हैं,

$instructors=""; 
$instituitions=""; 

$compactData=array('students', 'instructors', 'instituitions'); 
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions); 

return View::make("user/regprofile", compact($compactData)); 
return View::make("user/regprofile")->with($data); 
+0

नहीं, काम नहीं कर रहा है :( – VP1234

+0

क्या आपको एक ही त्रुटि मिल रही है? –

+0

हाँ, अपरिभाषित चर छात्र – VP1234

0

इस कोड के साथ प्रयास करें:

return View::make('user/regprofile', array 
    (
     'students' => $students, 
     'variable_1' => $variable_1, 
     'variable_2' => $variable_2 
    ) 
); 
:

return View::make('user/regprofile', array 
    (
     'students' => $students 
    ) 
); 

या आप दृश्य में अधिक चरों पारित करने के लिए चाहते हैं, तो

6

देखने के लिए एक एकल चर पारित करने के लिए। आपका मार्ग

Route::get('/sleep', '[email protected]'); 

में

function sleep() 
{ 
     return view('welcome')->with('title','My App'); 
} 

आपका देखें Welcome.blade.php में:

आपका नियंत्रक के अंदर की तरह एक विधि पैदा करते हैं।

function sleep() 
{ 
     $data = array(
      'title'=>'My App', 
      'Description'=>'This is New Application', 
      'author'=>'foo' 
      ); 
     return view('welcome')->with($data); 
} 

आप चर की तरह {{ $author }} उपयोग कर सकते हैं: आप की तरह {{ $title }}

एक सरणी के लिए अपने चर (एक से अधिक मान) परिवर्तन, करने के लिए नींद विधि गूंज कर सकते हैं।

-1

मुझे लगता है कि नियंत्रक से डेटा को देखने के लिए डेटा खराब है। क्योंकि यह पुन: प्रयोज्य नहीं है और नियंत्रक फटर बनाते हैं। दृश्य को 2 भागों में विभाजित किया जाना चाहिए: टेम्पलेट और सहायक (जो कहीं से भी डेटा प्राप्त कर सकता है)। अधिक जानकारी के लिए आप लार्वेल में संगीतकार को देख सकते हैं।

संबंधित मुद्दे