How to use addSelect() Eloquent Method in Laravel

How to use addSelect() Eloquent Method in Laravel

How to use addSelect() Eloquent Method in Laravel, in this article we will step by step guide to the use of addSelect() method. It is useful when we want to select a column of table with multiple where using.

It is a query builder.

How to use addSelect() Eloquent Method in Laravel

<?php  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
use DB;
  
class PostController extends Controller
{
    public function index()
    {
        $cate = Category::select("id", "title")
                        ->addSelect("slug")
                        ->addSelect(DB::raw('1 as number'))
                        ->take(10)
                        ->get();
 
        return cate;
    }
}

Comments

No Comment posted Yet!

Leave a Reply

OK! You can skip this field.