Laravel Many to Many Relationship with 2 foreign key - laravel

I have 3 tables - users, graduation_institutes, graduation_degrees
I defined a pivot table - graduation_degree_user
graduation_degree_user table has data like below -
id--------doctor_id--------graduation_degree_id----------graduation_institute_id---------year
In my User model, I defined relation like below -
public function graduations(){
return $this->belongsToMany('App\Models\User\GraduationDegree')->withTimestamps();
}
In my controller, I m doing -
if($request->has('graduations')){
$user->graduations()->sync($request->graduations);
}
return $user->graduations;
Where graduations is an array which contains multiple object.
I am getting the following response -
[
{
"id": 3,
"degree": "MD",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 2,
"graduation_degree_id": 3,
"graduation_institute_id": 1,
"created_at": "2020-12-01T07:54:23.000000Z",
"updated_at": "2020-12-01T07:54:23.000000Z"
}
},
{
"id": 4,
"degree": "MBBS",
"created_at": null,
"updated_at": null,
"pivot": {
"user_id": 2,
"graduation_degree_id": 4,
"graduation_institute_id": 2,
"created_at": "2020-12-01T07:54:24.000000Z",
"updated_at": "2020-12-01T07:54:24.000000Z"
}
}
]
In response, I am getting graduation_institute_id but I also want the name of the institute which is a field 'institute' defined in graduation_institutes table.

Related

Laravel Eloquent with query

I want to pull the invoice detail with the "id" number of 3 in the tools table from the "invoice_details" table. "plate" "invoice_details" relationship "plate_no" in the "trucks" table, but when I pull the data, a different data is coming, where am I making a mistake?
public function getTruck($id)
{
$truck = Truck::find($id)->with(['truckHistory'])->first();
return $truck;
}
Truck extends Model
public function truckHistory(){
return $this->hasMany(InvoiceDetail::class,'plate_no','plate');
}
Normally the incoming data is TRUE when I don't write a with condition
THIS IS TRUE
$truck = Truck::find(3);
{
"id": 3,
"plate": "73AD323",
"created_at": "2021-10-13T08:38:23.000000Z",
"updated_at": "2021-10-13T08:38:23.000000Z"
}
When I type a condition, the id is wrong.
$truck = Truck::find(3)->with(['truckHistory'])->first();
{
"id": 1,
"plate": "33EER36",
"created_at": "2021-10-11T06:01:29.000000Z",
"updated_at": "2021-10-11T06:01:29.000000Z",
"truck_history": [
{
"id": 1,
"plate_no": "33EER36",
"created_at": "2021-10-11T06:03:16.000000Z",
"updated_at": "2021-10-11T06:03:16.000000Z"
},
{
"id": 2,
"plate_no": "33EER36",
"created_at": "2021-10-11T06:06:18.000000Z",
"updated_at": "2021-10-11T06:06:18.000000Z"
}
]
}
Using first will return the first record of the trucks table, to do so you can use
Truck::with(['truckHistory'])->find($id);

How to merge two collections with a specific condition in laravel?

