How to query without using Eloquent? - laravel

I have these few tables:
issues
1. id
2. name
tags
1. id
2. name
issue_tag
1. issue_id
2. tag_id
images
1. id
2. url
3. issue_id
The relationship of these tables is stated below:
Issue hasMany Images, Images belongsTo Issue
Issues belongsToMany Tags, Tags belongsToMany Issues
How can I retrieve all the records by NOT USING ELOQUENT, just by using the QUERY BUILDER. I would like to retrieve the data in the format like:
[
{
id: issue_id,
name: issue_name,
tags: [
{
id: tag_id_1,
name: tag_name_1
},
{
id: tag_id_2,
name: tag_name_2
}
]
},
{
...
},
...
]
Please someone help me because I could not solve this problem for a long time, I can only solve it by using Eloquent. But using Eloquent is not a solution for me.

You can use join query. For Details follow this link demo
Or
Use DB:: statement ('your raw query here'). Hope this helps.

Use can Laravel DB::select(); function
With the use of select, we can pass Raw SQL query into select
$innerData = [];
$issueAll = DB::select('select i.id,i.name,i2.url as image,t.id as tag_id,
t.name as tag_name,it.issue_id from issues as i
inner join issue_tag as it on it.issue_id=i.id
inner join tags as t on it.tag_id = t.id
inner join images as i2 on i.id = i2.issue_id
');
foreach ($issueAll as $issue) {
$innerData[$issue->id]["id"] = $issue->id;
$innerData[$issue->id]["name"] = $issue->name;
if (isset($innerData[$issue->id]['tags'][$issue->issue_id])) {
array_push($innerData[$issue->id]['tags'], ['id'=>$issue->tag_id, 'name'=>$issue->tag_name]);
} else {
$innerData[$issue->id]['tags'][$issue->issue_id] = ['id'=>$issue->tag_id, 'name'=>$issue->tag_name];
}
}
return $innerData;
The response you get
{
"1":{
"id":1,
"name":"issue1",
"tags":{
"1":{
"id":1,
"name":"tag1"
},
"2":{
"id":1,
"name":"tag1"
}
}
},
"2":{
"id":2,
"name":"issue2",
"tags":{
"2":{
"id":1,
"name":"tag1"
}
}
}
}
Hope it helps.

In laravel if you don't want to use eloquent then you can use with DB::statement('your query') and don't forget to use USE DB; at the top of the file

Related

Merge Model and its Relationship with result like join query in laravel

I'm new at Laravel and Programming at that. I have a problem joining model with its relationship, here is what my model:
class MainClass extends Model
{
public function first()
{
return $this->hasMany(First::class);
}
public function second()
{
return $this->hasMany(Second::class);
}
public function third()
{
return $this->hasMany(Third::class);
}
}
When i try to get MainClass records then load it's relationship like:
$main = Main::where('status', 'ready')->get()
$main->load(['first','second'])
Here's what i got:
[{
"id":"1",
"name":"First Person",
"status": "ready",
"first":[
{"main_id": "1", "prop":"One"},
{"main_id":"1", "prop":"Two"}],
"second":[
{"main_id": "1", "other":"Yes"},
{"main_id":"1", "other":"Two"},
{"main_id":"1", "other":"Three"}]
},{
"id":"5",
"name":"Fifth Person",
"status": "ready",
"first":[
{"main_id": "5", "prop":"Five"},
{"main_id":"5", "prop":"Six"}],
"second":[
{"main_id": "5", "other":"Laptop"},
{"main_id":"5", "other":"Pc"}]
}]
How can i merge that relationship so the result will be like join query,
this is what i want:
[{
"id":"1",
"name":"First Person",
"status": "ready",
"prop":"One",
"other:"Yes"
},{
"id":"1",
"name":"First Person",
"status": "ready",
"prop":"Two",
"other":"Two"
}]
I know there is a way to combine collection with merge or push in laravel, but i can't seem to get it right.
As of why not using join query, because i want to load relationship dynamically, so relation is not always loaded, but sometime they do. While join query, i have to write it manually (as far as i know) :-)
Maybe someone can point me somewhere, or maybe there is a package for something like this?
Thanks in advance
Use eager loading, fetching the collection, then run groupBy, then the each, then the map function to return the results formatted as your wish.
Why not just simply run the joins
You can do it with join;
$main = Main::query()->select(['main.id', 'main.name', 'main.status', 'f.prop', 's.other', 't.blabla'])
->leftJoin('first as f', 'f.main_id', 'main.id')
->leftJoin('second as s', 's.main_id', 'main.id')
->leftJoin('third as t', 't.main_id', 'main.id')
->where('main.status', 'ready')
->get();

GraphQL Filter on Enum Column

Below is my GraphQL Query to Fetch Posts from Strapi backend.
Please note I am running this on my Nuxt app.
Now I want to bring only those posts which have post_status = "Publish"
post_status is a ENUM field with two option as Draft and Publish
query GetPosts{
posts {
id
post_title
post_excerpt
post_featured_image{url}
post_content
post_category{category_name}
postingredients{ingredient{ingredient_name}, ingredient_unit}
updated_at
post_author{username}
post_slug
}
}
I did not understand how can I get
How to bring post_status values on my original Query
How to filter on the post_status where I can get only Published posts.
query GetStatusEnum{
__type(name: "ENUM_POST_POST_STATUS") {
name
enumValues {
name
} } }
Result of the above:
{
"data": {
"__type": {
"name": "ENUM_POST_POST_STATUS",
"enumValues": [
{
"name": "Publish"
},
{
"name": "Draft"
}
]
}
}
}
To add your post_status in your original request you just have to add it in the list of the attributes you want to fetch.
{
posts {
id
post_title
post_status <- here /!\
}
}
Here is the query to fetch Posts that have Publish as post_status
{
posts(where: { post_status: "Publish" }) {
id
post_title,
post_status
}
}
You can play with GraphQL playground in your strapi application:
http://localhost:1337/graphql
You will see in the right of you page a docs button that will show you all the information you need to create your GraphQL request.
I had a similar scenario (though I'm using a Prisma layer as well so keep that in mind) and i'm not sure that you can filter for enum values on the call but you can filter what it returns.
const posts = [the array of all posts]
const isPublished = (post) => {
if (post.post_status.includes('Publish')) {
return post;
}
}
let publishedPosts = posts.filter(isPublished);
return publishedPosts;

Laravel Eloquent getting value from previous relation/pivot

I have a bunch of results coming from eager loading through many to many relations. I need to filter one thing, I have this output :
"id_stats":32,
"n_stats":"Mastery",
"image_stats":null,
"pivot":{
"id_equipement":11,
"id_stats":32,
"weight":"235",
"rand-elem":"3"
},
"elements":[
{
"id_elements":2,
"n_elements":"Feu",
"pivot":{
"id_stats":32,
"id_elements":2,
"id_equipement":15
}
},
{
"id_elements":3,
"n_elements":"Eau",
"pivot":{
"id_stats":32,
"id_elements":3,
"id_equipement":13
}
}
]
I must filter the equipement on pivot(parent).id_equipement = elements.id_equipement otherwise I will have the result of the whole table. I can either filter my result like this. I already tried to add an inner join in my belongsToMany relation but it didn't help!
Here is parts of my code:
EncyclopedieModel.php
public function stats(){
return $this->belongsToMany(StatsModel::class, 'equipement_stats', 'id_equipement','id_stats')
->withPivot('weight')->withPivot('random');
}
StatsModel.php
public function elements(){
return $this->belongsToMany(ElementModel::class, 'stuff_equipement_stats_elements', 'id_stats', 'id_elements')
->withPivot('id_equipement');
}
Thanks!

where() and sort() not working for populated records in sails

I created a Sails application with two models Person and Department.
They are having a one-to-one relationship.Sails-mysql is the adapter using.When I am trying to populate department details along with Person using where() or sort() criteria the resulting records are not sorted or where() is not applied.
Person.js
attributes: {
firstname:{
alpha:true,
required:true
},
lastname:{
alpha:true,
required:true
},
age:{
numeric:true,
required:true
},
department:{
model:'Department'
}
}
Department.js
attributes: {
DepartmentName:{
required:true,
alpha:true
},
Description:{
required:true,
alpha:true
},
//relation
person:{
model:'Person'
}
}
PersonController.js
Person.find().populate('department').where({DepartmentName:{"startsWith":"hr"}}).sort('Description desc').exec(console.log);
is not working.
I tried where() and sort() in all the possible way like
var sort='DepartmentName desc';
and pass that variable in the populate() like one below:
Person.find().populate('department',sort).exec(console.log);
but that is also not working.In the same way I tried for where() also that is also a failure.
Help me in this.
For me this is working ;)
Person.find().populate('department', {
where: {
DepartmentName: {
'startsWith': 'hr'
}
},
sort: 'DepartmentName desc'}
).exec(......);
For more information check here: http://sailsjs.org/documentation/reference/waterline-orm/queries/populate
under section Populating a collection association

How to take an array of docs as condition to query then join with other table?

I have three tables hold these docs:
//user----tables:users
{
Id:"user/001"
Name:"Jack Losen"
....
}
//schooling---table:schoolings
{
Id:"schooling/001"
Name:""
Major:"",
EnrollYear:1983
}
//user_schooling-----join table:user_schooling
{
user_id:"user/001"
schooling_id:"schooling/001"
}
How to take an array of schoolings as condition to query user ids by join table:user_schooling?
for example:if I want to get all ids of the users who have been in A school at B year,or(not and) been in C school at D year...(or more),then my query condition may like this:
condition: [
{
Name:"A"
....
EnrollYear:B
},
{
Name:"C"
...
EnrollYear:D
}
]
Guys in rethinkdb mail list gave right answer:
r.table('user_schooling').filter(function(row) {
return r.table('schooling').get(row('SchoolingId')).do(function(school) {
return r.expr([{Name:"MASTER", EnrollTime:2003}]).pluck('Name', '
EnrollTime').contains(school.pluck('Name', 'EnrollTime'));
});
}).pluck("UserId")

Resources