Order list sorting according to the annual income only when both cgpa were same - mysql

I got a collection but I want to sort that collection. If the two application's cgpa is equal then sort the list collection according to the annual_salary where lower will up at list. Default the collection sort with the cgpa. below is my server response.
Thanks in advance
"applications": [
{
"id": 1,
"name": "Dr. W Khan",
"annual_salary": 5000,
"created_at": "2022-11-07T19:16:01.000000Z",
"updated_at": "2022-11-07T19:16:01.000000Z",
"education": [
{
"id": 1,
"application_id": 1,
"name": "HSC",
"cgpa": 400,
"created_at": "2022-11-07T19:16:01.000000Z",
"updated_at": "2022-11-07T19:16:01.000000Z"
}
]
},
{
"id": 2,
"name": "Dr. M Khan",
"annual_salary": 7000,
"created_at": "2022-11-07T19:16:14.000000Z",
"updated_at": "2022-11-07T19:16:14.000000Z",
"education": [
{
"id": 2,
"application_id": 2,
"name": "HSC",
"cgpa": 350,
"created_at": "2022-11-07T19:16:14.000000Z",
"updated_at": "2022-11-07T19:16:14.000000Z"
}
]
},
{
"id": 3,
"name": "Dr.",
"annual_salary": 5000,
"created_at": "2022-11-07T19:16:28.000000Z",
"updated_at": "2022-11-07T19:16:28.000000Z",
"education": [
{
"id": 3,
"application_id": 3,
"name": "HSC",
"cgpa": 350,
"created_at": "2022-11-07T19:16:28.000000Z",
"updated_at": "2022-11-07T19:16:28.000000Z"
}
]
}
]
This is the query for me.
$application = Application::with(
[
'education' => function ($query) {
$query->orderBy('cgpa', 'desc');
},
]
)
->orderBy('annual_salary', 'asc')
->get();
But ordering the list followed by a second orderBy.

Related

Impossible to get datas from Json Flutter

