Passing data from controller to view in Laravel
Asked Answered
T

10

27

I am new to Laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.

I have a controller - ProfileController and inside that a function:

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

In my view, I have this code:

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

I am receiving this error: Undefined variable: students (View:regprofile.blade.php)

Thumbstall answered 13/5, 2015 at 16:29 Comment(0)
B
28

Can you give this a try,

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

While, you can set multiple variables something like this,

$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);
Bonnette answered 13/5, 2015 at 16:40 Comment(1)
Oops I had a missing bracket after compact('students). ThanksThumbstall
H
20

For Passing a single variable to view.

Inside Your controller create a method like:

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

In Your route

Route::get('/sleep', 'TestController@sleep');

In Your View Welcome.blade.php. You can echo your variable like {{ $title }}

For An Array(multiple values) change,sleep method to :

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

You can access you variable like {{ $author }}.

Heptateuch answered 4/1, 2016 at 10:35 Comment(1)
Better elaborate answer.Newbill
W
11

The best and easy way to pass single or multiple variables to view from controller is to use compact() method.

For passing single variable to view,

return view("user/regprofile",compact('students'));

For passing multiple variable to view,

return view("user/regprofile",compact('students','teachers','others'));

And in view, you can easily loop through the variable,

@foreach($students as $student)
   {{$student}}
@endforeach
Willms answered 14/1, 2019 at 3:48 Comment(0)
Q
4

You can try this as well:

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

Also, use this variable in your view.blade file to get students name and other columns:

{{$students['name']}}
Quatrain answered 4/12, 2018 at 13:37 Comment(0)
T
1

Try with this code:

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

Or if you want to pass more variables into view:

return View::make('user/regprofile', array
    (
        'students'    =>  $students,
        'variable_1'  =>  $variable_1,
        'variable_2'  =>  $variable_2
    )
);
Toby answered 14/5, 2015 at 8:46 Comment(0)
L
1

In Laravel 5.6:

$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
Loading answered 2/8, 2018 at 13:57 Comment(0)
S
0
public function showstudents() {
     $students = DB::table('student')->get();
     return (View::make("user/regprofile", compact('student')));
}
Siva answered 11/7, 2019 at 12:29 Comment(1)
Please consider adding some comments explaining how your code answers the question in order to improve this answer.Genic
M
0

try with this code :

Controller:
-----------------------------
 $fromdate=date('Y-m-d',strtotime(Input::get('fromdate'))); 
        $todate=date('Y-m-d',strtotime(Input::get('todate'))); 

 $datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To 
        return view('inventoryreport/inventoryreportview', compact('datas'));

View Page : 
@foreach($datas as $student)
   {{$student}}

@endforeach
[Link here]
Moltke answered 7/9, 2019 at 7:4 Comment(0)
D
0
$books[] = [
            'title' => 'Mytitle',
            'author' => 'MyAuthor,
            
        ];

//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));

----------------------------------------------------


//to use this on myView.blade.php
<script>
    myVariable = {!! json_encode($books) !!};
    console.log(myVariable);
</script>
Dyewood answered 25/10, 2020 at 12:8 Comment(0)
S
0

In laravel 8 and above, You can do route binding this way.

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

In the view file, you can access it like below.

@foreach($students as $student)
        {{$student->name}}
@endforeach
Sensorium answered 15/6, 2021 at 8:54 Comment(1)
This is not route-model binding.Greyso

© 2022 - 2024 — McMap. All rights reserved.