Laravel convert array to single object when one related model is specified - php

I'm working inside a Laravel 9 project. I have a model called Affiliate which defines a hasMany relationship on my AffiliateProduct model. My AffiliateProduct defines a hasOne relationship for both a Product and Country.
An affiliate can have multiple products, but when the related product and country model are specified, I'd like to return a single entry instead of AffiliateProduct being an array, for example, right now I'd have to do:
$affiliate[0]->products->country
But I'd like to just be able to do:
$affiliate->products->country
Here's my query:
// get affiliate
$affiliate = Affiliate::where('aff_id', $request->input('AffId'))
->where('allow_submission', true)
->where('is_enabled', true)
->with([
'products' => function ($query) use ($productSlug, $countrySlug) {
$query->whereHas('product', function ($q) use ($productSlug) {
$q->where('slug', $productSlug)
->orWhere('is_default', true)
->where('is_enabled', true);
})->whereHas('country', function ($q) use ($countrySlug) {
$q->where('slug', $countrySlug)
->orWhere('is_default', true)
->where('is_enabled', true);
});
},
'products.product',
'products.country'
])
->first();
And this is what my current JSON output looks like:
{
"Result": "Accepted",
"Message": "Yay!",
"Model": {
"id": 2,
"user_id": 1,
"company_id": 1,
"aff_id": "test",
"description": "test",
"api_hash": "test",
"allow_submission": true,
"is_favourited": false,
"is_default": false,
"is_enabled": true,
"last_used_at": null,
"created_at": "2023-02-01T15:19:40.000000Z",
"updated_at": "2023-02-01T15:19:40.000000Z",
"deleted_at": null,
"is_deleting": false,
"products": [
{
"id": 7,
"user_id": 3,
"company_id": 1,
"affiliate_id": 2,
"country_id": 1,
"product_id": 1,
"serve_method_id": 3,
"application_form_id": null,
"pingtree_group_id": 1,
"callback_url": null,
"callback_condition": null,
"duplicate_response": null,
"created_at": "2023-02-01T15:39:01.000000Z",
"updated_at": "2023-02-01T15:39:01.000000Z",
"deleted_at": null,
"is_deleting": false,
"product": {
"id": 1,
"user_id": 1,
"company_id": 1,
"name": "Payday",
"slug": "payday",
"description": "Support brokering loan leads via an application.",
"is_default": true,
"is_enabled": true,
"created_at": "2023-02-01T15:19:39.000000Z",
"updated_at": "2023-02-01T15:19:39.000000Z",
"deleted_at": null,
"is_deleting": false
},
"country": {
"id": 1,
"user_id": 1,
"company_id": 1,
"country_code": "GB",
"calling_code": "44",
"name": "United Kingdom",
"slug": "united-kingdom",
"description": "United Kingdom of Great Britain and Northern Ireland",
"currency": "GBP",
"is_default": true,
"is_enabled": true,
"created_at": "2023-02-01T15:19:39.000000Z",
"updated_at": "2023-02-01T15:19:39.000000Z",
"deleted_at": null,
"is_deleting": false
}
}
]
}
}
Also, any tips on potentially improving my query would be appreciated.

Related

Laravel pluck fails to get value from load relationship

I need to return just an array of the name values for each of my roles. My roles is a hasMany relationship. This is currently how I'm trying to do it, but the returned result is unchanged, what am I missing?
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(Request $request, Company $company)
{
$this->authorize('view', $company);
if ($request->boolean('with_roles')) {
$company = $company->load(['roles' => function ($query) {
$query->pluck('name');
}]);
}
return new ApiSuccessResponse($company);
}
gives me:
{
"model": {
"id": 1,
"user_id": 1,
"contact_first_name": "John",
"created_at": "2023-02-15T09:45:48.000000Z",
"updated_at": "2023-02-15T09:45:48.000000Z",
"deleted_at": null,
"is_deleting": false,
"roles": [
{
"id": 6,
"company_id": 1,
"name": "accountant",
"guard_name": "api",
"created_at": "2023-02-15T09:45:59.000000Z",
"updated_at": "2023-02-15T09:45:59.000000Z"
},
{
"id": 2,
"company_id": 1,
"name": "admin",
"guard_name": "api",
"created_at": "2023-02-15T09:45:48.000000Z",
"updated_at": "2023-02-15T09:45:48.000000Z"
},
{
"id": 4,
"company_id": 1,
"name": "affiliate",
"guard_name": "api",
"created_at": "2023-02-15T09:45:56.000000Z",
"updated_at": "2023-02-15T09:45:56.000000Z"
},
{
"id": 3,
"company_id": 1,
"name": "affiliate_manager",
"guard_name": "api",
"created_at": "2023-02-15T09:45:55.000000Z",
"updated_at": "2023-02-15T09:45:55.000000Z"
},
{
"id": 5,
"company_id": 1,
"name": "buyer",
"guard_name": "api",
"created_at": "2023-02-15T09:45:58.000000Z",
"updated_at": "2023-02-15T09:45:58.000000Z"
},
{
"id": 8,
"company_id": 1,
"name": "guest",
"guard_name": "api",
"created_at": "2023-02-15T09:46:02.000000Z",
"updated_at": "2023-02-15T09:46:02.000000Z"
},
{
"id": 7,
"company_id": 1,
"name": "user",
"guard_name": "api",
"created_at": "2023-02-15T09:46:00.000000Z",
"updated_at": "2023-02-15T09:46:00.000000Z"
}
]
}
}
I expect to see:
{
"model": {
"id": 1,
"user_id": 1,
"contact_first_name": "John",
"created_at": "2023-02-15T09:45:48.000000Z",
"updated_at": "2023-02-15T09:45:48.000000Z",
"deleted_at": null,
"is_deleting": false,
"roles": [
"accountant",
"admin",
"affiliate"
...
]
}
}
pluck doesn't modify the original relationship, it only returns an array. Try this one :
public function show(Request $request, Company $company)
{
$this->authorize('view', $company);
$result = $company->toArray();
if ($request->boolean('with_roles')) {
$result['roles'] = $company->roles()->pluck('name')->toArray();
}
return new ApiSuccessResponse($result);
}