In my Flutter project i would like to get datas from an API.
I used QuickType to generate my classes to parse my Json.
This error is always occured:
_TypeError (type 'List' is not a subtype of type 'String')
This my API response:
[
{
"id": 1,
"label": "Animals",
"created_at": "2022-05-25T13:36:42.517Z",
"updated_at": "2022-05-25T13:36:42.517Z",
"sub_categories": [
{
"id": 2,
"label": "Ant",
"category": 1,
"published_at": "2022-05-25T13:40:48.172Z",
"created_at": "2022-05-25T13:40:48.174Z",
"updated_at": "2022-05-25T16:29:58.047Z"
},
{
"id": 3,
"label": "Cats",
"category": 1,
"published_at": "2022-05-25T13:42:07.022Z",
"created_at": "2022-05-25T13:42:07.023Z",
"updated_at": "2022-05-25T16:30:08.576Z"
},
{
"id": 4,
"label": "Dogs",
"category": 1,
"published_at": "2022-05-25T13:42:12.270Z",
"created_at": "2022-05-25T13:42:12.271Z",
"updated_at": "2022-05-25T16:30:14.262Z"
}
]
},
{
"id": 2,
"label": "Art",
"created_at": "2022-05-25T13:36:45.994Z",
"updated_at": "2022-05-25T13:36:45.994Z",
"sub_categories": [
{
"id": 7,
"label": "Paint",
"category": 2,
"published_at": "2022-05-28T08:01:46.941Z",
"created_at": "2022-05-28T08:01:46.943Z",
"updated_at": "2022-05-28T08:01:46.948Z"
}
]
},
{
"id": 3,
"label": "Astrology",
"created_at": "2022-05-25T13:36:49.498Z",
"updated_at": "2022-05-25T13:36:49.498Z",
"sub_categories": [
{
"id": 8,
"label": "Taurus",
"category": 3,
"published_at": "2022-05-28T08:02:07.520Z",
"created_at": "2022-05-28T08:02:07.521Z",
"updated_at": "2022-05-28T08:02:07.526Z"
}
]
},
{
"id": 4,
"label": "Astronomy",
"created_at": "2022-05-25T13:36:54.035Z",
"updated_at": "2022-05-25T13:36:54.035Z",
"sub_categories": [
{
"id": 9,
"label": "Hubble",
"category": 4,
"published_at": "2022-05-28T08:02:14.747Z",
"created_at": "2022-05-28T08:02:14.748Z",
"updated_at": "2022-05-28T08:02:14.753Z"
}
]
},
{
"id": 5,
"label": "Cook",
"created_at": "2022-05-25T13:37:34.930Z",
"updated_at": "2022-05-25T13:37:34.930Z",
"sub_categories": [
{
"id": 5,
"label": "Plancha",
"category": 5,
"published_at": "2022-05-25T13:42:26.369Z",
"created_at": "2022-05-25T13:42:26.370Z",
"updated_at": "2022-05-25T16:30:23.512Z"
},
{
"id": 6,
"label": "Bakery",
"category": 5,
"published_at": "2022-05-25T16:31:49.887Z",
"created_at": "2022-05-25T16:31:49.890Z",
"updated_at": "2022-05-25T16:31:49.900Z"
}
]
},
{
"id": 6,
"label": "Crypto Currencies",
"created_at": "2022-05-25T13:37:43.113Z",
"updated_at": "2022-05-25T13:37:43.113Z",
"sub_categories": [
{
"id": 10,
"label": "Bitcoin",
"category": 6,
"published_at": "2022-05-28T08:02:52.814Z",
"created_at": "2022-05-28T08:02:52.815Z",
"updated_at": "2022-05-28T08:02:52.821Z"
}
]
},
{
"id": 7,
"label": "DIY",
"created_at": "2022-05-25T13:37:48.652Z",
"updated_at": "2022-05-25T13:37:48.652Z",
"sub_categories": [
{
"id": 11,
"label": "Tools",
"category": 7,
"published_at": "2022-05-28T08:03:05.030Z",
"created_at": "2022-05-28T08:03:05.031Z",
"updated_at": "2022-05-28T08:03:05.038Z"
}
]
},
{
"id": 8,
"label": "Ecology",
"created_at": "2022-05-25T13:37:53.776Z",
"updated_at": "2022-05-25T13:37:53.776Z",
"sub_categories": [
{
"id": 12,
"label": "Planet",
"category": 8,
"published_at": "2022-05-28T08:03:19.915Z",
"created_at": "2022-05-28T08:03:19.916Z",
"updated_at": "2022-05-28T08:03:19.922Z"
}
]
},
{
"id": 9,
"label": "Economy",
"created_at": "2022-05-25T13:37:59.384Z",
"updated_at": "2022-05-25T13:37:59.384Z",
"sub_categories": [
{
"id": 13,
"label": "Wall Street",
"category": 9,
"published_at": "2022-05-28T08:03:41.942Z",
"created_at": "2022-05-28T08:03:41.943Z",
"updated_at": "2022-05-28T08:03:41.948Z"
}
]
},
{
"id": 10,
"label": "Literature",
"created_at": "2022-05-25T13:38:07.354Z",
"updated_at": "2022-05-25T13:38:07.354Z",
"sub_categories": [
{
"id": 14,
"label": "Zola",
"category": 10,
"published_at": "2022-05-28T08:03:59.314Z",
"created_at": "2022-05-28T08:03:59.316Z",
"updated_at": "2022-05-28T08:03:59.322Z"
}
]
},
{
"id": 11,
"label": "Mode",
"created_at": "2022-05-25T13:38:32.915Z",
"updated_at": "2022-05-25T13:38:32.915Z",
"sub_categories": [
{
"id": 15,
"label": "Squirt",
"category": 11,
"published_at": "2022-05-28T08:04:10.245Z",
"created_at": "2022-05-28T08:04:10.246Z",
"updated_at": "2022-05-28T08:08:02.312Z"
}
]
},
{
"id": 12,
"label": "Movies",
"created_at": "2022-05-25T13:38:36.502Z",
"updated_at": "2022-05-25T13:38:36.502Z",
"sub_categories": [
{
"id": 16,
"label": "Tarantino",
"category": 12,
"published_at": "2022-05-28T08:04:48.519Z",
"created_at": "2022-05-28T08:04:48.520Z",
"updated_at": "2022-05-28T08:04:48.526Z"
}
]
},
{
"id": 13,
"label": "Music",
"created_at": "2022-05-25T13:38:41.227Z",
"updated_at": "2022-05-25T13:38:41.227Z",
"sub_categories": [
{
"id": 17,
"label": "Rock",
"category": 13,
"published_at": "2022-05-28T08:05:20.575Z",
"created_at": "2022-05-28T08:05:20.576Z",
"updated_at": "2022-05-28T08:05:20.580Z"
}
]
},
{
"id": 14,
"label": "Nature",
"created_at": "2022-05-25T13:38:46.772Z",
"updated_at": "2022-05-25T13:38:46.772Z",
"sub_categories": [
{
"id": 18,
"label": "Forest",
"category": 14,
"published_at": "2022-05-28T08:05:32.566Z",
"created_at": "2022-05-28T08:05:32.567Z",
"updated_at": "2022-05-28T08:05:32.572Z"
}
]
},
{
"id": 15,
"label": "Politic",
"created_at": "2022-05-25T13:38:52.016Z",
"updated_at": "2022-05-25T13:38:52.016Z",
"sub_categories": [
{
"id": 19,
"label": "Vote",
"category": 15,
"published_at": "2022-05-28T08:05:38.045Z",
"created_at": "2022-05-28T08:05:38.046Z",
"updated_at": "2022-05-28T08:05:38.052Z"
}
]
},
{
"id": 16,
"label": "Sciences",
"created_at": "2022-05-25T13:38:58.047Z",
"updated_at": "2022-05-25T13:38:58.047Z",
"sub_categories": [
{
"id": 20,
"label": "Chemistry",
"category": 16,
"published_at": "2022-05-28T08:05:51.520Z",
"created_at": "2022-05-28T08:05:51.521Z",
"updated_at": "2022-05-28T08:05:51.528Z"
},
{
"id": 21,
"label": "Chemistery",
"category": 16,
"published_at": "2022-05-28T08:05:54.625Z",
"created_at": "2022-05-28T08:05:54.627Z",
"updated_at": "2022-05-28T08:05:54.631Z"
}
]
},
{
"id": 17,
"label": "Series",
"created_at": "2022-05-25T13:39:02.864Z",
"updated_at": "2022-05-25T13:39:02.864Z",
"sub_categories": [
{
"id": 22,
"label": "GOT",
"category": 17,
"published_at": "2022-05-28T08:06:06.350Z",
"created_at": "2022-05-28T08:06:06.351Z",
"updated_at": "2022-05-28T08:06:06.355Z"
}
]
},
{
"id": 18,
"label": "Sexuality",
"created_at": "2022-05-25T13:39:10.901Z",
"updated_at": "2022-05-25T13:39:10.901Z",
"sub_categories": [
{
"id": 23,
"label": "Couple",
"category": 18,
"published_at": "2022-05-28T08:06:33.963Z",
"created_at": "2022-05-28T08:06:33.964Z",
"updated_at": "2022-05-28T08:06:33.968Z"
}
]
},
{
"id": 19,
"label": "Social Life",
"created_at": "2022-05-25T13:39:16.760Z",
"updated_at": "2022-05-25T13:39:16.760Z",
"sub_categories": [
{
"id": 24,
"label": "Interractions",
"category": 19,
"published_at": "2022-05-28T08:06:55.099Z",
"created_at": "2022-05-28T08:06:55.099Z",
"updated_at": "2022-05-28T08:06:55.104Z"
}
]
},
{
"id": 20,
"label": "Sport",
"created_at": "2022-05-25T13:39:21.296Z",
"updated_at": "2022-05-25T13:39:21.296Z",
"sub_categories": [
{
"id": 25,
"label": "Football",
"category": 20,
"published_at": "2022-05-28T08:07:13.972Z",
"created_at": "2022-05-28T08:07:13.973Z",
"updated_at": "2022-05-28T08:07:13.979Z"
}
]
},
{
"id": 21,
"label": "Theater",
"created_at": "2022-05-25T13:39:26.063Z",
"updated_at": "2022-05-25T13:39:26.063Z",
"sub_categories": [
{
"id": 26,
"label": "Molière",
"category": 21,
"published_at": "2022-05-28T08:07:22.217Z",
"created_at": "2022-05-28T08:07:22.219Z",
"updated_at": "2022-05-28T08:07:22.224Z"
}
]
},
{
"id": 22,
"label": "Vehicules",
"created_at": "2022-05-25T13:39:30.859Z",
"updated_at": "2022-05-25T13:39:30.859Z",
"sub_categories": [
{
"id": 27,
"label": "Audi",
"category": 22,
"published_at": "2022-05-28T08:07:30.514Z",
"created_at": "2022-05-28T08:07:30.515Z",
"updated_at": "2022-05-28T08:07:30.522Z"
}
]
},
{
"id": 23,
"label": "Video Games",
"created_at": "2022-05-25T13:39:36.926Z",
"updated_at": "2022-05-25T13:39:36.926Z",
"sub_categories": [
{
"id": 28,
"label": "Elden Ring",
"category": 23,
"published_at": "2022-05-28T08:07:38.335Z",
"created_at": "2022-05-28T08:07:38.339Z",
"updated_at": "2022-05-28T08:07:38.343Z"
}
]
}
]
My classes generated by QuickType:
class Category {
Category({
required this.id,
required this.label,
required this.createdAt,
required this.updatedAt,
required this.subCategories,
});
int id;
String label;
DateTime createdAt;
DateTime updatedAt;
List<SubCategory> subCategories;
factory Category.fromRawJson(String str) =>
Category.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Category.fromJson(Map<String, dynamic> json) => Category(
id: json["id"],
label: json["label"],
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
subCategories: List<SubCategory>.from(
json["sub_categories"].map((x) => SubCategory.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"label": label,
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
"sub_categories":
List<dynamic>.from(subCategories.map((x) => x.toJson())),
};
}
class SubCategory {
SubCategory({
required this.id,
required this.label,
required this.category,
required this.publishedAt,
required this.createdAt,
required this.updatedAt,
});
int id;
String label;
int category;
DateTime publishedAt;
DateTime createdAt;
DateTime updatedAt;
factory SubCategory.fromRawJson(String str) =>
SubCategory.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory SubCategory.fromJson(Map<String, dynamic> json) => SubCategory(
id: json["id"],
label: json["label"],
category: json["category"],
publishedAt: DateTime.parse(json["published_at"]),
createdAt: DateTime.parse(json["created_at"]),
updatedAt: DateTime.parse(json["updated_at"]),
);
Map<String, dynamic> toJson() => {
"id": id,
"label": label,
"category": category,
"published_at": publishedAt.toIso8601String(),
"created_at": createdAt.toIso8601String(),
"updated_at": updatedAt.toIso8601String(),
};
}
And this is my code:
List<Category> categoryFromJson(String str) =>
List<Category>.from(json.decode(str).map((x) => Category.fromJson(x)));
String categoryToJson(List<Category> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
Future<List<Category>> getCategories() async {
var response =
await http.get(Uri.parse("http://192.168.0.10:1337/categories/"));
if (response.statusCode == 200) {}
var decodedData = jsonDecode(response.body);
final category = categoryFromJson(decodedData);
print(category);
return category;
}
Thank you for your awnsers :)
API providing list of data, try using
Future<List<Category>?> loadData() async {
//use http insted of `rootBundle`
final response = await rootBundle.loadString("assets/j.json");
var decodedData = jsonDecode(response) as List;
final items = decodedData.map((e) => Category.fromJson(e)).toList();
return items;
}

Laravel: How to group by users conversation correctly

I have like this database scheme:
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained()
->onDelete('cascade');
$table->unsignedBigInteger('sender_id');
$table->foreign('sender_id')->references('id')->on('users');
$table->text('message');
$table->foreignId('message_id')
->nullable()
->constrained()
->onDelete('cascade');
$table->tinyInteger('status')->default(0);
$table->tinyInteger('view_status')->default(0);
$table->timestamps();
});
I tried get two user conversation like this:
$user = auth()->user();
$conversations = Message::where([
['user_id', $user->id],
])->orWhere([
['sender_id', $user->id],
])->groupBy('sender_id', 'user_id')->latest()->get();
But result is wrong and not grouping users conversation.
Here is simple data:
My query result currently:
[
{
"id": 3,
"user_id": 1,
"sender_id": 2,
"message": "How are you? 😀",
"message_id": null,
"status": 0,
"view_status": 0,
"created_at": "2021-08-17T08:18:03.000000Z",
"updated_at": "2021-08-17T08:18:03.000000Z",
"user": {
"id": 1,
"name": "John Doe"
},
"sender": {
"id": 2,
"name": "Nicolas Smith"
}
},
{
"id": 2,
"user_id": 2,
"sender_id": 1,
"message": "Hello",
"message_id": null,
"status": 0,
"view_status": 1,
"created_at": "2021-08-17T07:25:18.000000Z",
"updated_at": "2021-08-17T08:19:09.000000Z",
"user": {
"id": 2,
"name": "Nicolas Smith"
},
"sender": {
"id": 1,
"name": "John Doe"
}
},
{
"id": 1,
"user_id": 3,
"sender_id": 2,
"message": "Hi",
"message_id": null,
"status": 0,
"view_status": 0,
"created_at": "2021-08-16T23:44:17.000000Z",
"updated_at": "2021-08-16T23:44:17.000000Z",
"user": {
"id": 3,
"name": "Richard"
},
"sender": {
"id": 2,
"name": "Nicolas Smith"
}
}
]
How I can group by correctly?

