I have tables, users, profiles, contacts. 1 user has 1 profile, 1 profile has many contacts, I want to get profile with filtered contact and selected column (I don't want get all column). I success with coding below
UserController.php
$profile = $user->with(['profile.emails', 'profile.phones'])->first();
User.php
public function profile()
{
return $this->hasOne(Profile::class);
}
Profile.php
public function emails()
{
return $this->hasMany(Contact::class)
->where('type', 'email');
}
public function phones()
{
return $this->hasMany(Contact::class)
->where('type', 'phone');
}
result data like this
{
"username": "user",
"email": "user#mail.com",
"profile": {
"first_name": "Alex",
"last_name": "Ariana",
"emails": [
{
"id": 1,
"profile_id": 1,
"type": "email",
"prefix": "Personal",
"value": "alex#mail.com",
"is_primary": 0,
},
{
"id": 2,
"profile_id": 1,
"type": "email",
"prefix": "Business",
"value": "business#mail.com",
"is_primary": 1,
}
],
"phones": [
{
"id": 3,
"profile_id": 1,
"type": "phone",
"prefix": "45",
"value": "564637345345",
"is_primary": 1,
}
]
}
}
but then I add select on the relation like this
public function emails()
{
return $this->hasMany(Contact::class)
->select([DB::raw('IFNULL(prefix, "Email") AS type'), 'value AS url', 'is_primary'])
->where('type', 'email');
}
public function phones()
{
return $this->hasMany(Contact::class)
->select(['prefix', 'value AS number', 'is_primary'])
->where('type', 'phone');
}
then I got empty contact data like this
{
"id": 1,
"username": "user",
"email": "user#mail.com",
"profile": {
"first_name": "Alex",
"last_name": "Ariana",
"emails": [ ],
"phones": [ ]
}
}
Why this is happen?, if I access from user via hasManyThrough it works, but when I use hasMany, it result empty.
Related
Relation method
User model:
public function children()
{
return $this->hasMany(self::class, 'referrer_id')->with('children');
}
Controller Method:
$user = User::with('children')->where('id',$this->request->UserId)->get();
Response
{
"status": true,
"status_code": "Success",
"message": true,
"payload": {
"profile": [
{
"id": 1,
"referrer_id": null,
"name": "beciwerugy",
"username": "lahaky",
"email": "figo#mailinator.com",
"children": [
{
"id": 3,
"referrer_id": 1,
"name": "asd",
"username": "asde",
"email": "asda#asd.com",
"children": [
{
"id": 4,
"referrer_id": 3,
"name": "asd",
"username": "asde12",
"email": "as12a#asd.com",
]
}
]
}
]
}
]
}
]
}
}
I want to count all nestedchild at once
Currently im getting all nested child in ther list
but i just want count
I have attempted to achieve to pull the user's info and the product details (Which specified the nickname and name) when the product name equal to "Oops" but I have no idea why the getProducts does not return any things.
User Model
public function getProducts(){
return $this->hasMany('App\Models\Product','users_id');
}
Product Model
public function users(){
return $this->belongsTo(User::class);
}
The code of pulling data:
$products = User::with(['getProducts' => function($query){
$query->select("users_id","name","nickname");
}])->get();
The current output:
"data": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"get_products": [
]
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"get_products": [
{
"nickname":"MCD",
"name":"Oops"
},
{
"nickname":"Mary Brown",
"name":"Oops"
},
{
"nickname":"Kentucy",
"name":"KFC"
},
{
"nickname":"Texas Chicken",
"name":"TXS"
}
]
}
]
The expected output
"data": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"get_products": [
]
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"get_products": [
{
"nickname":"MCD",
"name":"Oops"
},
{
"nickname":"Mary Brown",
"name":"Oops"
}
]
}
]
getProducts is not a good name for the relationship, lets simply call it products.
public function products()
{
return $this->hasMany('App\Models\Product','users_id');
}
Your code in the comment doesn't work because you specify the where clause in the main query, not the sub query.
return User::with(['products' => function ($query) {
$query->select("users_id","name","nickname");
}])
// This got to be in the sub query.
->where("products.name","Oops")
->get();
So let's update your code to this:
$productName = 'Oops';
return User::with(['products' => function ($query) use ($productName) {
$query->select("users_id","name","nickname", "price")
->where("name","LIKE", "%{$productName}%");
}])
->get();
I have seen your comment to this answer. Let's define a total custom attribute for the User model:
class User extends Model
{
protected $appends = ['total'];
public function getTotalAttribute()
{
// This is higher order message, if you haven't used it: https://laravel.com/docs/8.x/collections#higher-order-messages
return $this->products->sum->price;
}
}
Then the total attribute will be part of any user.
i'm new in laravel and php and now i wanna create a rest api
I have this query:
$questions = Question::with(
array('picture' => function($query){
$query>select('question_id','picture_name')>pluck('picture_name')>all();
},
'user'=>function($query){
$query->select('id','image','name');
}))->get()->toArray();
my query return this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
{
"question_id": 1,
"picture_name": "1527484678.jpg"
},
{
"question_id": 1,
"picture_name": "1527485120.jpg"
}
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
but i want this json:
{
"questions:": [
{
"id": 1,
"title": "23",
"caption": "last",
"address": "lsdbabfd",
"picture": [
"1527484678.jpg",
"1527485120.jpg"
],
"user": {
"id": 1,
"image": "defaultPicture",
"name": "alex"
}
}
]
}
how can i do that?
i already tried to do that with Laravel pluck method and Eloquent Mutators
you can add attribute method
class Question extends Model
{
...
protected $appends = ['pictures'];
public function getPicturesAttribute()
{
return $this->pictures->pluck('picture_id');
}
}
I am working on a system where I need many users can have many accounts and they can have multiple roles per account.
How can I set that up in eloquent.
I am currently having in User model:
public function accounts() {
return $this->belongsToMany('App\Models\Account');
}
public function roles() {
return $this->belongsToMany('App\Models\Account')->withPivot('role');
}
I have a pivot table user_account where the role is defined.
My problem is that when I go
$user = User::with('accounts')
->with('roles')
->where('id', $user['id'])
->first();
My output is:
{
"id": 1,
"name": "John Doe",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"accounts": [
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1
}
},
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1
}
}
],
"roles": [
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1,
"role": "SYSTEMADMINISTRATOR"
}
},
{
"id": 1,
"name": "Acme Inc",
"type": "BUSINESS",
"created_at": "2018-03-11 20:46:46",
"updated_at": "2018-03-11 20:46:46",
"pivot": {
"user_id": 1,
"account_id": 1,
"role": "USER"
}
}
]
}
What I want is for the roles to contain just the data from the pivot table with user_id, account_id and the role name. Any pointers? I would also like the accounts output to contain just one account.
Through extensive searching on the net I actually found a solution.
You need to make an AccountUser model and add the role to the account_user table.
In the User class you set up these relations:
public function roles() {
return $this->hasMany('\App\Models\AccountUser');
}
public function accounts() {
return $this->hasManyThrough('\App\Models\Account', '\App\Models\AccountUser', 'id', 'id');
}
And in the AccountUser class
public function user() {
return $this->belongsTo('App\Models\User');
}
public function account() {
return $this->belongsTo('App\Models\Account');
}
And in the Account class
public function accountUser() {
return $this->hasMany('\App\Models\AccountUser');
}
Now I can do
$user = User::with('roles')
->with('accounts')
->where('email', $data['email'])
->first();
And I get my expected output :)
Hope this helps someone...
I have the following tables:
users
- id
- username
kids
- id
- user_id
groups
- id
group_user
- id
- group_id
- user_id
sits
- id
- user_id
They of course have addl fields, but it's just extraneous info.
My User model contains:
public function kids()
{
return $this->hasMany('Kid');
}
public function sits()
{
return $this->hasMany('Sit');
}
public function groups()
{
return $this->belongsToMany('Group')->withPivot('status');
}
My UserController#show method looks like this:
public function show($user)
{
$user = User::find($user)->load('kids', 'groups', 'sits');
return $user;
}
So, when I hit /user/show/1 I get the JSON I expect.
{
"id": "1",
"username": "somename",
"email": "somename#example.com",
"kids": [
{
"id": "4",
"user_id": "1"
},
{
"id": "3",
"user_id": "1"
},
{
"id": "1",
"user_id": "1"
}
],
"groups": [
{
"id": "1",
"name": "Group Name",
"pivot": {
"user_id": "1",
"group_id": "1"
}
}
],
"sits": [
{
"id": "2",
"user_id": "1",
"start": "2014-01-16 00:00:31",
"end": "2014-01-16 00:00:31",
"sitter_id": "2"
}
]
}
But my question is what is the "Laravel Way" to add another object to this response that is filtered? Let's say I wanted to return sits but also upcomingSits where it was filtered by a certain "start" date or where "sitter_id"=0.
Where is the proper MVC approach for that sort of logic and how would I use Laravel to return it in this response?
You can do something like this:
$user = User::with('kids', 'groups', 'sits')->find($user_id)->toArray();
$sits = Sit::where(.....)->get()->toArray();
return Response::json(array(
'user' => $user,
'sits' => $sits
));