Laravel 6 Eloquent ORM MVC Tutorial

Today, We want to share with you Laravel 6 Eloquent ORM MVC Tutorial.In this post we will show you laravel 6 eloquent multiple where, hear for Laravel 6 Eloquent ORM Tutorial with CRUD Example we will give you demo and example for implement.In this post, we will learn about Using Laravel’s Eloquent ORM Efficiently with an example.

Laravel 6 Eloquent ORM MVC Tutorial

There are the Following The simple About updateorcreate in laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel 6 model refresh, so the laravel 6 create model with migration is used for this example is following below.

Laravel 6 Eloquent LEFT JOIN WHERE NULL

$data = Product::leftJoin('categories', function($join) {
      $join->on('products.id', '=', 'categories.cat_id');
    })
    ->whereNull('categories.cat_id')
    ->first([
        'products.id',
        'products.slug',
        'products.permalink',
        'products.date_created',
        'products.status',
        'products.catalog_visibility',
        'products.purchasable',
        'products.attributes',
        'products.images',
        'products.tags',
        'products.categories',
        'products.purchase_note',
        'products.rating_count'
    ]);

Eloquent’s where() method

AND, OR, IN NULL, NOT NULL, GREATER THAN, EQUALS TO, NOT EQUALS

$products = Product::where("id", "!=", 1)->get();
// =, <, >, <=, >=, <>, !=, LIKE, NOT LIKE, BETWEEN, ILIKE

$products = Product::where(function ($query) {
    $query->where('title', 'LIKE', '%laravel%')
        ->orWhere('title', 'LIKE', '%eloquent%');
})->get();

$products = Product::whereRaw('views > ? and votes > ?', [500, 50])->get();

$products = Product::whereRaw(DB::raw("store_id IN (SELECT member_id FROM member_permissions GROUP BY member_permissions.member_id WHERE role = 1)"))->get();

$products = Product::whereExists(function($query){
    $query->select(DB::raw(1))
        ->from('member_credits')
        ->whereRaw('member_credits.member_id = products.store_id')
        ->groupBy('member_credits.member_id')
        ->havingRaw("COUNT(*) > 3");
})->get();
// Any of the following may be used instead of Detail::whereExists
// ->orWhereExists(), ->whereNotExists(), ->orWhereNotExists()

$products = Product::whereIn('store_id', [1,2,3])->get();
// Any of the following may be used instead of Detail::whereExists
// ->orWhereIn(),

$products = Product::whereNotIn('store_id', function($query){
    $query->select('member_id')
        ->from('banned_members')
        ->groupBy('banned_members.member_id');
})->get();

// Any of the following may be used instead of Product::whereExists
// ->whereNotIn(), ->orWhereNotIn

Eloquent where clause with date

->whereDay()
->whereMonth('merrage_date', '=', 25)
->whereYear('merrage_date', '>', 2020)
->whereDate('merrage_date', '>', '1998-04-25')

Eloquent where null or not null

->whereNull('is_active')
->orWhereNull('is_active')
->whereNotNull('is_active')
->orWhereNotNull('is_active')

Ordering hasMany relation data

return $this->hasMany('Products::class')->orderBy('published_date');

restore() soft deleted Eloquent Models

Product::withTrashed()->where("id", 1)->restore();

SQL JOIN’s with Eloquent

join('tags', 'tags.product_id','=', 'products.id')
    ->select('tags.id', 'product.id')->first();

$product = Product::where('id', $productId)
    ->leftJoin('product_views', 'product_views.product_id', '=', 'products.id')
    ->first();

$product = Product::where('id', $productId)
    ->join('category',function($join) use($memberId) {
        $join->on('category.product_id', '=', 'products.id')
            ->on('category.member_id', '=', $memberId, 'and', true);
    })->first();

Cache Data in Eloquent

$products = Cache::remember('products', $seconds, function () {
 return DB::table('products')->get();
});

//OR to remember forever

$products = Cache::rememberForever('products', function () {
 return DB::table('products')->get();
});
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel 6 eloquent tutorial pdf.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment