Laravel Eloquent - Get Eloquent Relationship Model Data From nested Relationship - laravel

I have 3 models like this-
User
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'profile_picture',
'first_name',
'last_name',
'email',
'cell_no',
'website',
'date_of_birth',
'social_security_number_p1',
'social_security_number_p2',
'social_security_number_p3',
'address',
'location_latitude',
'location_longitude',
'password',
'user_type',
'is_enabled'
];
protected $hidden = [
'password',
'remember_token',
'id',
'user_type',
'is_enabled',
'created_at',
'updated_at'
];
public function advertisement()
{
return $this->hasMany('App\Advertisement', 'user_id');
}
}
Advertisement
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class Advertisement extends Model
{
protected $fillable = [
'user_id',
'category_id',
'sub_category_id',
'title',
'price',
'description',
'address',
'location_lat',
'location_lon',
'is_active',
'deleted_at'
];
public function User()
{
return $this->belongsTo('App\User','user_id', 'id');
}
public function Offer()
{
return $this->hasMany('App\Offer', 'add_id', 'id');
}
}
Offer
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Offer extends Model
{
protected $fillable = [
'add_id',
'user_id',
'price',
'message'
];
public function User()
{
return $this->hasOne('App\User','user_id','id');
}
public function Advertisement()
{
return $this->hasOne('App\advertisement','add_id','id');
}
}
I want to get advertisements with offer and offers with offer sender (user).
So, I am doing something like this-
return Advertisement::with('AdvertisementImages')
->with('Offer')
->with('User')
->where('user_id', Auth::user()->id)
->get();
And getting this-
[
{
"id": 1,
"user_id": 16,
"title": "123asd",
"price": 123,
"description": "asd asd asd ",
"address": "Dhaka University Campus, Dhaka, Dhaka Division, Bangladesh",
"location_lat": 23.73,
"location_lon": 90.39,
"total_views": "110",
"avg_rating": "4.0000",
"is_reviewed": 0,
"is_offer_sent": 1,
"advertisement_images": [
{
"image_name": "16-68261.jpg"
},
{
"image_name": "16-34746.JPG"
},
{
"image_name": "16-79570.jpg"
}
],
"offer": [
{
"id": 1,
"add_id": 1,
"user_id": 9,
"price": 123,
"message": "asd ad asdasd ",
"created_at": "2016-08-11 02:22:43",
"updated_at": "2016-08-11 02:22:43"
},
{
"id": 2,
"add_id": 1,
"user_id": 15,
"price": 43,
"message": "as dfasd ",
"created_at": "2016-08-11 02:24:04",
"updated_at": "2016-08-11 02:24:04"
},
{
"id": 3,
"add_id": 1,
"user_id": 16,
"price": 23,
"message": "hgf",
"created_at": "2016-08-11 02:25:31",
"updated_at": "2016-08-12 01:37:42"
}
],
"user": {
"profile_picture": "",
"first_name": "Ne",
"last_name": "Ajgor",
"cell_no": null,
"email": "ajgor#me.com",
"website": "",
"date_of_birth": "0000-00-00",
"social_security_number_p1": "",
"social_security_number_p2": "",
"social_security_number_p3": "",
"address": "",
"location_latitude": 0,
"location_longitude": 0
}
}
]
So, it is wrong. I am getting user from Advertisement.
But I like to have user from Offer.
So, I need to have something like this-
[
{
"id": 1,
"user_id": 16,
"title": "123asd",
"price": 123,
"description": "asd asd asd ",
"address": "Dhaka University Campus, Dhaka, Dhaka Division, Bangladesh",
"location_lat": 23.73,
"location_lon": 90.39,
"total_views": "110",
"avg_rating": "4.0000",
"is_reviewed": 0,
"is_offer_sent": 1,
"advertisement_images": [
{
"image_name": "16-68261.jpg"
},
{
"image_name": "16-34746.JPG"
},
{
"image_name": "16-79570.jpg"
}
],
"offer": [
{
"id": 1,
"add_id": 1,
"user_id": 9,
"price": 123,
"message": "asd ad asdasd ",
"created_at": "2016-08-11 02:22:43",
"updated_at": "2016-08-11 02:22:43",
"user": {
"profile_picture": "",
"first_name": "Me",
"last_name": "Ajgor",
"cell_no": null,
"email": "ajgor#me.com",
"website": "",
"date_of_birth": "0000-00-00",
"social_security_number_p1": "",
"social_security_number_p2": "",
"social_security_number_p3": "",
"address": "",
"location_latitude": 0,
"location_longitude": 0
}
},
{
"id": 2,
"add_id": 1,
"user_id": 15,
"price": 43,
"message": "as dfasd ",
"created_at": "2016-08-11 02:24:04",
"updated_at": "2016-08-11 02:24:04",
"user": {
"profile_picture": "",
"first_name": "Ne",
"last_name": "Ajgor",
"cell_no": null,
"email": "ajgor#me.com",
"website": "",
"date_of_birth": "0000-00-00",
"social_security_number_p1": "",
"social_security_number_p2": "",
"social_security_number_p3": "",
"address": "",
"location_latitude": 0,
"location_longitude": 0
}
},
{
"id": 3,
"add_id": 1,
"user_id": 16,
"price": 23,
"message": "hgf",
"created_at": "2016-08-11 02:25:31",
"updated_at": "2016-08-12 01:37:42",
"user": {
"profile_picture": "",
"first_name": "Je",
"last_name": "Ajgor",
"cell_no": null,
"email": "ajgor#me.com",
"website": "",
"date_of_birth": "0000-00-00",
"social_security_number_p1": "",
"social_security_number_p2": "",
"social_security_number_p3": "",
"address": "",
"location_latitude": 0,
"location_longitude": 0
}
}
]
}
]
Can anyone please help me, what I need to call?
Thanks in advance for helping.

I have solved it like this-
Advertisement::with('AdvertisementImages')
->with('Offer','Offer.User')
->where('user_id', Auth::user()->id)
->has('Offer')
->get();

Related

Laravel groupBy when date does not exist

I'm running the code:
DowntimeHistoryMonitoring::whereDate('created_at', '<=', now())->get(['id', 'created_at'])->groupBy(function ($item){
return $item->created_at->format('Y-m-d');
});
Which returns:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
]
}
How can I do so that I get an empty result if I don't find records, and defined by a limit within the groupBy. Example with a limit of 5:
{
"2022-11-30": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T13:06:42.000000Z"
},
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-30T14:06:42.000000Z"
}
],
"2022-11-29": [
{
"id": "507a3832-c9d3-4baf-b6a8-ff8100c59897",
"created_at": "2022-11-29T13:06:42.000000Z"
}
],
"2022-11-28": [
],
"2022-11-27": [
],
"2022-11-26": [
],
}
What should I do?

jenssegers/laravel-mongodb One to Many (with reference) relationship doesn't work at all

I have 2 models created with jenssegers/laravel-mongodb package.
<?php
class CronJob extends Model {
protected $connection = 'mongodb';
protected $collection = 'cronjobs';
protected $primaryKey = '_id';
protected $dates = [
'created_at',
'updated_at'
];
public function details()
{
return $this->hasMany(CronJobDetail::class, 'parent_id', '_id');
}
}
class CronJobDetail extends Model {
protected $connection = 'mongodb';
protected $collection = 'cronjobsdetails';
protected $primaryKey = '_id';
protected $dates = [
'created_at',
'updated_at'
];
public function job()
{
return $this->belongsTo(CronJob::class, 'parent_id', '_id');
}
When I call CronJobDetail::first()->job I get the parent CronJob object successfully. But when I do the same as inverse like CronJob::first()->details, I get empty collection for some reason. Why isn't it working as expected?
Here's some example data for better insight:
CronJob document
{
"_id": {
"$oid": "6308cd0b2b290000c100086b"
},
"name": "Deneme-2022-08-26 16:39:23 (2)",
"job": "assignproduct",
"job_count": 2,
"product_id": "630869372b290000c1000865",
"product_name": "Deneme",
"tax_rate": 18,
"organisation": 111,
"created_by": 4,
"created_by_name": "Çağlar TUFAN",
"price": 150,
"currency": "TL",
"created_at": "2022-08-26 16:39:23",
"updated_at": "2022-08-26 16:39:23"
}
CronJobDetails documents
{
"_id": {
"$oid": "6308cd0b2b290000c100086b"
},
"name": "Deneme-2022-08-26 16:39:23 (2)",
"job": "assignproduct",
"job_count": 2,
"product_id": "630869372b290000c1000865",
"product_name": "Deneme",
"tax_rate": 18,
"organisation": 111,
"created_by": 4,
"created_by_name": "Çağlar TUFAN",
"price": 150,
"currency": "TL",
"created_at": "2022-08-26 16:39:23",
"updated_at": "2022-08-26 16:39:23"
},
{
"_id": {
"$oid": "6308cd0b2b290000c100086d"
},
"job": "assignproduct",
"email": "erhan#w3.com.tr",
"product_id": "630869372b290000c1000865",
"product_name": "Deneme",
"organisation": 111,
"status": 0,
"created_by": 4,
"created_by_name": "Çağlar TUFAN",
"price": 150,
"currency": "TL",
"created_at": "2022-08-26 16:39:23",
"updated_at": "2022-08-26 16:39:23",
"name": "Deneme-2022-08-26 16:39:23 (2)",
"parent_id": {
"$oid": "6308cd0b2b290000c100086b"
}
}

How to retrieve Json data type data in SQL using a where condition in LARAVEL

Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?
I want to display all the order that contains order->Product->user->id === 1
{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
{
"id": 26,
"Product": [
{
"id": 4,
"name": "Araliya Rice",
"desription": "Araliya Rice",
"salePrice": 500,
"category": "Rice",
"user": {
"id": 1,
"name": "Mohamed Naalir",
}
}
],
},
{
"id": 27,
"Product": [
{
"id": 2,
"name": "white sugar",
"desription": "aaa",
"salePrice": 100,
"category": "Sugar",
"user": {
"id": 5,
"name": "Mohamed Sharaf",
}
}
],
}
]
}
json where clauses
$orders = DB::table('orders')
->whereJsonContains('Product', [['user' => ['id' => 1]]])
->get();

How to get Order and OrderLineItem for a transaction using Connect V2?

I am writing a wrapper to retrive transaction data for the Square Connect API V2. I am able to retrive the trasactions with Order data missing.
I am getting following response using All Transaction and Retrieve Transaction API:
{
"transactions": [
{
"id": "mYziFkYv2QK7e2kb2vyIhegeV",
"location_id": "75S3K9Z9KSVYK",
"created_at": "2017-04-17T11:00:51Z",
"tenders": [
{
"id": "2qeDw6CmCs299m9w0RY7KQB",
"location_id": "75S3K9Z9KSVYK",
"transaction_id": "mYziFkYv2QK7e2kb2vyIhegeV",
"created_at": "2017-04-17T11:00:51Z",
"amount_money": {
"amount": 10000,
"currency": "INR"
},
"processing_fee_money": {
"amount": 0,
"currency": "INR"
},
"type": "OTHER"
}
],
"product": "REGISTER",
"client_id": "75S3K9Z9KSVYK-a776-4377-84f5-75S3K9Z9KSVYK"
},
{
"id": "UJsg9IdIv9WWvqT1h2VkbxgeV",
"location_id": "75S3K9Z9KSVYK",
"created_at": "2017-04-17T11:00:37Z",
"tenders": [
{
"id": "UVuQghb8RTF8OUcmAsaXKQB",
"location_id": "75S3K9Z9KSVYK",
"transaction_id": "UJsg9IdIv9WWvqT1h2VkbxgeV",
"created_at": "2017-04-17T11:00:37Z",
"amount_money": {
"amount": 0,
"currency": "INR"
},
"processing_fee_money": {
"amount": 0,
"currency": "INR"
},
"type": "NO_SALE"
}
],
"product": "REGISTER",
"client_id": "75S3K9Z9KSVYK-a751-4434-a041-75S3K9Z9KSVYK"
}
]}
Is there any way to get order (line item) details?
If you are looking for itemizations, you can use the v1 transactions endpoints.
See here: https://docs.connect.squareup.com/api/connect/v1/#get-paymentid

ArrowDB dashboard upload photo to user

when I upload a photo using the arrowdb dashboard on https://platform.appcelerator.com
Cloud.Users.query only shows the photo_id
but whne I created a new user using the dashboard and attached a phot it is showing in the Cloud.Users.query
eg . photo uploaded after user created
{
"id": "563019f18cb04aede69e2111",
"first_name": "store1",
"last_name": "123",
"created_at": "2015-10-28T00:42:25+0000",
"updated_at": "2016-01-22T08:59:44+0000",
"external_accounts": [],
"confirmed_at": "2015-10-28T00:42:25+0000",
"username": "user",
"admin": "false",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
},
"photo_id": "56a1dc083a654d090d126792",
"friend_counts": {
"requests": 0,
"friends": 0
}
}
eg. photo uploaded while creating user
{
"id": "56a1f0333a65234234390d7",
"first_name": "qqqq",
"last_name": "wwwe",
"created_at": "2016-01-22T09:02:43+0000",
"updated_at": "2016-01-22T09:07:18+0000",
"external_accounts": [],
"confirmed_at": "2016-01-22T09:02:43+0000",
"username": "qwe",
"admin": "false",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
},
"photo": {
"id": "56a1f0333a654d090d0390d8",
"filename": "userPhoto.jpg",
"size": 25394,
"md5": "e20f4fcadf6cde9fccfb458dd11951d4",
"created_at": "2016-01-22T09:02:43+0000",
"updated_at": "2016-01-22T09:02:43+0000",
"processed": true,
"urls": {
"original": "https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/xmqh1djNEIChtQFP6d37HNH5DQNCXQoX/photos/51/d4/56a1f0333a654d090d0390d9/userPhoto_original.jpg"
},
"content_type": "image/jpeg",
"user": {
"id": "56a1f0333a65234234390d7",
"first_name": "qqqq",
"last_name": "wwwe",
"created_at": "2016-01-22T09:02:43+0000",
"updated_at": "2016-01-22T09:07:18+0000",
"external_accounts": [],
"confirmed_at": "2016-01-22T09:02:43+0000",
"username": "qwe",
"admin": "false",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
},
"photo_id": "56a1f0333a654d090d0390d8",
"friend_counts": {
"requests": 0,
"friends": 0
}
}
},
"friend_counts": {
"requests": 0,
"friends": 0
}
}
basically the user which had photo uploaded during creation shows this extra info
"photo": {
"id": "56a1f0333a654d090d0390d8",
"filename": "userPhoto.jpg",
"size": 25394,
"md5": "e20f4fcadf6cde9fccfb458dd11951d4",
"created_at": "2016-01-22T09:02:43+0000",
"updated_at": "2016-01-22T09:02:43+0000",
"processed": true,
"urls": {
"original": "https://s3-us-west-1.amazonaws.com/storage-platform.cloud.appcelerator.com/xmqh1djNEIChtQFP6d37HNH5DQNCXQoX/photos/51/d4/56a1f0333a654d090d0390d9/userPhoto_original.jpg"
},
"content_type": "image/jpeg",
"user": {
"id": "56a1f0333a65234234390d7",
"first_name": "qqqq",
"last_name": "wwwe",
"created_at": "2016-01-22T09:02:43+0000",
"updated_at": "2016-01-22T09:07:18+0000",
"external_accounts": [],
"confirmed_at": "2016-01-22T09:02:43+0000",
"username": "qwe",
"admin": "false",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
},
I've been experiencing also that kind of problem, plus I've noticed that the images won't show up from the ArrowDB user interface (not sure if related).
The API returns only an empty object when you query the model:
photo: {}
I've been creating a ticket for the ArrowDB user interface https://jira.appcelerator.org/browse/API-1277.
sachinmw did you create a ticket for the initial problem yet?
A workaround could be to use the photo_id and run another separate query to retrieve the photo model but this is not good for network optimisation purposes.
EDIT
Ok after dealing with Appcelerator directly about having an empty photo: {} Object return the answer is pretty simple:
Whenever you use the query() function like Cloud.Objects.query() from ti.cloud on any ArrowDB object, there is a parameter called response_json_depth which is set to 1 by default and will only return one level of the JSON Object returned by the API.
Without touching that parameter I was seeing:
{
"Vehicle": [
{
"name": "foo",
"photo: {}
}
]
}
By setting response_json_depth to 3 I managed to have:
{
"Vehicle": [
{
"name": "foo",
"photo: {
"urls": {
"original": "http://bar.com"
}
}
}
]
}
Hope that will help someone. The also applies for the Cloud.Objects.show() method for any ArrowDB object.

Resources