Can
Be Better

Select specific columns using eloquent orm

I wonder how I can use eloquent orm to only select specific columns.

Eg my query:

$user = User::where('u_id', '=', $userid)
            ->where('type', '=', 1)
            ->where('is_active', '=', 1)
        -> "only select firstname and lastname"
Best Answer(As Selected By kiwo123)
kiwo123

@JarekTkaczyk Thanks for explaining that to me, I thought it was a performance issue

I got it wrong before, thought I had to use pluck() since I cant see either get([]) / first([]) with an array looking at the Laravel docs on the website http://laravel.com/docs/4.2/eloquent

But, if I only select a few columns, can I still run updates on that model even though I havent selected the value I wanted to update

Eg:

$user = User::where('u_id', '=', $userid)
            ->where('type', '=', 1)
            ->where('is_active', '=', 1)
            ->first(['type', 'c_id']);

if( $user->type == 1 )
{
   $user->name = 'john';
   $user->lastname = 'doe';
   $user->save();
}
JarekTkaczyk
 JarekTkaczyk

2 years ago(398,410 XP)

@kiwo123

Just like in the docs:

$user = User::where('u_id', '=', $userid)
            ->where('type', '=', 1)
            ->where('is_active', '=', 1)
            ->select('firstname', 'lastname')
            ->get() / first()

// or
$user = User::where('u_id', '=', $userid)
            ->where('type', '=', 1)
            ->where('is_active', '=', 1)
            ->get(['firstname', 'lastname']) / first(['firstname', 'lastname'])

However If you want just some of the columns, then I wouldn’t use Model, but rather simple array.

So better tell us what the purpose is.

kiwo123
kiwo123

2 years ago(16,680 XP)

@JarekTkaczyk

Why shouldnt I use Models? Is it a performance issue?

Should I only use models when I need to do create/update/delete? And than stick to DB:: for simple selects? – eg getting a value to compare to another

JarekTkaczyk
 JarekTkaczyk

2 years ago(398,410 XP)

@kiwo123 What I mean is, that if you need only 2 fields: lastname and firstname, then maybe array will be good.

Personally I either use Model with all its features and fields, or simple array. Matter of preference, no performance or any other issues involved.

That’s why I ask why you need that.

kiwo123
kiwo123

2 years ago(16,680 XP)

@JarekTkaczyk Thanks for explaining that to me, I thought it was a performance issue

I got it wrong before, thought I had to use pluck() since I cant see either get([]) / first([]) with an array looking at the Laravel docs on the website http://laravel.com/docs/4.2/eloquent

But, if I only select a few columns, can I still run updates on that model even though I havent selected the value I wanted to update

Eg:

$user = User::where('u_id', '=', $userid)
            ->where('type', '=', 1)
            ->where('is_active', '=', 1)
            ->first(['type', 'c_id']);

if( $user->type == 1 )
{
   $user->name = 'john';
   $user->lastname = 'doe';
   $user->save();
}
kiwo123
kiwo123

2 years ago(16,680 XP)

@JarekTkaczyk Thanks, I couldnt see either get() or first() being used with an array in the docs : http://laravel.com/docs/4.2/eloquent

But if I only select a few values, can I still run updates on that model? Even though I havent selected the value I want to update.

Eg:

$user = User::where('u_id', '=', $userid)->first(['type', 'is_active']);

if( $user->type == 1)
{
    $user->username = 'john';
    $user->lastname = 'doe';
    $user->save();
}
JarekTkaczyk
 JarekTkaczyk

2 years ago(398,410 XP)

@kiwo123 Yes, you can, as long as you select the primary key. Otherwise you would end up with query update X set Y=Z where id is null

TKay
 TKay

10 months ago(1,080 XP)

Hi everyone, I have a eloquent model named Store which has relation ship with another model User. Is it possible to do something like this:

Store::whereUser(Auth::user())

or, Something like:

Store::whereUserId(Auth::user()->id)

which will give me collection of stores?

I found something like:

User::whereToken($token)
omitobisam

@TKay, If you already specified a relationship with user and store for example

//Model User
.......
public function stores()
{
    return $this->hasMany(Store::class);
}
.......

//Model Store
.......
public function user()
{
    return $this->belongs(User::class);
}
.......

The you can get a all the stores for a user for example in your controller with

$user_stores = Auth::user()->stores();

Auth::user() already is a model for User, and just get the relationship with Store.

赞(0) 打赏
不开启评论,如有问题疑问请发邮件。[email protected]最长的路 » Select specific columns using eloquent orm
分享到: 更多 (0)