Laravel I can't retrieve requester data using with

I am using this package
https://github.com/renoki-co/befriended
My issue is trying to get all my friends posts with my post including user's data, but the problem is the requester data is null.
$user = $request->user();
$followersPost = UserPost::with(['user' => function ($query) use ($user) {
$query->filterFollowingsOf($user);
}])->latest()->get();
return $followersPost;
Response
[
{
"id": 35,
"post_body": "The Requster Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 1,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:58.000000Z",
"updated_at": "2020-12-06T18:08:58.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": null
},
{
"id": 34,
"post_body": "Test Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 2,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:14.000000Z",
"updated_at": "2020-12-06T18:08:14.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
},
{
"id": 24,
"post_body": null,
"location_id": null,
"location_title": null,
"location_icon": null,
"location_coordinate": null,
"location_vicinity": null,
"user_id": 3,
"viewer_ids": null,
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z",
"images": [
],
"musics": [
{
"id": 4,
"post_id": 24,
"song_name": "Confetti",
"song_preview": "https://cdns-preview-6.dzcdn.net/stream/c-6eb643a05083c023f874f13739fc7c87-3.mp3",
"song_picture": "https://api.deezer.com/album/183603832/image",
"song_artist": "Little Mix",
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z"
}
],
"movies": [
],
"emojs": [
{
"id": 16,
"post_id": 24,
"user_id": 3,
"emojy": "015-smile-1",
"created_at": "2020-12-06T01:59:15.000000Z",
"updated_at": "2020-12-06T01:59:15.000000Z",
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
},
{
"id": 17,
"post_id": 24,
"user_id": 1,
"emojy": "089-broken-heart",
"created_at": "2020-12-06T16:58:07.000000Z",
"updated_at": "2020-12-06T16:58:24.000000Z",
"user": {
"id": 1,
"_id": "737ba284-424d-4b0d-8a56-2e845603f2ba",
"name": "Mohammed",
"username": "mohammed",
"email": "h4ck3r.x0#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128001.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:26:09.000000Z",
"updated_at": "2020-12-05T00:26:41.000000Z"
}
},
{
"id": 19,
"post_id": 24,
"user_id": 2,
"emojy": "021-sad",
"created_at": "2020-12-06T16:58:36.000000Z",
"updated_at": "2020-12-06T16:58:36.000000Z",
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
}
],
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
}
]
As you can see the first object in the array dosen't include the user data.
So what should i do $query->filterFollowingsOf($user); to include my data?
I think the with callback is used for modifying the query that selects the user data. Instead, try using a whereHas method that filters the related users.
$user = $request->user();
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->latest()
->get();
return $followersPost;
I'd also recommend using paginate() instead of get() for performance when there could be a significant number of posts.
Edit:
To get either your friend's posts or your posts, try:
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->orWhere('user_id', $user->id)
->latest()
->get();

How to modify array structure returned by eloquent in laravel

