Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Laravel select a specific column with Relation

Laravel select a specific column with Relation

Today, I will give you an example of “Select a specific column with Relation in laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and Laravel 8 application.

First, what we’re doing here, This is the example :

categories table relation with products table

select query in relation data and with function

Need of select columns in relationship in Laravel

We have two tables first one is categories and the second one is products also we added a categories table primary key as a foreign key in the products table name cat_id.

category table

product table

Select query in Relation in Laravel

We will use select query in relation because when we get products table data with categories table, This works but returns too much data from relationship tables. So we select a few columns which we need in the blade file.

We use the select query in the model but mostly we need a select function in controller, that is why we will use the select query in our controller.

Related article:- Laravel One To One Eloquent Relationships.

We will Get Specific Columns Using “With()” and Function in Laravel Eloquent.

app\Models\Category.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Cviebrock\EloquentSluggable\Sluggable;


class Category extends Model
{
   
    use SoftDeletes;
    use HasFactory;
    use Sluggable;  
    
    protected $fillable = ['title','slug'];
 
     public function sluggable()
     {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

app\Models\Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Cviebrock\EloquentSluggable\Sluggable;

class Product extends Model
{
    use SoftDeletes;
    use HasFactory;
    use Sluggable;  
    
    protected $fillable = ['title','slug'];
 
     public function sluggable()
     {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function CategoryData()
   {
        return $this->hasOne('App\Models\Category','id','cat_id');
        }
}

routes\web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ProductController;


/*
|--------------------------------------------------------------------------
| 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!
|
*/


Route::get('/', [ProductController::class, 'index']);

app\Http\Controllers\ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\Category;

class ProductController extends Controller
{


    public function index(){
       $products = Product::with('CategoryData:id,slug')->select('id','cat_id','product_name','brand_name','product_image','price','discount','price_after_discount','slug')
        ->limit(25)->get();
        dd($products);    
    }
}

Run Application :

127.0.0.1:8000/

Output :

select query in with conditions in relation in laravel

In this article, we learned “Laravel select a specific column with Relation Example”, I hope this article will help you with your Laravel application Project.

Read also : Dynamic Product Carousel Slider in Laravel.