I have Two collections competences and coCompetences those collections look like :
competence :
...
{
"id": 6,
"category_id": 17,
"user_id": 1,
"objective_level": 4,
"current_level": 4,
"target_date": "2021-11-28",
"obtained_date": "2022-10-14",
"comment": "",
"created_at": "2020-01-08 10:06:28",
"updated_at": "2020-01-08 10:06:28",
"name": null,
"competenceName": "Hierarchy Building & BOM (Bill of Material)",
"category": {
"id": 17,
"competence": "Hierarchy Building & BOM (Bill of Material)",
"created_at": "2020-01-08 09:53:55",
"updated_at": "2020-01-08 09:53:55",
"wheel_id": 10
}
},
{
...
coCompetences :
{
"category": {
"id": 12,
"competence": "Criticality Analysis",
"created_at": "2020-01-08 09:53:55",
"updated_at": "2020-01-08 09:53:55",
"wheel_id": 10
},
"user_id": 1,
"competenceName": "Criticality Analysis",
"category_id": 12,
"objective_level": 0,
"current_level": 0,
"target_date": "2020-01-14",
"obtained_date": "2020-01-14",
"comment": ""
},
I would like to push only coCompetences elements into competences where coCompetences.category_id are not exist in competences.category_id
in another way : add all coCompetences elements to competences except those where coCompetences.category_id already exists in coCompetences.
I don't want to write code instead of you, algorithm will be enough, I hope.
simplest way
Take array Ids from first array, for example by pluck()
Filter second and return items that don't exist in first
push filtered data to first

How to map Laravel API resources for Non-Related Models

Hi I'm creating non related model resources. Inventories and Categories. They have child relationship models but they are not connected directly.
Sample:
{
"data": {
"inventories": [
{
"id": 1,
"user_id": 1,
"sub_category_id": 6,
"created_at": "2019-03-08 03:00:00",
"updated_at": "2019-03-08 03:00:00",
"deleted_at": null,
"get_inventory_details": {
"id": 1,
"inventory_id": 1,
"image_path": "Pahiram-Drone1-1.png",
"name": "Drone i1",
"description": "Drone i1 The Best Drone yet!",
"quantity": 10,
"cost_per_day": 1000,
"cost_per_hour": 100,
"status": "0",
"created_at": "2019-03-08 03:00:00",
"updated_at": "2019-03-08 03:00:00",
"deleted_at": null
}
}
],"categories": [
{
"id": 1,
"category_id": 1,
"parent_id": null,
"created_at": "2019-03-08 03:00:00",
"updated_at": "2019-03-08 03:00:00",
"deleted_at": null,
"get_sub_category_details": {
"id": 1,
"sub_category_id": 1,
"category_type_id": 1,
"name": "Drone DJI 1",
"description": "Drone DJI 1",
"created_at": "2019-03-08 03:34:23",
"updated_at": "2019-03-08 03:34:23",
"deleted_at": null
}
}
]
}
}
This is my sample data
It works with:
public function toArray($request)
{
return parent::toArray($request);
}
But not:
public function toArray($request)
{
return [
'inventory_ids' => $this->inventories->id,
'category_ids' => $this->categories->id
];
}
My expected result is to be able to map my response.
You Don't access direct id from categories and inventories, because its an array not object.
This should be usefull for you.
$categoryIds = Collection::make($this->categories)->pluck('id')->toArray();
$inventoryIds = Collection::make($this->inventories)->pluck('id')->toArray();
return [
'category_ids' => $categoryIds,
'inventory_ids' => $inventoryIds
];

Laravel get only one column from relation

I have a table user_childrens whose contains id_parent and id_user.
I'm trying to list all childrens of the parent with this:
code:
//relation in model via belongsTo
$idparent = auth('api')->user()->id;
$list = UserChildren::where('id_parent',$idparent)
->with('child:id,name,email')
->get();
return $list->toJson();
The return is:
[
{
"id": 1,
"id_parent": 1,
"id_user": 1,
"created_at": null,
"updated_at": null,
"child": {
"id": 1,
"name": "Mr. Davin Conroy Sr.",
"email": "prempel#example.com"
}
},
{
"id": 4,
"id_parent": 1,
"id_user": 2,
"created_at": null,
"updated_at": null,
"child": {
"id": 2,
"name": "Krystel Lehner",
"email": "cernser#example.net"
}
}
]
But it's API so I want only the child column like:
[
{
"id": 1,
"name": "Mr. Davin Conroy Sr.",
"email": "prempel#example.com"
},
{..}
]
UserChildren Model:
public function child() {
return $this->belongsTo('App\User','id_user','id');
}
I know that I could do this via .map() on collection but maybe there is other solution already on this query
You can use this code
$idparent = auth('api')->user()->id;
$childs = User::whereHas('user_childrens', function ($query) use ($idparent) {
$query->where('id_parent', $idparent);
})->get(['id', 'name', 'email']);
dd($childs->toJson());
And User model define user_childrens relation.
public function user_childrens()
{
return $this->hasMany('App\UserChildren','id_user','id');
}
See also docs https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence

Laravel - pivot data not available in email

I have the relations in the models as follows:
class HotelBooking extends Model
{
public function rooms(){
return $this->belongsToMany('App\Room', 'hotelbooking_room', 'hotelbooking_id')->withPivot(['quantity', 'addon_id']);
}
}
When I call $booking->rooms()->get();, I should get the following data:
"rooms": [
{
"id": 28,
"hotel_id": 89,
"type": "Double",
"quantity": 4,
"adults": 2,
"children": 1,
"description": null,
"created_at": "2016-05-15 12:24:39",
"updated_at": "2016-05-15 15:29:58",
"pivot": {
"hotelbooking_id": 30,
"room_id": 28,
"quantity": 1,
"addon_id": 386
}
}
]
Which is normally correct. What's weird is that when I send $rooms to the email data, the pivot attribute is not sent. It works well along the website but in case of sending it in the email it does not. Any idea why?!

Resources