Fetch Data
Routes

Route::get('fetch_data','Fetch_Data@get_students');

Controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Student;

class Fetch_Data extends Controller {
public function get_students(){
$students = DB::table('student')->get();
return view('fetch_data', ['students' => $students]);
}
}

View

<html>
<head>
<title>View Student Records</title>
</head>
<body>
<table border = 1>
<tr>
<td>ID</td>
<td>Name</td>
</tr>
@foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
</tr>
@endforeach
</table>
</body>
</html>

Different Type of Query

To Get A Single Row From A Table
$user = DB::table('users')->where('name', 'Shishir Developer')->first();

To Get All Record
$students = DB::select('select * from student');

To Get A Single Column From A Table
$name = DB::table('users')->where('name', 'Shishir Developer')->pluck('name');

To Get Record of students where marks is greater than 400
$students = DB::table('student')
->where('marks', '>', 400)
->get();

To Print Record Where name is descending order and count every 10 Record
$users = DB::table('student')
->orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 10)
->get();

Join Query Example
$data = DB::table('area')
->join('city', 'area.city_id', '=', 'city.id')
->select('area.*', 'city.name as city_name')
->get();

To Get 10 Record from 5th Row
$students = DB::table('student')->skip(5)->take(10)->get();


Previous
Next