N1ql SUM on UNNEST Array

N1ql SUM on UNNEST Array
I have a single bucket (Couchbase Community edition 6.5) consisting of the following documents:
FishingDoc
{
"boatIds": ["boatId_1","boatId_2","boatId_3"],
"areaIds": ["areaId_1","areaId_2","areaId_3"],
"total": 10,
"date": "2021-05-13T00:00:00Z",
"type": "fishing"
},
{
"boatIds": ["boatId_1","boatId_3"],
"areaIds": ["areaId_2","areaId_3"],
"total": 25,
"date": "2021-05-15T00:00:00Z",
"type": "fishing"
}
RiverDoc
{
"_id": "areaId_1",
"size": 5,
"type": "river"
},
{
"_id": "areaId_1",
"size": 10,
"type": "river"
},
{
"_id": "areaId_1",
"size": 15,
"type": "river"
}
BoatDoc
{
"_id": "areaId_1",
"name": "Small Boat",
"type": "boat"
},
{
"_id": "areaId_1",
"name": "Medium Boat",
"type": "boat"
},
{
"_id": "areaId_1",
"name": "Large Boat",
"type": "boat"
}
I need a query where I can get all of the fishing docs broken up per river and per boat. I got this working using the UNNEST operator in the following query:
SELECT river.size,
boat.name,
fishing.total
FROM bucket_name fishing
UNNEST fishing.riverIds AS river
UNNEST fishing.boatIds AS boat
WHERE fishing.type = "fishing"
But the problem in this query is that the total value in the above query is the total for the entire fishing object.
I need to get the total, relative to the size of the unnested river. So I need to join in and sum the total of all the rivers for the fishing object and get the specific river's size relative to the total.
Here is the select statement I have in mind but I have no idea on how to actually write the correct query:
SELECT river.size,
boat.name,
river.size/SUM( fishing.riverIds[0].size, fishing.riverIds[1].size, fishing.riverIds[2].size ) * fishing.total
FROM bucket_name fishing
UNNEST fishing.riverIds AS river
UNNEST fishing.boatIds AS boat
WHERE fishing.type = "fishing"
INSERT INTO default VALUES ("f01", { "boatIds": ["boatId_1","boatId_2","boatId_3"], "areaIds": ["areaId_1","areaId_2","areaId_3"], "total": 10, "date": "2021-05-13T00:00:00Z", "type": "fishing" });
INSERT INTO default VALUES ("f02", { "boatIds": ["boatId_1","boatId_3"], "areaIds": ["areaId_2","areaId_3"], "total": 25, "date": "2021-05-15T00:00:00Z", "type": "fishing" });
INSERT INTO default VALUES ("areaId_1", { "_id": "areaId_1", "size": 5, "type": "river" });
INSERT INTO default VALUES ("areaId_2", { "_id": "areaId_2", "size": 10, "type": "river" });
INSERT INTO default VALUES ("areaId_3", { "_id": "areaId_3", "size": 15, "type": "river" });
INSERT INTO default VALUES ("boatId_1", { "_id": "boatId_1", "name": "Small Boat", "type": "boat" });
INSERT INTO default VALUES ("boatId_2", { "_id": "boatId_2", "name": "Medium Boat", "type": "boat" });
INSERT INTO default VALUES ("boatId_3", { "_id": "boatId_3", "name": "Large Boat", "type": "boat" });
SELECT ARRAY {"size": f.river.[v], "name": f.boat.[f.boatIds[pos]], "total": f.total*f.river.[v]/ARRAY_SUM(OBJECT_VALUES(f.river))}
FOR pos:v IN f.areaIds END AS distribution
FROM (SELECT d.*,
OBJECT v._id:v.size FOR v IN (SELECT r._id, r.size FROM default AS r USE KEYS d.areaIds) END AS river,
OBJECT v._id:v.name FOR v IN (SELECT b._id, b.name FROM default AS b USE KEYS d.boatIds) END AS boat
FROM default AS d
WHERE d.type = "fishing") AS f;
{
"requestID": "fbe127b4-2ebb-4b01-a8a1-0bfe5310ed42",
"signature": {
"distribution": "array"
},
"results": [
{
"distribution": [
{
"name": "Small Boat",
"size": 5,
"total": 1.6666666666666667
},
{
"name": "Medium Boat",
"size": 10,
"total": 3.3333333333333335
},
{
"name": "Large Boat",
"size": 15,
"total": 5
}
]
},
{
"distribution": [
{
"name": "Small Boat",
"size": 10,
"total": 10
},
{
"name": "Large Boat",
"size": 15,
"total": 15
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "6.946602ms",
"executionTime": "6.881065ms",
"resultCount": 2,
"resultSize": 730,
"serviceLoad": 2
}
}
OR
SELECT river AS SectionSize,
f.total*(river/ARRAY_SUM(f.sections)) AS total,
f.boats[UNNEST_POS(river)] AS name
FROM (SELECT d.total,
(SELECT RAW r.size FROM default AS r USE KEYS d.areaIds) AS sections,
(SELECT RAW b.name FROM default AS b USE KEYS d.boatIds) AS boats
FROM default AS d
WHERE d.type = "fishing") AS f
UNNEST f.sections AS river;
{
"results": [
{
"SectionSize": 5,
"name": "Small Boat",
"total": 1.6666666666666665
},
{
"SectionSize": 10,
"name": "Medium Boat",
"total": 3.333333333333333
},
{
"SectionSize": 15,
"name": "Large Boat",
"total": 5
},
{
"SectionSize": 10,
"name": "Small Boat",
"total": 10
},
{
"SectionSize": 15,
"name": "Large Boat",
"total": 15
}
]
}

unable to get desired json format while creating laravel 5 api

i have the following Template model and TestCategory hasMany Test:
class Template extends Model
{
protected $table = 'test_templates';
protected $fillable = [
'customers_id',
'tests_categories_id',
'tests_id'
];
public function customers(){
return $this->belongsTo(Customer::class, 'customers_id');
}
public function testCategories(){
return $this->belongsTo(TestCategory::class, 'tests_categories_id');
}
public function tests(){
return $this->belongsTo(Test::class, 'tests_id');
}
}
And I have the following index method defined in TemplateController
public function index($customerId)
{
$templates = Template::with( 'customers','testCategories','tests')
->where('customers_id', $customerId)->get();
$templates = $templates->groupBy('tests_categories_id');
$templates = json_decode($templates, true);
$templates = array_values($templates);
return response()->json(['Status' => True, 'template' => $templates]);
}
I got the following json output:
{
"Status": true,
"template": [
[
{
"id": 1,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 1,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "9842********",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 1,
"tests_category_id": 1,
"name": "Lamination",
"created_at": null,
"updated_at": null
}
},
{
"id": 2,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 2,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984*******",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 2,
"tests_category_id": 1,
"name": "Paper Type",
"created_at": null,
"updated_at": null
}
}
],
[
{
"id": 7,
"customers_id": 1,
"tests_categories_id": 2,
"tests_id": 8,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984******",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 2,
"name": "Scratch Test",
"created_at": null,
"updated_at": null
},
"tests": {
"id": 8,
"tests_category_id": 2,
"name": "HRN Visibility",
"created_at": null,
"updated_at": null
}
}
]
]
}
I want to collect all the tests as per the test_categories without reprinting customers and test_categories as following:
{
"Status": true,
"template": [
[
{
"id": 1,
"customers_id": 1,
"tests_categories_id": 1,
"tests_id": 1,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984*****",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 1,
"name": "General Tests",
"created_at": null,
"updated_at": null
},
"tests": {
{
"id": 1,
"tests_category_id": 1,
"name": "Lamination",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"tests_category_id": 1,
"name": "Paper Type",
"created_at": null,
"updated_at": null
}
}
}
],
[
{
"id": 7,
"customers_id": 1,
"tests_categories_id": 2,
"tests_id": 8,
"created_at": "2018-09-10 11:03:44",
"updated_at": "2018-09-10 11:03:44",
"customers": {
"id": 1,
"name": "Queen PVT.Ltd",
"address": "gotham",
"contact_number": "984265*****",
"created_at": "2018-09-10 11:03:43",
"updated_at": "2018-09-10 11:03:43"
},
"test_categories": {
"id": 2,
"name": "Scratch Test",
"created_at": null,
"updated_at": null
},
"tests": {
{
"id": 8,
"tests_category_id": 2,
"name": "HRN Visibility",
"created_at": null,
"updated_at": null
}
}
}
]
]
}
If I understand the problem correctly the tests are being separated into different templates when you are expecting some of them to be related to the same template.
This is because your Template class is storing the test_id that it belongs to and it can only store 1 test per Template. So when you are calling Template::with('tests') it can only return one Test::class.
Because you expect to see that many Tests belongTo one Template, and when you look at the relationship from the Template to Test, you expect that a Template has many Tests. Therefore, your Template class should implement the hasMany relationship like so:
class Template extends Model
{
protected $fillable = [
'customers_id',
'tests_categories_id' // notice the removal of the test_id which should also be removed from the test_templates migration
];
...
public function tests(){
return $this->hasMany(Test::class); //change belongsTo to hasMany
}
...
}
Instead of storing the test_id on the Template class you should be storing the template_id on your Test class and look something like so:
class Test extends Model
{
protected $fillable = [
...
'test_template_id' // This should be added to $fillable and the tests migration
...
];
...
public function template(){
return $this->belongsTo(Template::class, test_template_id);
}
...
}
Now when you call Template::with('tests') it can return many Test::class and should match your expected output.

