简体   繁体   中英

Laravel how use value instead of full object with join in eloquent model

When I call eloquent:

$user = User::where('idUser', 1)->with(['privilege'])->first()->toArray();

It gives me:

{
    "idUser": 1,
    "name": "UserName",
    "email": "UserName@gmail.com",
    "image": "https://image.com",
    "createdAt": "2019-05-07 15:43:47",
    "privilege": {
        "idPrivilege": 1,
        "name": "user"
    }
}

When I call Eloquent:

$user = User::where('idUser', 1)->with(['privilege:name'])->first()->toArray();

Element privilege in json is set to null , but when I call:

$user = User::where('idUser', 1)->with(['privilege:idPrivilege,name'])->first()->toArray();

It is as same as first call. How can I set element privilege to fe user (I just want a simple value instead of the full object of Privilege )?

I can use something like:

$user['privilege'] = $user['privilege']['name']; 

But this one does not look so nice!

Using resource:

public function toArray($request)
    {
        return [
            'idUser' => $this->idUser,
            'name' => $this->name,
            'email' => $this->email,
            'privilege' => $this->privilege['name'],
            'createdAt' => $this->created_at,
        ];
    }

In controller:

$user = User::where('idUser', 1)->with('privilege')->first();
return UserResource::make($user);

Gives:

{
"data": {
"idUser": 1,
"name": "UserName",
"email": "UserName@gmail.com",
"privilege": "user",
"createdAt": "2019-05-07 15:43:47"
}
}

How can i just return object instead of data{object} ?

Try without the backets:

$user = User::where('idUser', $id)->with('privilege:name')->first()->toArray();

or this:

$user = User
        ::where('idUser', $id)
        ->with(['privilege' => function($query) {
            return $query->select('name');
        }])
        ->first()
        ->toArray();

But then, you could customize the response to return to your view using API Resources . With this, you can have many different resources to use on the same elements and format the response to any of your needs.

As stated in HCK 's answer, you could use

$user = User
        ::where('idUser', $id)
        ->with(['privilege' => function($query) {
            return $query->select('name');
        }])
        ->first()
        ->toArray();

To get what you need. Now, if you are already using API Resources , and want to remove the outter data object, you can add the following in your AppServiceProvider boot method:

use Illuminate\Http\Resources\Json\Resource;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Resource::withoutWrapping(); // With this, your resources won't have the
                                     // outter data wrapping

    }
}

Just take a look at the docs !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM