Database से data fetch करने के लिए Laravel हमें बहुत easy और convenient methods provide करता है , जिनकी help से हम easily Database से interact हो पाते हैं।


Laravel Database से interact करने के लिए PDO use करता है जिससे Application को SQL injection attacks से भी बचाया जा सके। इसलिए parameters bind करते समय हमें string को clean करने की जरूरत नहीं पड़ती।


Laravel Retrieve Results

इस Topic में DB Model के through data fetch करना सीखेंगे , हालाँकि अगर आप Table Model से fetch करना चाहे तो app directory के अंदर model define करके भी data fetch कर सकते हैं।

Laravel Retrieve All Rows From A Table

Laravel हमें database से data fetch करने के लिए chaining allow करती है।

File : app/Http/Controllers/UserController.php

CopyFullscreenClose Fullscreen
<?php 
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::table('users')->get();

        return view('user.index', compact('users'));
    }
}

get() method सभी records को contain किये हुए Illuminate\Support\Collection return करता है। जिस पर foreach loop apply करके data iterate कर सकते हैं।

foreach ($users as $user) {
    echo $user->name;
}

Laravel Retrieve A Single Row

$user = DB::table('users')->where('name', 'John')->first();

echo $user->name;

आप record की primary id के according find() method के through भी single record get कर सकते हैं।

$user = DB::table('users')->find(3);

Laravel Retrieve A List Of Column Values

अगर आपको सिर्फ single column का collection retrieve करना हो तो pluck() method के through आप get कर सकते हैं।

$titles = DB::table('roles')->pluck('title');

foreach ($titles as $title) {
    echo $title;
}

Laravel Aggregates

Laravel query builder आपको aggregate functions जैसे count, max, min, avg, and sum भी provide करता है।

/*Total records*/
$users = DB::table('users')->count();

/*Sum*/
$price = DB::table('orders')->sum('price');

/*Maximum Price*/
$price = DB::table('orders')->max('price');

/*Average*/
$price = DB::table('orders')->where('finalized', 1)->avg('price');

Laravel Selects

specified column names को select करने के लिए select() method use कर सकते हैं।

$users = DB::table('users')->select('name', 'email as user_email')->get();

Laravel Joins

Laravel Inner Join

दो tables को inner join करने के लिए join() method का use किया जाता है।

$users = DB::table('users')
            ->join('contacts', 'users.id', 'contacts.user_id')
            ->join('orders', 'users.id', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

Laravel Left Join

दो tables को left join करने के लिए leftJoin() method का use किया जाता है।

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', 'posts.user_id')
            ->get();

Laravel Where Clauses

where clause query perform करने के लिए Laravel कई सारे methods provide कराता है।

/*equals to*/
$users = DB::table('users')->where('votes', '=', 100)->get();

/*greater than equals to*/
$users = DB::table('users')->where('votes', '>=', 100)->get();

/*less than*/
$users = DB::table('users')->where('votes', '<', 100)->get();

Laravel orWhere

$users = DB::table('users')->where('votes', '=', 100)->orWhere('votes', '=', 200)->get();

Laravel whereIn / whereNotIn / orWhereIn / orWhereNotIn

जब किसी Array में से values को match करना हो वहां पर whereIn() use करते हैं।

$users = DB::table('users')->whereIn('id', [1, 2, 3])->get();

/*whereNotIn*/
$users = DB::table('users')->whereNotIn('id', [1, 2, 3])->get();

Laravel whereBetween

$users = DB::table('users')->whereBetween('votes', [1, 100])->get();

orWhereBetween
$users = DB::table('users')->whereBetween('votes', [1, 50])->orWhereBetween('votes', [51, 100])->get();

Laravel whereDate / whereMonth / whereDay / whereYear / whereTime

date / month / day / year / time से compare करने के लिए whereDate() / whereMonth() / whereDay() / whereYear() / whereTime() से करते हैं।

/*whereDate()*/
$users = DB::table('users')->whereDate('created_at', '2016-12-31')->get();

/*whereMonth()*/
$users = DB::table('users')->whereMonth('created_at', '12')->get();

/*whereDay()*/
$users = DB::table('users')->whereDay('created_at', '31')->get();

/*whereYear()*/
$users = DB::table('users')->whereYear('created_at', '2016')->get();

/*whereTime()*/
$users = DB::table('users')->whereTime('created_at', '=', '11:20:45')->get();

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers