Optimizing Laravel Query to get required data - laravel

I have the following models in my Laravel project
Merchant
Deals
Deal Votes
Deal Clicks
Deal Deal Types
Merchant Rating
This is how my Merchants Model looks like
class Merchants extends Model
{
//
public function deals()
{
return $this->hasMany('App\Deals', 'merchant_id', 'merchant_id');
}
}
Deals Model
class Deals extends Model
{
//
public function clicks()
{
return $this->hasMany('App\Clicks', 'deal_id', 'deal_id');
}
public function merchants()
{
return $this->hasOne('App\Merchants', 'deal_id', 'deal_id');
}
public function votes()
{
return $this->hasMany('App\Deal_votes', 'deal_id', 'deal_id');
}
public function deal_dealtypes()
{
return $this->hasMany('App\Deal_dealtypes', 'deal_id', 'deal_id');
}
}
Merchant Rating Model
public function merchants()
{
return $this->belongsTo('App\Merchants', 'merchant_id', 'merchant_id');
}
For a specific Merchant Landing page, I am trying to get some Merchant info, deals tied to the merchant, count of votes and clicks for each Deal. This is what I have done so far
$merchant = Merchants::where('merchant_url_text', $merchant_url_text)
-> with('categories' ,'deals','deals.votes','deals.clicks', 'deals.deal_dealtypes')
-> first();
This however is returning Merchant Info, Deal Info, All the Votes records tied to the Deal and all the clicks tied to each deal.
Attached sample data response I am getting
{
merchant_id: 6605,
master_merchant_id: null,
skimlinks_id: null,
merchant_name: "Groupon",
network: "cj",
network_id: "",
network_notes: null,
dual_network: null,
related_merchants: null,
affiliate_link: "http://api.coupilia.com/merchants/?aid=1423&mid=6605",
skimlinks_url: null,
fmtcurl: null,
merchant_homepage_url: "www.groupon.com?z=dealpage",
status: null,
selected_status: null,
relationship_status: null,
primary_country: "US",
apofpo: null,
primary_category: null,
subcategories: null,
special_payment_options: null,
mobile_certified: null,
logos: "",
custom_logo: "https://res-5.cloudinary.com/dhhntbusx/image/upload/t_media_lib_thumb/v1475206550/Groupon-coupons_mzjdm8.jpg",
custom_description: null,
added: null,
lastupdated: null,
merchant_url_text: "groupon.com",
about: "",
primary_category_id: "43",
parent_merchant_id: null,
skimlinks_ids: null,
custom_merchant_name: null,
ship_to_countries: "",
created_at: "2016-10-07 08:04:12",
updated_at: "2016-10-07 08:04:12",
categories: {
id: 43,
slug: "social",
category_name: "Social",
category_url_name: "social",
category_parent_id: null,
category_grandparent_id: null,
created_at: "2016-10-07 08:04:03",
updated_at: "2016-10-07 08:04:03"
},
deals: [
{
deal_id: 875438,
merchant_name: "Groupon",
merchant_id: 6605,
status: null,
deal_text: "Save Up To 90% Off Local Restaurants, Shops, Events And More",
restrictions: "",
coupon_code: "",
deal_start_date: "1/31/2016",
deal_end_date: "11/6/2016",
discount_type: null,
deal_type: "deal",
affiliate_url: "",
final_sale_price: null,
was_price: null,
discount: null,
percent_off: null,
dollars_off: null,
network: null,
image: null,
skimlinks_url: null,
fmtcurl: null,
created: null,
lastupdated: null,
threshold: null,
rank: "3",
starred: null,
master_merchant_id: null,
brands: null,
local: null,
link_id: null,
direct_url: null,
pixel_html: null,
created_at: "2016-10-07 08:05:26",
updated_at: "2016-10-07 08:05:26",
votes: [
{
id: 2,
deal_id: 875438,
vote: 1,
user_id: 0,
uuid: "ad63d5a0-2582-11e6-9a1c-49da886e8d6c",
created_at: "2016-10-12 05:23:57",
updated_at: "2016-10-12 05:23:57"
},
{
id: 5,
deal_id: 875438,
vote: 1,
user_id: 0,
uuid: "bde41ea0-903c-11e6-b93b-51bc5393a427",
created_at: "2016-10-12 05:28:50",
updated_at: "2016-10-12 05:28:50"
}
],
clicks: [
{
id: 1,
tracking_id: null,
merchant_id: null,
deal_id: "875438",
user_id: null,
user_tracking_id: null,
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com",
referring_domain: null,
page_type: null,
page_url: null,
utm_source: null,
utm_medium: null,
utm_term: null,
utm_campaign_id: null,
os: "OS X",
ip_address: "10.0.2.2",
browser: "Chrome",
initial_referring_domain: null,
referring_keyword: null,
deal_position: null,
click_type: null,
created_at: "2016-10-12 02:19:59",
updated_at: "2016-10-12 02:19:59"
},
{
id: 12,
tracking_id: null,
merchant_id: null,
deal_id: "875438",
user_id: null,
user_tracking_id: null,
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com",
referring_domain: null,
page_type: null,
page_url: null,
utm_source: null,
utm_medium: null,
utm_term: null,
utm_campaign_id: null,
os: "OS X",
ip_address: "10.0.2.2",
browser: "Chrome",
initial_referring_domain: null,
referring_keyword: null,
deal_position: null,
click_type: null,
created_at: "2016-10-21 03:41:11",
updated_at: "2016-10-21 03:41:11"
},
{
id: 15,
tracking_id: null,
merchant_id: null,
deal_id: "875438",
user_id: null,
user_tracking_id: null,
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com",
referring_domain: null,
page_type: null,
page_url: null,
utm_source: null,
utm_medium: null,
utm_term: null,
utm_campaign_id: null,
os: "OS X",
ip_address: "10.0.2.2",
browser: "Chrome",
initial_referring_domain: null,
referring_keyword: null,
deal_position: null,
click_type: null,
created_at: "2016-10-21 03:55:33",
updated_at: "2016-10-21 03:55:33"
},
{
id: 16,
tracking_id: null,
merchant_id: null,
deal_id: "875438",
user_id: null,
user_tracking_id: null,
referring_url: "http://trystin.app:8000/deals-coupons/groupon.com",
referring_domain: null,
page_type: null,
page_url: null,
utm_source: null,
utm_medium: null,
utm_term: null,
utm_campaign_id: null,
os: "OS X",
ip_address: "10.0.2.2",
browser: "Chrome",
initial_referring_domain: null,
referring_keyword: null,
deal_position: null,
click_type: null,
created_at: "2016-10-21 04:06:08",
updated_at: "2016-10-21 04:06:08"
}
],
deal_dealtypes: [
{
id: 1888,
deal_id: "875438",
dealtype_id: "3",
created_at: "2016-10-07 08:05:26",
updated_at: "2016-10-07 08:05:26"
}
]
},
}
I need some help with the following
Get limited fields and not all fields tied to Merchant
Get limited fields from deals object
Get Count of Votes and Clicks for each deal in the response

To get limited fields you can use select() function and to get counts you can use withCount() function.
So your query will be as:
$merchant = Merchants::where('merchant_url_text', $merchant_url_text)
->select('id', 'name')
->with(['deals' => function($q) {
$q->select('id', 'name')
->with('votes','clicks', 'deal_dealtypes')
->withCount('votes')
->withCount('clicks');
}])
->with('categories')
-> first();

Related

How to return consistent data types in laravel pagination

I have a function that returns staff and there associated attributes as below
foreach ($merchant_user_ac->staffs->sortByDesc('id') as $staff) {
$wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);
$payments = StaffPayment::where('merchant_id', AH::cMiD())
->where('user_id', $staff->id)
->when(!empty($start_date) && !empty($end_date), function ($q) use (
$start_date,
$end_date
) {
$q->whereBetween(\DB::raw('date(payment_date)'), [
$start_date,
$end_date,
]);
})
->sum('amount');
$balance_owed = $wage - $payments;
$transactions->add([
'id' => $staff->id,
'name' => $staff->name,
'profilephoto' => $staff->profilephoto,
'wage' => $wage,
'payments' => $payments,
'salary' => $staff->salary,
'rent' => $staff->rent,
'balance_owed' => $balance_owed + $staff->salary - $staff->rent,
]);
}
$merchant_staffs = collect(json_decode(json_encode($transactions), false));
$merchant_staffs = $merchant_staffs->paginate(10);
return response()->json($merchant_staffs);
In the results, the first page is OK but the subsequent pages are having a different data type from the first page and displaying the data becomes an issue.
The data key has different data types.
I have tried paginating before the foreach loop but the response did not have the pagination links.
I have tried adding ->toArray() method when collecting the data but has the same issue of different types.
How can I return the same data in all pagination links?
The data returned is as below
{
"current_page": 1,
"data": [
{
"id": 532,
"name": "George2",
"profilephoto": "photos/GwSIKoIXUk1GdL7boD7Ht9mSp1loxM1nGcZ5l5Gd.jpg",
"wage": 0,
"payments": 10000,
"salary": "90000.00",
"rent": "1000.00",
"balance_owed": 79000
},
{
"id": 528,
"name": "david",
"profilephoto": null,
"wage": 100,
"payments": 0,
"salary": "67000.00",
"rent": "67000.00",
"balance_owed": 100
},
{
"id": 524,
"name": "Naggie",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 503,
"name": "Khaki ",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 502,
"name": "Susan",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 476,
"name": "Maggie",
"profilephoto": null,
"wage": 0,
"payments": 17000,
"salary": null,
"rent": "15000.00",
"balance_owed": -32000
},
{
"id": 475,
"name": "Aggy",
"profilephoto": null,
"wage": 0,
"payments": 15000,
"salary": "15000.00",
"rent": null,
"balance_owed": 0
},
{
"id": 465,
"name": "Rhoda",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": null,
"rent": null,
"balance_owed": 0
},
{
"id": 464,
"name": "Very New Staff",
"profilephoto": null,
"wage": 500,
"payments": 0,
"salary": "10000.00",
"rent": null,
"balance_owed": 10500
},
{
"id": 422,
"name": "jane",
"profilephoto": null,
"wage": 0,
"payments": 0,
"salary": "15000.00",
"rent": null,
"balance_owed": 15000
}
],
"first_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
"from": 1,
"last_page": 3,
"last_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
"label": "1",
"active": true
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"label": "2",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
"label": "3",
"active": false
},
{
"url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"label": "Next »",
"active": false
}
],
"next_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"path": "http://127.0.0.1:8000/api/v1/staff/list",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 25
}
Try
$pagination = $merchant_user_ac->staffs()->latest()->paginate(10);
$data = $pagination->getCollection()->map(function ($staff) {
$wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);
$payments = StaffPayment::where('merchant_id', AH::cMiD())
->where('user_id', $staff->id)
->when(!empty($start_date) && !empty($end_date), function ($q) use (
$start_date,
$end_date
) {
$q->whereBetween(\DB::raw('date(payment_date)'), [
$start_date,
$end_date,
]);
})
->sum('amount');
$balance_owed = $wage - $payments;
return [
'id' => $staff->id,
'name' => $staff->name,
'profilephoto' => $staff->profilephoto,
'wage' => $wage,
'payments' => $payments,
'salary' => $staff->salary,
'rent' => $staff->rent,
'balance_owed' => $balance_owed + $staff->salary - $staff->rent,
];
});
return response()->json($pagination->setCollection($data));
}

I need return the user_roles in graphql query

i need return my user with your role, i'm using prisma.io and nestjs + graphql;
this is my return in graphl
{
"data": {
"login": {
"user": {
"secure_id": "1086dd35-d9ab-442a-8311-af4720ed3d9a",
"name": "Super Admin",
"email": "super#admin.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InN1cGVyQGFkbWluLmNvbSIsInN1YiI6IjEwODZkZDM1LWQ5YWItNDQyYS04MzExLWFmNDcyMGVkM2Q5YSIsImlhdCI6MTY0OTY4NDg5NSwiZXhwIjoxNjQ5Njg4NDk1fQ.bwQRzcQ_gVR2wlVGPpKdSH1yZ6e6ipmENB3NqhKeN-8"
}
}
}
and this is the console.log of the user at moment of signin:
{
id: 5,
secure_id: '1086dd35-d9ab-442a-8311-af4720ed3d9a',
name: 'Super Admin',
email: 'super#admin.com',
password: '$2b$10$ioWYGeN.luOAzB9KdUjsp.qYPkgoqLtEqqcoN05dljgXQXPkX2N6W',
created_at: 2022-04-07T19:37:22.081Z,
updated_at: 2022-04-07T19:37:22.082Z,
UserRoles: [
{
user_id: 5,
roles_id: 2,
created_at: 2022-04-07T19:37:22.096Z,
updated_at: 2022-04-07T19:37:22.096Z
}
]
}
in the log i receive the UserRoles, but not in the signin.
this is the repository: https://github.com/paulozy/tgl-nest-and-graphql

How to return only the child models of a one to many relationship - Laravel

I was hoping I could get some assistance here.
Here is my model structure:
Property 1
--Image 1
--Image 2
Property 2
--Image 3
--Image 4
What I'm trying to do is retrieve all Image models.
This is what I have tried:
$properties = Auth::user()
->landlord_profile_auto
->properties()
->with('images')
->get();
dd($properties->images);
Property [images] does not exist on this collection instance.
Thanks a lot
EDIT:
Here is the dump of a property:
>>> Property::whereHas('images')->with('images')->first()
=> App\Models\Property {#3064
id: 3,
created_at: "2019-01-23 17:31:34",
updated_at: "2019-01-23 20:22:45",
address_line_1: "ABC",
address_line_2: "ABC",
unit: "calculateStorageUsage",
city: "ABC",
postal_code: "calculateStorageUsage",
url_slug: null,
is_draft: 0,
is_refurb: 0,
purchased_date: "2019-01-23",
bedrooms: 3,
max_tenants: null,
rent_amount: "589595.00",
currency_id: 252,
country_id: 491,
rent_frequency_id: 7,
property_type_id: null,
featured_image_id: 4,
landlord_profile_id: 6,
images: Illuminate\Database\Eloquent\Collection {#3059
all: [
App\Models\Image {#3078
id: 4,
created_at: "2019-01-23 20:22:42",
updated_at: "2019-01-23 20:22:42",
name: null,
caption: null,
path: "property_images/Ba1XYIB394xLsH4365391beAZ.jpg",
size_kb: 849.85,
imageable_type: "App\Models\Property",
imageable_id: 3,
},
App\Models\Image {#3077
id: 5,
created_at: "2019-01-23 20:22:45",
updated_at: "2019-01-23 20:22:45",
name: null,
caption: null,
path: "property_images/An3bgmKJzMcPuZyYizp9Lm6dj.jpg",
size_kb: 849.85,
imageable_type: "App\Models\Property",
imageable_id: 3,
},
],
},
}
Properties has a one to many relationship to images. Images is a polymorphic table.
Try this:
#foreach($properties->images as $image)
{
// here you can acess each image
}
#endforeach

How can I format following javascript arrays data in c3js?

I have to display the following data in a c3js multi-line chart.
The date will be the x axis.
each row will be each line in the chart.
When I try to display this in multi line chart it doesn't display and also not giving any error.
Please help me.
["date", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04", "2017-05-06", "2017-05-08", "2017-05-09", "2017-05-10", "2017-05-12", "2017-05-13", "2017-05-16", "2017-05-17", "2017-05-18", "2017-05-19", "2017-05-20", "2017-05-23", "2017-05-24", "2017-05-25", "2017-05-29"]["Shrirang Adkar", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Delhi Store", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Ajay Subramaniam", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Shivani Yadavalli", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Siddhesh 2020", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Salva", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Iranna Zampa", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Hisam", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null] ["Samruddhi Sawant", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Manish K S", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null] ["Sukanya Ray", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Karen Ho", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Srishti Narang", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Sreejata Mitra", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]["Estroz", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
You can check this JSFiddle. You need to put , between every chunk of datas.
var chart = c3.generate({
data: {
x: 'date',
columns: [
["date", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04", "2017-05-06", "2017-05-08", "2017-05-09", "2017-05-10", "2017-05-12", "2017-05-13", "2017-05-16", "2017-05-17", "2017-05-18", "2017-05-19", "2017-05-20", "2017-05-23", "2017-05-24", "2017-05-25", "2017-05-29"],
["Shrirang Adkar", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Delhi Store", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Ajay Subramaniam", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Shivani Yadavalli", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Siddhesh 2020", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Salva", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Iranna Zampa", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Hisam", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Samruddhi Sawant", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Manish K S", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Sukanya Ray", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Karen Ho", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Srishti Narang", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Sreejata Mitra", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
["Estroz", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
]
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}
});

Customize Kendo Line chart?

]
Can I achieve the chart like the above with kendo? The idea is the "line" and "Longdash" type in one line.
Any help/suggestion would be very much appreciated.
You could have separate series for the dashed lines and hide them from legend:
series: [{
name: "India",
data: [3.907, 7.943, 7.848, 9.284, 9.263, 9.801, 3.890, 8.238, 9.552, null]
},{
name: "World",
data: [1.988, 2.733, 3.994, 3.464, 4.001, 3.939, 1.333, -2.245, 4.339, null]
},{
name: "India-predict",
data: [null, null, null, null, null, null, null, null, 9.552, 10.855],
dashType: "dash",
visibleInLegend: false,
color: "red"
},{
name: "World-predict",
data: [null, null, null, null, null, null, null, null, 4.339, 5.727],
dashType: "dash",
visibleInLegend: false,
color: "red"
}],
DEMO

Resources