I currently have the below array structure that I get when running Bonus::with('users')->get()
I have tried adding the array and merging similar keys but it is not working. I tried using the helper methods provided in laravel but I don't seem to be able to get it working.
[
{
"id": 1,
"users_id": 1,
"bonus_type_id": 1,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 2,
"users_id": 1,
"bonus_type_id": 2,
"is_paid": 0,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "test",
"email": "test#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
},
{
"id": 3,
"users_id": 2,
"bonus_type_id": 2,
"is_paid": 1,
"amount": "100.00",
"description": "desc",
"note": null,
"created_by": 2,
"last_modified_by": null,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"user": {
"id": 1,
"name": "another test",
"email": "another#testing.com",
"email_verified_at": null,
"status": 1,
"created_at": "2019-07-18 11:41:35",
"updated_at": "2019-07-18 11:41:35",
"deleted_at": null
}
}
]
I want the result to be something like that
[
{
user_id: 1,
name: "test",
bonuses: [
{
bonus_type_id: 1,
amount: 100,
is_paid: 0,
},
{
bonus_type_id: 2,
amount: 100,
is_paid: 0,
}
],
},
{
user_id: 2,
name: "another",
bonuses: [
{
bonus_type_id: 2,
amount: 100,
is_paid: 1,
},
]
}
]
You need to change your eloquent relationship. Define hasMany relationship function bonuses inside User model for bonuses table and use that to get the desired result like so:
User::with('bonuses')->get();
That way it will return results from users table and populate bonuses for each user.
makes sense?
You can get specific information with select subquery, if your relations are correctly set, this should work.
User::select('user_id','name','bonus_id')
->with(array('bonuses'=> function($query){
$query->select('bonus_type_id','amount','is_paid');
}))->get();

How can I hidden data in JSON if variable is empty

I got a some trouble in my Laravel application, in Search function, when I do search the result is
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "4232343432",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
{
"id": 3,
"user_id": 16,
"identity_number": "213918273",
"name": "Pekerja_3",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2345234234",
"image": null,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"partner_id": null,
"skill": []
}
]
}
as you can see that "Skill" with id number 3 is empty, I want all in id number 3 is empty also.
My controller is :
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])->whereHas('skill');
And I want something like this :
{
"data": [
{
"id": 2,
"user_id": 8,
"identity_number": "213918273",
"name": "Pekerja_2",
"gender_id": 1,
"date_of_birth": "1999-05-25",
"address": "Jalan Bandung Raya no 50",
"province_id": 32,
"city_id": 3273,
"district_id": 3273160,
"phone": "2534234234",
"image": null,
"created_at": "2018-01-11 10:59:54",
"updated_at": "2018-01-11 10:59:54",
"partner_id": null,
"skill": [
{
"id": 3,
"worker_id": 2,
"skill_id": 6,
"sub_skill_id": 18,
"created_at": "2018-01-15 13:06:48",
"updated_at": "2018-01-15 13:06:48",
"price": null,
"unit": null
}
]
},
]
}
The point is, if "skill" variable is empty then data not show and vice versa.
Thankyou any help will appreciate, sorry for bad english
You can add a callback to your whereHas function with the same filter as the with function
$worker = Worker::with(['skill' => function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
}])
->whereHas('skill', function($q) use ($request) {
$q->where('worker_skills.sub_skill_id', $request['sub_skill_id']);
})
->get();
You can try this $worker = Worker:has('skill')->get();
Only worker that have at least one skill are contained in the collection

Subscribed topics

In my forum I'm showing all topics. It looks like this:
So a user can subscribe by clicking on the red hearth.
I'm loading the topics with this json:
{
"forum": {
"id": 1,
"slug": "test",
"name": "test",
"description": "test",
"created_at": null,
"updated_at": null
},
"topics": {
"total": 6,
"per_page": 5,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://forum.dev/api/forum/test?page=2",
"prev_page_url": null,
"from": 1,
"to": 5,
"data": [
{
"id": 1,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 2,
"slug": "2",
"name": "1",
"description": "1\t",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 3,
"slug": "1",
"name": "1",
"description": "1\t",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 4,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
},
{
"id": 5,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null
}
]
}
}
I'm returning that ^ like this:
$forum->topics()->orderBy('created_at', 'DESC')->paginate(5);
So how do I get a subscribe value on every topic object?
So like this:
"data": [
{
"id": 1,
"slug": "1",
"name": "1",
"description": "1",
"forum_id": 1,
"created_at": null,
"updated_at": null,
"subscribed": true
},
I already made this on my topic model:
/**
* #return mixed
*/
public function subscriptions()
{
return Topic::subscribedBy(Auth::user())->get();
}
And it's working. But how do I send that ^ with every topic.
You can add an attribute (which is not present in the database) by creating an accessor.
public function getSubscriptionsAttribute()
{
return Topic::subscribedBy(Auth::user())->get();
}
and then adding it to the $append property.
protected $appends = ['subscriptions'];
If you're using the $visible whitelist you might have to add it to that property too.
Source (Its all the way in the bottom.)

Categories