[php] Passing data from controller to view in Laravel

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></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)

This question is related to php laravel laravel-blade

The answer is


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']}}

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 }}.


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

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]

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
    )
);

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

$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>

In Laravel 5.6:

$variable = model_name::find($id);
return view('view')->with ('variable',$variable);

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-blade

Passing data from controller to view in Laravel Laravel Blade html image Laravel Escaping All HTML in Blade Template How can I change the size of a Bootstrap checkbox? How to Get the Current URL Inside @if Statement (Blade) in Laravel 4? Truncate string in Laravel blade templates How to Set Variables in a Laravel Blade Template