I am writing a support ticket system in laravel. I need to establish relationships between tables. I'm new to Laravel. I need to get the messages of a ticket and the files of those messages. By reading the documentation, I have established a relationship between the messages belonging to the ticket and it works correctly. However, I need to get the files under those messages, can you help me with this?
The json of the messages is as follows:
{
"ticket": [
{
"id": 1,
"subject": "test 1",
"department_id": 1,
"priority": "low",
"status": "open",
"users_id": 1,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-04T17:31:10.000000Z",
"deleted_at": null,
"author": "Admin Admin",
"author_id": 1,
"department": "Genel",
"messages": [
{
"id": 1,
"admin_tickets_id": 1,
"users_id": 1,
"admin_users_id": null,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 5,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 9,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test için yaziyom",
"deleted_at": null,
"created_at": "2022-10-04T22:19:32.000000Z",
"updated_at": "2022-10-04T22:19:32.000000Z"
},
{
"id": 10,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:26:39.000000Z",
"updated_at": "2022-10-04T22:26:39.000000Z"
},
{
"id": 11,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:27:00.000000Z",
"updated_at": "2022-10-04T22:27:00.000000Z"
},
{
"id": 12,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:28:45.000000Z",
"updated_at": "2022-10-04T22:28:45.000000Z"
},
{
"id": 13,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "yine deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:32:34.000000Z",
"updated_at": "2022-10-04T22:32:34.000000Z"
}
]
}
]
}
but I want it to be like this:
Pay attention to the file below the message.
{
"ticket": [
{
"id": 1,
"subject": "test 1",
"department_id": 1,
"priority": "low",
"status": "open",
"users_id": 1,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-04T17:31:10.000000Z",
"deleted_at": null,
"author": "Admin Admin",
"author_id": 1,
"department": "Genel",
"messages": [
{
"id": 1,
"admin_tickets_id": 1,
"users_id": 1,
"admin_users_id": null,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
"attachments": [
{
"id": 1
"name": "deneme",
"extension": "pdf",
},
]
},
{
"id": 5,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test mesajıdır.",
"deleted_at": null,
"created_at": "2022-10-03T11:47:42.000000Z",
"updated_at": "2022-10-03T11:47:42.000000Z"
},
{
"id": 9,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test için yaziyom",
"deleted_at": null,
"created_at": "2022-10-04T22:19:32.000000Z",
"updated_at": "2022-10-04T22:19:32.000000Z"
},
{
"id": 10,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:26:39.000000Z",
"updated_at": "2022-10-04T22:26:39.000000Z"
},
{
"id": 11,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "test",
"deleted_at": null,
"created_at": "2022-10-04T22:27:00.000000Z",
"updated_at": "2022-10-04T22:27:00.000000Z"
},
{
"id": 12,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:28:45.000000Z",
"updated_at": "2022-10-04T22:28:45.000000Z"
},
{
"id": 13,
"admin_tickets_id": 1,
"users_id": null,
"admin_users_id": 1,
"reply": "yine deneme",
"deleted_at": null,
"created_at": "2022-10-04T22:32:34.000000Z",
"updated_at": "2022-10-04T22:32:34.000000Z"
}
]
}
]
}
I share my controllers and models:
TicketsController.php
public function show($id) {;
$data = array();
$data['ticket'] = AdminTickets::addSelect([
'author' => Users::select(DB::raw("name || ' ' || surname as fullname"))
->whereColumn('id', 'admin_tickets.users_id')
->limit(1),
'author_id' => Users::select('id')
->whereColumn('id', 'admin_tickets.users_id')
->limit(1),
'department' => AdminTicketDepartments::select('name')
->whereColumn('id', 'admin_tickets.department_id')
->limit(1),
])->where('id', $id)->with('messages', fn ($query) =>
$query->whereHas('attachments')
)->get();
return $data;
die();
return view('admin.tickets.show', $data);
}
AdminTickets Model:
public function messages()
{
return $this->hasMany(AdminTicketReplies::class);
}
AdminTicketReplies Model:
public function attachments() {
return $this->hasManyThrough(AdminTicketAttachments::class, AdminTicketReplies::class);
}
I think I'm making a mistake in the AdminTicketReplies model. How can I do that?
You are now getting all messages that have at least one attachment. you can add the attachments by using another with():
->where('id', $id)->with('messages', fn ($query) =>
$query->whereHas('attachments')
$query->with('attachments')
)->get();
If you want all messages and include the attachments, you can use the dot notation:
->where('id', $id)->with('messages.attachments')
Related
I would like to perform the average rating of respective product which stored in the pivot table called "reviews".Below are my existing code.
Product Model:
public function reviews(){
return $this->belongsToMany(User::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
public function getProductRatingAttribute(){
return $this->reviews()->average('rating');
}
User Model:
public function reviews(){
return $this->belongsToMany(Product::class,'reviews')->withPivot('comment','rating')->withTimestamps();
}
Migration for the reviews
Schema::create('reviews', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('product_id');
$table->string('comment')->nullable();
$table->integer('rating');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
I have attempted to use the below to code construct the expected output but it leads N+1 issue because it does not take advantage of the eager loading of the Laravel Eloquent.
$s = Product::with('reviews')->append('product_rating');
As I check telescope, it produces 6 query which can lead performance issue when the data is large.
Expected output:
{
"msg": [
{
"id": 4,
"created_at": "2021-04-09T07:32:35.000000Z",
"updated_at": "2021-04-09T07:32:35.000000Z",
"name": "MacDonald",
"nickname": "MCD",
"users_id": 1,
"price": "0.00",
"product_rating": "3.3333",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 3,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 2,
"name": "Kelvin Ooi",
"email": "kelvin.ooi#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-13T12:07:11.000000Z",
"role": 1,
"pivot": {
"product_id": 4,
"user_id": 2,
"comment": "Ouch",
"rating": 5,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
},
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 4,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 10,
"created_at": null,
"updated_at": null,
"name": "Mary Bown",
"nickname": "MB",
"users_id": 1,
"price": "2.88",
"product_rating": "2.0000",
"reviews": [
{
"id": 1,
"name": "John Smith",
"email": "john.smith#hotmail.com",
"email_verified_at": null,
"created_at": "2021-04-08T13:29:13.000000Z",
"updated_at": "2021-04-08T13:29:13.000000Z",
"role": 0,
"pivot": {
"product_id": 10,
"user_id": 1,
"comment": "Ouch",
"rating": 2,
"created_at": "2021-05-01T11:51:26.000000Z",
"updated_at": "2021-05-01T11:52:07.000000Z"
}
}
]
},
{
"id": 11,
"created_at": null,
"updated_at": null,
"name": "Pizza Hut",
"nickname": "pizzahut",
"users_id": 1,
"price": "4.10",
"product_rating": null,
"reviews": [
]
},
{
"id": 12,
"created_at": "2021-04-09T08:00:42.000000Z",
"updated_at": "2021-04-09T08:00:42.000000Z",
"name": "Domino Pizza",
"nickname": "domino",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
},
{
"id": 13,
"created_at": "2021-04-26T16:12:53.000000Z",
"updated_at": "2021-04-26T16:12:53.000000Z",
"name": "Chicken Chop",
"nickname": "chickchop",
"users_id": 3,
"price": "0.00",
"product_rating": null,
"reviews": [
]
}
]
}
Appending is performing the query on each record.
Why not perform average on the collection itself?
$s = Product::with('reviews')->get();
$avg = $s->avg('pivot.rating');
Alternatively you can modify the relationship method to include a raw select of the average rating.
I have a collection that looks like this,
[
{
"id": 1,
"title": "Cooking",
"deleted_at": null,
"created_at": null,
"updated_at": null,
"listings": [
{
"id": 6,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:35:56.000000Z",
"updated_at": "2020-06-03T22:35:56.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 6
},
"assets": [],
"primary_image": []
},
{
"id": 7,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:10.000000Z",
"updated_at": "2020-06-03T22:36:10.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 7
},
"assets": [],
"primary_image": []
},
{
"id": 8,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:26.000000Z",
"updated_at": "2020-06-03T22:36:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 8
},
"assets": [],
"primary_image": []
},
{
"id": 9,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:36:42.000000Z",
"updated_at": "2020-06-03T22:36:42.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 9
},
"assets": [],
"primary_image": []
},
{
"id": 10,
"title": "Listing Number 1",
"slug": "listing-number-1",
"description": "Description",
"booking_details": "Booking Details",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:37:40.000000Z",
"updated_at": "2020-06-03T22:37:40.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 10
},
"assets": [],
"primary_image": []
},
{
"id": 11,
"title": "Listing Number 2",
"slug": "listing-number-2",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:31.000000Z",
"updated_at": "2020-06-03T22:46:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 11
},
"assets": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
],
"primary_image": [
{
"id": 3,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
},
{
"id": 4,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 11,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:46:33.000000Z",
"updated_at": "2020-06-03T22:46:33.000000Z"
}
]
},
{
"id": 12,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:50:31.000000Z",
"updated_at": "2020-06-03T22:50:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 12
},
"assets": [],
"primary_image": []
},
{
"id": 13,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T22:57:12.000000Z",
"updated_at": "2020-06-03T22:57:12.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 13
},
"assets": [],
"primary_image": []
},
{
"id": 15,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:00:49.000000Z",
"updated_at": "2020-06-03T23:00:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 15
},
"assets": [],
"primary_image": []
},
{
"id": 16,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:21.000000Z",
"updated_at": "2020-06-03T23:01:21.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 16
},
"assets": [],
"primary_image": []
},
{
"id": 17,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:26.000000Z",
"updated_at": "2020-06-03T23:01:26.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 17
},
"assets": [],
"primary_image": []
},
{
"id": 18,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:01:49.000000Z",
"updated_at": "2020-06-03T23:01:49.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 18
},
"assets": [],
"primary_image": []
},
{
"id": 19,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:02:19.000000Z",
"updated_at": "2020-06-03T23:02:19.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 19
},
"assets": [],
"primary_image": []
},
{
"id": 20,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:03:31.000000Z",
"updated_at": "2020-06-03T23:03:31.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 20
},
"assets": [],
"primary_image": []
},
{
"id": 21,
"title": "Listing Number 3",
"slug": "listing-number-3",
"description": "Description 1",
"booking_details": "Booking Details 1",
"cost": "25.00",
"active": 0,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:25.000000Z",
"updated_at": "2020-06-03T23:06:25.000000Z",
"pivot": {
"category_id": 1,
"listing_id": 21
},
"assets": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
],
"primary_image": [
{
"id": 5,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:26.000000Z",
"updated_at": "2020-06-03T23:06:26.000000Z"
},
{
"id": 6,
"type": "image",
"path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
"is_primary": 1,
"assetable_type": "App\\Listing",
"assetable_id": 21,
"user_id": 2,
"deleted_at": null,
"created_at": "2020-06-03T23:06:27.000000Z",
"updated_at": "2020-06-03T23:06:27.000000Z"
}
]
}
]
}
]
I only want to see a collection that contains items that don't have empty assets array,
I have tried this,
$category = Category::whereHas('listings.assets')->with(['listings.assets', 'listings.primaryImage' => function($query){
$query->where('assets.is_primary', '=', 1);
}])->get();
return $category->filter(function($item){
foreach($item->listings as $i) {
return !empty($i->assets);
}
});
But this just returns an empty [] what am I doing wrong?
You are returning in your loop, so as soon as it finds listings that does not have assets it will break the loop and be false. Instead use contains(), that check if any of the elements, passes the given condition. If one does, it will return true. Therefor not breaking the loop on empty collections, as you do now.
return $category->filter(function($item){
return $item->listings->contains(function ($listing) {
return $listing->assets->isNotEmpty();
});
});
I'm working on a small project using Laravel, right now i display my products that i have in my database in HTML table.
So right now i have created another database table called variants, to store products variants,
i would like to count the number of variants that i have for each product
this is my code :
as you can see i select also the categories, brands, suppliers
i want to count the number of the variants ( since i have already a table called variants, contain the product_id field )
public function index()
{
$products = Product::with(['category', 'brand', 'supplier'])->orderBy('id', 'desc')->paginate(15);
return response()->json($products);
}
My current Result :
{
"current_page": 1,
"data": [
{
"id": 3,
"product_name": "Ipad",
"product_description": null,
"product_category": 4,
"product_supplier": 1,
"product_buying_price": "5500.00",
"product_selling_price": "66.00",
"product_min_stock": "10.00",
"product_max_stock": "10.00",
"product_alert_stock": 10,
"product_alert_phone": "+1 (1084) 565-43109",
"product_alert_email": "daqaxipig#mailinator.net",
"product_cost_price": "65.00",
"product_supplier_discount": "10.00",
"product_brand": 5,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 4,
"category_name": "TABLET",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 5,
"brand_name": "Apple",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab#dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
},
{
"id": 2,
"product_name": "Samsung A35",
"product_description": "test",
"product_category": 1,
"product_supplier": 1,
"product_buying_price": "500.00",
"product_selling_price": "500.00",
"product_min_stock": "10.00",
"product_max_stock": "20.00",
"product_alert_stock": 15,
"product_alert_phone": "+1 (1023) 803-2526",
"product_alert_email": "duvupihujo#mailinator.net",
"product_cost_price": "600.00",
"product_supplier_discount": "30.00",
"product_brand": 4,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 1,
"category_name": "Super Phone",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 4,
"brand_name": "Samsung",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab#dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
},
{
"id": 1,
"product_name": "Elton Mullins",
"product_description": "Possimus rerum comm",
"product_category": 2,
"product_supplier": 1,
"product_buying_price": "97.00",
"product_selling_price": "354.00",
"product_min_stock": "10.00",
"product_max_stock": "20.00",
"product_alert_stock": 5,
"product_alert_phone": "+1 (884) 587-2414",
"product_alert_email": "pywu#mailinator.com",
"product_cost_price": "989.00",
"product_supplier_discount": "10.00",
"product_brand": 3,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 2,
"category_name": "Phones",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 3,
"brand_name": "Oppo",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab#dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
}
],
"first_page_url": "http://app.quicknsales.com/backend/product?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://app.quicknsales.com/backend/product?page=1",
"next_page_url": null,
"path": "http://app.quicknsales.com/backend/product",
"per_page": 15,
"prev_page_url": null,
"to": 3,
"total": 3
}
i need to add a key for ex : nbr_variants : x, foreach data item
I assumed your product table hasMany relation with variants. As you said in comment products.id & variants.product_id
Make function variants in product model using hasMany relationship.
Class Product extends Model{
public function variants(){
return $this->hasMany('App\Variant','product_id','id');
}
}
And now you can call it by using withCount
public function index()
{
$products = Product::with(['category', 'brand', 'supplier','variants'])->withCount('variants')->orderBy('id', 'desc')->paginate(15);
return response()->json($products);
}
As a result, You'll get product with variants and count as well.
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();
I am trying to use groupBy with Laravel's with function. Here is my query
$msgs=Conversation::where('sender', $sender)
->orWhere('reciever', $sender)
->orderBy('created_at', 'desc')
->with('message')
->get();
This query returns many records from the message table but I want to retrieve only one.
This is data and I want to group the result from the message object
{
"data": [
{
"id": 27,
"sender": 10,
"reciever": 4,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04",
"message": [
{
"id": 5,
"conversation_id": 27,
"last_sender": 10,
"msg": "5",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04"
},
{
"id": 6,
"conversation_id": 27,
"last_sender": 10,
"msg": "6",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:19",
"updated_at": "2017-07-26 22:46:19"
},
{
"id": 7,
"conversation_id": 27,
"last_sender": 10,
"msg": "7",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:35",
"updated_at": "2017-07-26 22:46:35"
},
{
"id": 8,
"conversation_id": 27,
"last_sender": 10,
"msg": "8",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:36",
"updated_at": "2017-07-26 22:46:36"
}
]
},
{
"id": 26,
"sender": 10,
"reciever": 7,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02",
"message": [
{
"id": 1,
"conversation_id": 26,
"last_sender": 10,
"msg": "1",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02"
},
{
"id": 2,
"conversation_id": 26,
"last_sender": 10,
"msg": "2",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:36",
"updated_at": "2017-07-26 22:45:36"
},
{
"id": 3,
"conversation_id": 26,
"last_sender": 10,
"msg": "3",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:50",
"updated_at": "2017-07-26 22:45:50"
},
{
"id": 4,
"conversation_id": 26,
"last_sender": 10,
"msg": "4",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:57",
"updated_at": "2017-07-26 22:45:57"
}
]
}
]
}
Any help is highly appreciated. Let me know if things are not clear. Thank you.
instead of 'with' replace it with this:
$msgs=Conversation::where('sender', $sender)
->orWhere('reciever', $sender)
->orderBy('created_at', 'desc')
->with(['message' => function ($query) { return $query->first();}])
->get();
this will return only one "message" instead of a collection of messages.
you can inside the callback function apply whatever whether it is (pluck, select,take...etc)