Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Created June 13, 2016 17:43
Show Gist options
  • Select an option

  • Save prodeveloper/726b3c0ea20e670f8e0d6ed6fd1d317d to your computer and use it in GitHub Desktop.

Select an option

Save prodeveloper/726b3c0ea20e670f8e0d6ed6fd1d317d to your computer and use it in GitHub Desktop.
How to use the student data in the frontend

##Step 1:

Ensure we have some records on your Student class that you can use. Ensure mysql is running.

Run mysql-ctl cli. This starts the mysql database which normally closes if your Cloud 9 account if offline for a while.

##Step 2:

Add the following line of code to your routes.php file.

Route::get('/students', function () {
    return 'all students';
});

The above line should print out to the browser all students when you hit the url /students.

We can now try experimenting to show all the existing data in our model. Modify the route to look as such

Route::get('/students', function () {
    return App\Student::all();
});

You should be able to see your records in plain json format.

##Step 3:

Go to your resources/views folder and create a new file students.blade.php this is where we will put in the html related to the students.

Change the code on the route to now return this file

Route::get('/students', function () {
    return view('students');
        //return App\Student::all();
});

Put in the basic html template for a table on the students.blade.php file so that now it looks like this.

<!DOCTYPE html>
<html>
    <body>
        <table >
            <tr>
                <th>Name</th>
                 <th>Email</th>
                 <th>Course</th>
            </tr>
        </table>
    </body>
</html>

Refresh your page to confirm you can now see the table.

##Step 4:

Now we need to load data to our table. We do that by first passing the students data to the view on the routes file.

Route::get('/students', function () {
    return view('students',['students'=>App\Student::all()]);
 });

Then we incorporate this data on the view file by using blade logic see https://laravel.com/docs/5.2/blade

<!DOCTYPE html>
<html>
    <body>
        <table >
            <tr>
                <th>Name</th>
                 <th>Email</th>
                 <th>Course</th>
            </tr>
            @foreach($students as $student)
            <tr>
                <td>{{$student->name}}</td>
                <td>{{$student->email}}</td>
                <td>{{$student->course}}</td>
            </tr>
            @endforeach
        </table>
    </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment