I have the following eloquent call:
\App\Models\AwardRecommendation::with('entities', 'countries')->get();
When I ask for with 'countries' is because the 'entities' table has the country_id and not the 'awards' table.
The models I have are: AwardRecommendation, Entity, ErnySans\Laraworld\Models\Countries
What I'm trying to do is to get the country data in the query but in the return response I just get:
0 => array:27 [▼
"id" => 1
"guid" => "268f1080-67f3-48e5-844d-96b8c09c339b"
"user_id" => null
"title" => "Example Title"
"slug" => "example-title"
"intro" => null
"description" => null
"type" => "award"
"scope" => "firm"
"scope_name" => "1"
"scope_practice_area_id" => 1
"entity_id" => 1
"year" => null
"image_badge" => null
"image_one" => null
"image_two" => null
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 0
"created_by" => null
"updated_by" => null
"deleted_by" => null
"created_at" => null
"updated_at" => null
"entities" => array:1 [▼
0 => array:45 [▼
"id" => 1
"guid" => "b00e2be5-e1bb-4f98-8a35-30f2f7dfceba"
"award_id" => 1
"entity_name" => "Example entity"
"slug" => "example-entity"
"url" => null
"description" => null
"title" => "Mr."
"first_name" => "John"
"last_name" => "McNamarca"
"email" => "john.mcnamara#examplecompany.com"
"department" => null
"phone" => "+1 (646) 412 123456"
"mobile_phone" => null
"home_phone" => null
"other_phone" => null
"fax" => null
"assistant" => null
"assistant_phone" => null
"do_not_call" => 0
"do_not_email" => 0
"do_not_fax" => 0
"email_opt_out" => 0
"fax_opt_out" => 0
"street" => null
"city" => null
"country_state" => null
"zip" => null
"country_id" => 240
"street_other" => null
"city_other" => null
"state_other" => null
"zip_other" => null
"country_id_other" => 240
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 0
"created_by" => null
"updated_by" => null
"deleted_by" => null
"deleted_at" => null
"created_at" => null
"updated_at" => null
"pivot" => array:1 [▼
"entity_id" => 1
]
]
]
"countries" => null
How can I get this done with Laravel Eloquent?
Thanks in advance for any help
You need to define the relationship in the Entity model first:
public function country()
{
return $this->belongsTo(ErnySans\Laraworld\Models\Countries::class);
}
And make sure ErnySans\Laraworld\Models\Countries is the correct full class name for Countries model.
Then load award recommendations with related entities and with each entity's country:
AwardRecommendation::with('entities.country')->get();
Related
This is some wierd situation in which I am there right now like I have one object
$this->remitter;
if I dump this I get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
+timestamps: false
#primaryKey: "id"
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
#original: array:15 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "9946617986"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
]
Now I did this
$remitterDetails = $this->remitter;
$remitterDetails->beneficiaries = $remitterDetails->beneficiaries->take(50)->map(function ($beneficiary) {
return [
'id' => $beneficiary->bene_id,
'name' => $beneficiary->name,
'account' => $beneficiary->account,
'ifsc' => $beneficiary->ifsc,
'bank' => $beneficiary->bank,
'verificationDt' => $beneficiary->last_success_dt,
];
});
Now the confusing part is this mapping changed my $this->remitter too
after dumping $this->remitter again i get this
App\Models\IpayAppBc\Remitter {#1960
#connection: "ipay_app_bc__write"
+table: "remitter"
#attributes: array:16 [
"id" => 15016478
"name" => "ASKHSYA"
"name_last" => ""
"dob" => null
"mobile" => "1234567"
"limit_expiry" => "{"PB":"202212"}"
"address" => "PANCHKULA PANCHKULA HARYANA"
"city" => "PANCHKULA"
"pincode" => "134117"
"state" => "HARYANA"
"two_factor" => 0
"status" => 0
"otp_status" => 2
"paytm_key" => "h00097ea42dc-a6ed-42bc-b2cf-3b6a876f878b"
"ab_kyc" => 0
"beneficiaries" => Illuminate\Support\Collection {#1956
#items: array:2 [
0 => array:6 [
"id" => "898ddaeff5a54bf2f05a3ce00f5cd13a"
"name" => "DUMMY"
"account" => "37893810436"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => "2022-12-01 16:58:57"
]
1 => array:6 [
"id" => "c647579b5881ffc39cb2ffb1fc600634"
"name" => "MRAN DW"
"account" => "3789381043"
"ifsc" => "SBIN0000001"
"bank" => "STATE BANK OF INDIA"
"verificationDt" => null
]
]
#escapeWhenCastingToString: false
}
I am just assuming why this happened is there any referencing happening because as per my knowledge this only happens when I use referencing to any variable ?
any information on this please
Thanks
I want to create a nested array which maintain parent child relations
by using their parent_id.I want to make the following query results
into nested array.NB:I have used same table to keep all category data.
$catItems= ProductCategory::all()->toArray();
$source = array();
foreach($catItems as $key => $value) {
$source[$key] = $value;
}
the following method I applied to create nested array
$nested = array();
foreach ($source as &$s) {
if(is_null($s['parent_id']) ) {
// no parent_id so we put it in the root of the array
$nested[] = &$s;
}
else {
$pid = $s['parent_id'];
if ( isset($source[$pid])) {
// If the parent ID exists in the source array
// we add it to the 'children' array of the parent after initializing it.
if ( !isset($source[$pid]['children']) ) {
$source[$pid]['children'] = array();
}
$source[$pid]['children'][] = &$s;
}
}
}
If i do dd($source) i am getting the following output
array:3 [▼
0 => array:8 [▼
"id" => 5
"name" => "Electronics"
"status" => 1
"parent_id" => null
"created_by" => 1
"updated_by" => null
"created_at" => "2021-05-06T06:32:37.000000Z"
"updated_at" => "2021-05-06T06:32:37.000000Z"
]
1 => array:8 [▼
"id" => 6
"name" => "tv"
"status" => 1
"parent_id" => 5
"created_by" => 1
"updated_by" => null
"created_at" => "2021-05-06T06:33:00.000000Z"
"updated_at" => "2021-05-06T06:33:00.000000Z"
]
2 => array:8 [▼
"id" => 7
"name" => "Home appliances"
"status" => 1
"parent_id" => null
"created_by" => 1
"updated_by" => null
"created_at" => "2021-05-06T06:33:16.000000Z"
"updated_at" => "2021-05-06T06:33:16.000000Z"
]
]
If i do dd($nested) i am getting the only parent array like bellow :
array:2 [▼
0 => & array:8 [▼
"id" => 5
"name" => "Electronics"
"status" => 1
"parent_id" => null
"created_by" => 1
"updated_by" => null
"created_at" => "2021-05-06T06:32:37.000000Z"
"updated_at" => "2021-05-06T06:32:37.000000Z"
]
1 => & array:8 [▼
"id" => 7
"name" => "Home appliances"
"status" => 1
"parent_id" => null
"created_by" => 1
"updated_by" => null
"created_at" => "2021-05-06T06:33:16.000000Z"
"updated_at" => "2021-05-06T06:33:16.000000Z"
]
]
Create this function to access children in ProductCategory.php file:
public function children()
{
return $this->hasMany(ProductCategory::class, 'parent_id');
}
In your controller:
$cartItems = ProductCategory::with('children')->get();
If you want to get parent of any child category:
public function parent()
{
return $this->belongsTo(ProductCategory::class, 'parent_id');
}
You can get its parents like this
public function parent()
{
return $this->belongsTo(ProductCategory::class)->with('parent');
}
I have SQL query & sub query results and then convert these results to collection as bellow
$co=collect($users);
my collection array is as bellow
Illuminate\Support\Collection^ {#1984
#items: array:4 [
"id" => 92
"user" => "abc"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
"groups" => array:6 [
0 => array:6 [
"id" => 1
"user_id" => 1
"group_id" => 92
"title" => "new group"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
1 => array:6 [
"id" => 1
"user_id" => 1
"group_id" => 91
"title" => "other group"
"created_at" => "2020-04-16T12:13:11.000000Z"
"updated_at" => "2020-12-05T20:32:56.000000Z"
as you can see there is two columns with name of {id} , one in first array and other in sub array "groups"
I want to search the collection for ID and for title
I am using these command but both are returning empty array.
$filtered = $co->where('id',1);
dd($filtered);
also
$filtered = $co->where('title','new group');
dd($filtered)
;
or
$filtered = $co->where('groups.title','new group');
dd($filtered);
how to get these values
Thanks
I managed to create a Customer in ChargeBee but I don't know how to access each of its values.
To receive the information I use the following code, it works perfect for me ...
ChargeBee_Environment::configure("mysite-test","test_afhjsfslfsfslk438493sshgeh");
$result = ChargeBee_Customer::retrieve("16HHGDGJslfksds");
$customer = $result->customer();
$card = $result->card();
By doing a dd($result) I get the following:
ChargeBee_Result {#356 ▼
-_response: array:1 [▼
"customer" => array:22 [▼
"id" => "16HHGDGJslfksds"
"first_name" => "User"
"last_name" => "Test"
"email" => "user#mail.com"
"phone" => "555667788"
"auto_collection" => "on"
"net_term_days" => 0
"allow_direct_debit" => false
"created_at" => 1603420880
"taxability" => "taxable"
"updated_at" => 1603420880
"pii_cleared" => "active"
"resource_version" => 1603420880216
"deleted" => false
"object" => "customer"
"billing_address" => array:6 [▶]
"card_status" => "no_card"
"promotional_credits" => 0
"refundable_credits" => 0
"excess_payments" => 0
"unbilled_charges" => 0
"preferred_currency_code" => "USD"
]
]
-_responseObj: array:1 [▼
"customer" => ChargeBee_Customer {#358 ▶}
]
}
How can I get for example the id of this Customer??? Which in this case is 16HHGDGJslfksds
You can use this line:
$customerId = $result->customer()->id
I'm having some trouble to iterate over the object (below) as it has the main User data and then the profile, jobs and photos arrays.
I'm getting the errors Invalid argument supplied for foreach()
Thanks in advance for any help here as I seem to got into a mental loop and cannot find what I'm doing wrong.
In the Controller iI call the DB:
$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();
$team->jsonSerialize();
What I've done to loop in the view for the variable that carries the object that gives me an error
<li>
{!! trans('global.words.partners') !!}
<ul class="dropdown">
#foreach($team as $member)
#foreach($member->jobs as $jobs)
// just display if partner | socio
#if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
#foreach($member->profile as $profile)
<li>
<a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
</a>
</li>
#endforeach
#endif
#endforeach
#endforeach
</ul>
</li>
The object (reduced version):
0 => array:15 [▼
"id" => 2
"guid" => "0b6e0ad2-7daa-4c2b-907a-d095ab577851"
"name" => "John Doe"
"slug" => "john-doe"
"email" => "mp#testingsite.com"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 19:59:47"
"updated_at" => "2018-02-12 19:59:52"
"profile" => array:23 [▼
"id" => 2
"user_id" => 2
"full_name" => "John Doe McNamara"
"full_name_slug" => "john-doe-mcnamara"
"short_name" => "John Doe"
"short_name_slug" => "john-doe-mcnamara"
"gender" => "male"
"birth_date" => "1973-05-05"
"work_start" => "2000-01-01"
"work_end" => null
"biography" => "John Doe started the firm in 1900."
"experience" => "A couple of years"
"education" => """
UCLA;\n
Berkley
"""
"affiliations" => ""
"notes" => ""
"hits" => 3
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:07:00"
"updated_at" => "2018-02-24 12:32:52"
]
"jobs" => array:2 [▼
0 => array:15 [▼
"id" => 1
"guid" => "45f91a68-d219-42b1-8980-8cb77d6688b4"
"type" => "Lawyer"
"slug" => "lawyer"
"department" => "Law"
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 4
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-11 01:43:04"
"updated_at" => null
"pivot" => array:2 [▼
"user_id" => 2
"job_id" => 1
]
]
1 => array:15 [▼
"id" => 11
"guid" => "fcf9f310-eeb1-4ff4-b133-6c80d6bf3b7f"
"type" => "Partner"
"slug" => "partner"
"department" => "Law"
"notes" => null
"language" => "en"
"state" => 1
"ordering" => 1
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-11 01:49:38"
"updated_at" => null
"pivot" => array:2 [▼
"user_id" => 2
"job_id" => 11
]
]
]
"photos" => array:4 [▼
0 => array:14 [▼
"id" => 5
"guid" => null
"user_id" => 2
"type" => "profile"
"directory" => "staff"
"subdirectory" => "profile"
"filename" => "john-doe#1-100.jpg"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:03:31"
"updated_at" => null
]
1 => array:14 [▼
"id" => 6
"guid" => null
"user_id" => 2
"type" => "bgs"
"directory" => "staff"
"subdirectory" => "bgs"
"filename" => "john-doe#1-100.jpg"
"state" => 1
"ordering" => 0
"created_by" => 1
"updated_by" => null
"deleted_by" => null
"created_at" => "2018-02-12 20:03:49"
"updated_at" => null
]
2 => array:14 [▶]
3 => array:14 [▶]
]
]
1 => array:15 [▶]
2 => array:15 [▶]
3 => array:15 [▶]
4 => array:15 [▶]
5 => array:15 [▶]
6 => array:15 [▶]
7 => array:15 [▶]
8 => array:15 [▶]
9 => array:15 [▶]
10 => array:15 [▶]
11 => array:15 [▶]
12 => array:15 [▶]
13 => array:15 [▶]
]
You are getting an error you used JsonSerializeed
Here is the code the that you can use
$team = \App\User::where('users.state', 1)->with('profile','jobs','photos')->get();
and in your view iterate over it as you already doing
<li>
{!! trans('global.words.partners') !!}
<ul class="dropdown">
#foreach($team as $member)
#foreach($member->jobs as $jobs)
// just display if partner | socio
#if ($jobs->slug== 'socio' || $jobs->slug == 'partner')
#foreach($member->profiles as $profile)
<li>
<a href="/{!! str_slug(trans('global.words.lawyer_male'), '-') !!}/{!! $member->profile->full_name_slug !!}" title="{!! $member->profile->full_name !!}"><img class="avatar" src="{{ asset('/images/staff/thumbs/'.$member->photos[3]->filename) }}" alt="{!! $member->profile->full_name !!}"> {!! $member->profile->full_name !!}
</a>
</li>
#endforeach
#endif
#endforeach
#endforeach
</ul>
</li>
Hope this helps