Get data from tables with same id column - laravel

I have 3 tables:
features:
id | name
branch_features:
id | branch_id | feature_id
permissions:
id | name | feature_id
I'm trying to get the features that only exist in branch_features with permissions. So the end result should be something like that:
Feature 1
Permission 1
Permission 2
Feature 4
Permission 7
Permission 8
I tried hasManyThrough and hasMany. But no success:
return $this->hasManyThrough(Permission::class, BranchFeature::class, 'feature_id', 'feature_id');
Could it be done without joining the 3 tables like that?
return $this->join('branch_features AS bf', 'bf.feature_id', 'features.id')->join('permissions AS p', 'p.feature_id', 'features.id')
Because I want to sort each permission beneath its feature like that:
Feature X
Permission A
Permission B

Related

Gatsby queries error with multiple queries error [duplicate]

To create the post template through this tutorial(I'm in part 4):
https://www.joaopedro.cc/blog-com-gatsby-e-react-parte-4
But when doing the "PostPage" query an error is occurring:
Multiple "root" queries found: "PostPage" and "PostPage".
Only the first ("PostPage") will be registered.
Instead of:
1 | query PostPage {
2 | markdownRemark {
3 | #...
4 | }
5 | }
6 |
7 | query PostPage {
8 | markdownRemark {
9 | #...
10 | }
11 | }
Do:
1 | query postPageAndPostPage {
2 | markdownRemark {
3 | #...
4 | }
5 | markdownRemark {
6 | #...
7 | }
8 | }
I've looked at other answers to similar questions, and from what I understand it was a case sensitive issue, but from what I see, everything is looking correct.
I'm not getting out of place. :( thanks in advance!
The error message here is actually quite helpful, but it doesn't take it all the way.
The issue is that you can only have a single query and you have multiple. The solution is to query for all the data you need in a single query rather than splitting it across multiple, as the error suggests.
Alas, you're trying to fetch what I imagine are two distinct data sets through the same field, which will give you another error by default. But there's an easy solution here: use aliases to de-duplicate the field name in the result.
query PostPage {
firstPost: markdownRemark {
#...
}
secondPost: markdownRemark {
#...
}
}
I had the same error today on Gatsby. There was one page Query and one Static Query used so the message was a bit misleading.
"This can happen when you use two page/static queries in one file"
The way this was tackled was I traced down the files that were using it. One of them was present but notice it was NOT in any way active in the project. Once it was removed the alert disappeared. So, in a nutshell, even if the file is NOT used anywhere the second identical Static Query is picked up.
In my case it was about having identical names of two page queries in two different pages.
Here's my setup:
in the pages folder, I have two components - index.tsx and account.tsx
both these components have this page query:
export const query = graphql`
query myquery($pathname: String) {
myquery(page: { eq: $pathname }) {
id
title
page
description
}
}
`
I got the error mentioned in the question
So, it turned out two queries in two different files but with the same name is forbidden.
In the end, I just used two different query names in my two files - myquery and myotherquery. Voila!
I just ran into the same error message with a React component that only used one StaticQuery. This component was copied in from another project (where it is working without any issues).
I've looked at other answers to similar questions, and from what I understand it was a case-sensitive issue...
This helped me to solve it - turned out that both the name of the React component as well as the StaticQuery were not propper camelCased (they contained "SEO" - all capital letters).
Error message is gone after cleaning this part up. I thought I share this as I found the error message in this context confusing.
ERROR #85910 GRAPHQL
Multiple "root" queries found: "SEOQuery" and "SEOQuery".
Only the first ("SEOQuery") will be registered.
Instead of:
1 | query SEOQuery {
2 | site {
3 | #...
4 | }
5 | }
6 |
7 | query SEOQuery {
8 | site {
9 | #...
10 | }
11 | }
Do:
1 | query seoQueryAndSeoQuery {
2 | site {
3 | #...
4 | }
5 | site {
6 | #...
7 | }
8 | }

Laravel 5.7 Get data from Pivot Table

I'm trying to get data from my pivot table.
clients table:
---|------
id | name
---|------
1 | John
---|------
2 | Steve
orders table:
---|------
id | description
---|------
1 | Mac
---|------
2 | Keyboard
---|------
3 | Printer
client_order (pivot)table:
id | client_id | order_id
---|-----------|------
1 | 1 1
---|-----------|------
2 | 1 | 2
---|-----------|------
3 | 2 | 3
Client.php
public function orders()
{
return $this->belongsToMany('App\Order','client_order');
}
Order.php
public function clients()
{
return $this->belongsToMany('App\Client','client_order');
}
Now, how can I retrieve data from pivot table? For example:
John | Mac, Keyboard (2 orders)
Steve| Printer (1 orders)
Thank you.
For client:
$client = Client::find(1); //or anyway you create the client
$client->orders; //it gives you a collection that you can get data
in a foreach loop
//for example
foreach($client->orders as $order){
echo $order->description;
}
For order:
$order = Order::find(1); //or anyway you create order
$order->clients; //it gives you a collection too
//for example
foreach($order->clients as $client){
echo $client->name;
}
This is for your new comment. First you select your users and then in a loop you can get the orders:
$clients = Client::all();
foreach($clients as $client){
echo $client->name." | ";
foreach($client->orders as $order){
echo $order->description;
}
echo "(".count($client->orders)." orders)";
}
You can achieve that using the relations as #Rouhollah Mazarei said, but you can also use the own pivot table to retrieve this information:
$clientsOrders = DB::table('client_order')->where('client_id', $clientId)->count()->get();
This will return to you how many orders this client made, you just need to inform his id.

Datamapper unable to find table

I have an app running on my Raspberry Pi with Postgresql 9.1.
My first iteration was to add weather records into a table called "Weathers". That was successful.
My next iteration is to use psychopg2 to write records from Python into a different table called "weather". That also is succcessful.
What should also be successful is to change the Weather class in my app to the new fields. But DataMapper returns a mapping error:
#records = Weather.all(:order => :timestamp.desc)
ArgumentError at /
+options [:order]+ entry :timestamp does not map to a property in Weather
Rereading the datamapper.org docs suggests it's something to do with my table name so I migrated over the older "weathers" table into another called "older" and dropped the "weathers" table. But DataMapper still fails to find this table.
My Postgres environment with a truncated view of the target table is:
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-------
public | customers | table | pi
public | older | table | pi
public | systemlog | table | pi
public | weather | table | pi
(4 rows)
Table "public.weather"
Column | Type | Modifiers | Storage | Description
-----------------------------+-----------------------------+------------------------------------------------------+----------+-------------
id | integer | not null default nextval('weather_id_seq'::regclass) | plain |
timestamp | timestamp without time zone | default now() | plain |
currentwindspeed | real | | plain |
bmp180temperature | integer | | plain |
Indexes:
"weather_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
My Datamapper class is:
class Weather
include DataMapper::Resource
property :id, Serial
property :bmp180temperature, String
property :insidehumidity, String
property :totalrain, String
property :currentwinddirection, String
property :currentWindSpeed, String
property :timestamp, DateTime
end
DataMapper.finalize
Weather.auto_upgrade!
Since this is Ruby I fired-up IRb, required the Datamapper gem and got:
records = Weather.all
DataObjects::SyntaxError: ERROR: column "bmp180temperature" does not exist
LINE 1: SELECT "id", "bmp180temperature", "insidehumidity", "totalra...
^
(code: 50360452, sql state: 42703, query: SELECT "id", "bmp180temperature", "insidehumidity", "totalrain", "currentwinddirection", "current_wind_speed", "timestamp" FROM "weathers" ORDER BY "id", uri: postgres:pi#localhost/postgres?scheme=postgres&user=pi&password=pw&host=localhost&port=&path=/postgres&query=&fragment=&adapter=postgres)
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `execute_reader'
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `block in read'
from /var/lib/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
This makes me think it's not finding the right table. It seems to be OK with the id column, perhaps.
I see what appears to be DataMapper is being used for PHP in the CodeIgniter framework but I'm unsure if it's the same DataMapper I'm using in Ruby.
What am I overlooking to get DataMapper to find this table?

how to set value of cell in multiple record - laravel, ajax

I use AJAX mechanism to set create or modify records in this table:
table:
id | item_type | item_id | creator_id | attitude
1 | exemplar | 3 | 33 | 1
2 | exemplar | 4 | 33 | 0
3 | exemplar | 3 | 35 | 1
In plain English: there are many exemplars to choose for one user. A given user can only set only one exemplar to value 1. In this particular case Exemplar #3 is active (attitude = 1). I want to set its "attitude" to 0 and in the same controller method where I have the below code.
The below code creates a new record for an exemplar which has never been chosen before, or changes the value of 'attitude column.
$user_id = Auth::user()->id;
$countatt = $exemplar->attitudes()->where('creator_id', $user_id)->first();
if (!$countatt)
{
$countatt = new Userattitude;
$countatt->creator_id = $user_id;
$countatt->item_type = 'exemplar';
$countatt->item_id = $exemplar_id;
}
$countatt->attitude = $value; // $value = 1
$countatt->save();
Problem to solve:
1. how, using the best practices, set all other records of the same user (creator_id) and exemplar_id to 0
My best guess isbe to put the below 4 lines before the code quoted above:
$oldactive= Exemplar::where('creator_id', $user_id)->where(exemplar_id, $exemplar_id)->first();
$zeroing_attitude= $oldactive->attitudes()->first();
$zeroing_attitude->attitude = 0;
$zeroing_attitude->save();
;
The above solution works only in case when there is only one exemplar with value of 'attitude' set to 1. But in the future I want to allow users to have multiple exemplars active. I am not familiar with Eloquent enough to rewrite the logic for multiple active Exemplars.
Sometimes there will be no active Exemplars set, which means that this collection would be empty
$oldactive= Exemplar::where('creator_id', $user_id)->where(exemplar_id, $exemplar_id)->first();
How should I skip executing the rest of the code in such case? By adding IF as below?
if($oldactive) {}
Thank you.
$oldactive= Exemplar::where('creator_id', $user_id)->where(exemplar_id,$exemplar_id)->first();
foreach($oldactive->attitudes() as $zeroing_attitude){
$zeroing_attitude->attitude = 0;
$zeroing_attitude->save();
}

xpolog count syntax

I have a website with application pool "accounts.services"
I want to use xpolog to group all logs by their title
and order them by their count
I have tried:
(Computer = VM2014.conduit.local ) in app.accountsservices | count | group by Source | order by count des
but go syntax error
what is wrong?
Computer contains VM2014 in app.accountsservices | count | group by source | order by count desc

Resources