CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New

Retrieve data from database using Laravel framework

In this example we will discuss about how to retrieve a record or data from MySQL database using laravel framework PHP.

For retrieve data from MySQL database using laravel framework first we have to create a table in data base.

After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in laravel framework please visit the link : Insert data in Laravel.

The SELECT statement is used to retrieve data from one or more tables:

The SQL query for retrieve specific column.

SELECT column_name(s) FROM table_name

or we can use the * character to retrieve ALL columns from a table:

SELECT * FROM table_name

To learn more about SQL, please visit our SQL tutorial.

Create 3 files for retrieve data in Laravel :

  • StudViewController.php (app/Http/Controllers/StudViewController.php)
  • stud_view.blade.php (resources/views/stud_view.blade.php)
  • web.php (routes/web.php)

In this example we retrieve the students data.


<!DOCTPE html>
<title>View Student Records</title>
<table border = "1">
<td>First Name</td>
<td>Last Name</td>
<td>City Name</td>
@foreach ($users as $user)
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td>{{ $user->city_name }}</td>
<td>{{ $user->email }}</td>


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudViewController extends Controller {
public function index(){
$users = DB::select('select * from student_details');
return view('stud_view',['users'=>$users]);


| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
//retrive data

After fetch data the table look like this.

Id first name last name City name Email Id
1 Divyasundar Sahu Mumbai
2 Hritika Sahu Pune
3 Milan Jena Chennai