lumen order by with two level relation

this is a task manager web app
i have 3 tables rows, tasks and inputs
the inputs table has order column to choice which column to be shown first
row has many tasks
input has many tasks
so every task has an input and a row
i want to sort the tasks by row_id and then by input.order
first try
App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
->orderBy('rows.id', 'ASC', 'order', 'ASC')
->get();
it ignores the order
result
[
{
"id": 1,
"user_id": 1,
"created_at": "2018-06-07 18:40:23",
"updated_at": "2018-06-07 18:40:23",
"tasks": [
{
"id": 1,
"row_id": 1,
"input_id": 1,
"option_id": 1,
"value": null,
"input": {
"id": 1,
"name": "assigned to",
"type": 1,
"value": "mario",
"required": 1,
"order": 2,
"user_id": 1,
"created_at": "2018-06-07 18:40:16",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 2,
"row_id": 1,
"input_id": 2,
"option_id": null,
"value": "test new option",
"input": {
"id": 2,
"name": "test new option",
"type": 0,
"value": "test new option",
"required": 0,
"order": 3,
"user_id": 1,
"created_at": "2018-06-07 18:40:44",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 3,
"row_id": 1,
"input_id": 3,
"option_id": null,
"value": "2018-07-01",
"input": {
"id": 3,
"name": "deadline",
"type": 3,
"value": "2018-07-01",
"required": 0,
"order": 4,
"user_id": 1,
"created_at": "2018-06-08 10:07:37",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 4,
"row_id": 1,
"input_id": 4,
"option_id": null,
"value": "",
"input": {
"id": 4,
"name": "priority",
"type": 6,
"value": "",
"required": 0,
"order": 1,
"user_id": 1,
"created_at": "2018-06-08 10:08:19",
"updated_at": "2018-06-08 10:08:19"
}
}
]
}
]
second try
App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
->orderBy('rows.id', 'ASC')
->orderBy('order', 'ASC')
->get();
but it generated an error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order' in 'order clause' (SQL: select * from `rows` order by `rows`.`id` asc, `order` asc)
third try
App\Row::with([
'user',
'tasks',
'tasks.option',
'tasks.input' =>
function($query) {
$query->orderBy('order', 'ASC');
}
])
->orderBy('rows.id', 'ASC')
->get();
it ignores the order result same as first
forth try
return App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
->join('tasks', 'rows.id', '=', 'tasks.row_id')
->join('inputs', 'inputs.id', '=', 'tasks.input_id')
->orderBy('rows.id', 'ASC')
->orderBy( 'inputs.order', 'ASC')
->get();
it generate row for every column and not row as it suppose to
result
[
{
"id": 4,
"user_id": 1,
"created_at": "2018-06-08 10:08:19",
"updated_at": "2018-06-08 10:08:19",
"row_id": 1,
"input_id": 4,
"option_id": null,
"value": "",
"name": "priority",
"type": 6,
"required": 0,
"order": 1,
"tasks": []
},
{
"id": 1,
"user_id": 1,
"created_at": "2018-06-07 18:40:16",
"updated_at": "2018-06-08 10:08:19",
"row_id": 1,
"input_id": 1,
"option_id": 1,
"value": "mario",
"name": "assigned to",
"type": 1,
"required": 1,
"order": 2,
"tasks": [
{
"id": 1,
"row_id": 1,
"input_id": 1,
"option_id": 1,
"value": null,
"input": {
"id": 1,
"name": "assigned to",
"type": 1,
"value": "mario",
"required": 1,
"order": 2,
"user_id": 1,
"created_at": "2018-06-07 18:40:16",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 2,
"row_id": 1,
"input_id": 2,
"option_id": null,
"value": "test new option",
"input": {
"id": 2,
"name": "test new option",
"type": 0,
"value": "test new option",
"required": 0,
"order": 3,
"user_id": 1,
"created_at": "2018-06-07 18:40:44",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 3,
"row_id": 1,
"input_id": 3,
"option_id": null,
"value": "2018-07-01",
"input": {
"id": 3,
"name": "deadline",
"type": 3,
"value": "2018-07-01",
"required": 0,
"order": 4,
"user_id": 1,
"created_at": "2018-06-08 10:07:37",
"updated_at": "2018-06-08 10:08:19"
}
},
{
"id": 4,
"row_id": 1,
"input_id": 4,
"option_id": null,
"value": "",
"input": {
"id": 4,
"name": "priority",
"type": 6,
"value": "",
"required": 0,
"order": 1,
"user_id": 1,
"created_at": "2018-06-08 10:08:19",
"updated_at": "2018-06-08 10:08:19"
}
}
]
},
{
"id": 2,
"user_id": 1,
"created_at": "2018-06-07 18:40:44",
"updated_at": "2018-06-08 10:08:19",
"row_id": 1,
"input_id": 2,
"option_id": null,
"value": "test new option",
"name": "test new option",
"type": 0,
"required": 0,
"order": 3,
"tasks": []
},
{
"id": 3,
"user_id": 1,
"created_at": "2018-06-08 10:07:37",
"updated_at": "2018-06-08 10:08:19",
"row_id": 1,
"input_id": 3,
"option_id": null,
"value": "2018-07-01",
"name": "deadline",
"type": 3,
"required": 0,
"order": 4,
"tasks": []
}
]
this file is here
the full code is here
Because it searches columns in table rows not in input.
App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'=> function ($q){
$q->orderBy('order', 'ASC'); // order from "rows" table
}])
->orderBy('id', 'ASC') // id from "rows" table
->get();