How to select specific columns in a polymorphic relationship where table columns are different? - laravel

How can I only select specific columns in a polymorphic relationship because the table columns are different?
$query->with(['Postable' =>function($query){
$query->select('business_title','name ','last_name');
}
The business_title column exists in the agency table. Name and last_name exist in the user table. If I select one table other table gets an error of column not found.

Either develop a naming convention that fulfills your needs (ex: columns like 'name', 'title' can be found on many tables), or do not use select in your query and get all coulmns: $query->with(['Postable']); and you would need appropriate ways to read your query results. Or simply use multiple queries to read from different tables which seems to be the rational option.

Related

Join database table based on Json colum having multiple key

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.
You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

Fetching model objects with distinct column values

I have an applications table, containing user_id, email, phone, etc.. An applicant may or may not create a user account, because registration is optional; hence user_id is nullable. They might also apply multiple times with the same email address, in which case email in applications table would be duplicated across several records.
Now, the objective is to fetch all Application model instances excluding duplicates. EX: if Jack applied five times with jack#gmail.com, the applications table would have 5 records with email column set to jack#gmail.com. When fetching Applications, I'd like to get only one record with jack#gmail.com, the other four should be ignored.
So, I tried multiple approaches, but they either fetched all records or didn't execute at all:
groupBy('email') fails with get() because of non-aggreagated column email:
Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'applications.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
At the same time, groupBy('email')->get(['email']) works well, but the problem is that I need other columns too (created_at at least), ideally - the full Application model instance.
Application::whereIn('id', function($query) { $query->select('id')->groupBy('email'); })->get() returns all records from the table
Application::select('*', 'DISTINCT email')->get() results in
Column not found: 1054 Unknown column 'DISTINCT email' in 'field list'
The Eloquent approach Application::distinct()->get(['email']) gives the proper list of emails, but once again, I need more columns that just the email. Once I start selecting more columns, I keep getting all records as results.
All of the following queries fail due to the strict mode being enabled:
SELECT * FROM ( SELECT * FROM applications ORDER BY id DESC ) AS tmp GROUP BY email
SELECT * FROM applications WHERE id IN(SELECT id FROM applications GROUP BY email)
SELECT email, id, created_at from ( SELECT email, id, created_at FROM applications) apps group by email
Can anyone help me to figure out the proper approach? I'd love to use Eloquent and work with actual models, rather than stdObj or other, if Eloquent can accommodate my problem/limitations.
P.S. The SQL engine I'm using is MySQL with strict mode enabled.
First of all, if you want to put an expression inside your eloquent you must use DB::raw('DISTINCT email') for that.
Second, I believe you should go with this approach:
Application::select('id', 'email', 'more_fields')->groupBy('email')->get();
You must have the column you group by it in your select() function.
Last option:
You can just use the ->distinct() function on your eloquent query to remove duplicates.

Codeigniter join multiple times same table and same column name

I'm having a problem, i have this table named IMOVEIS and another table named CARACTERISTICAS.
IMOVEIS must do at least 10 joins with CARACTERISTICAS, to get different caracteristics (this is a real state project).
Now I know how to make multiple joins in same table, the problem is: the column name of CARACTERISTICAS table (CT_NOME it's the column's name).
So how to change column name to each join result?

How do I link two parse tables per common field?

I have a simple relationship between two parse tables (objects).
Both contain a 'name' string column.
All I need to do is to create a join between the two tables per 'name' column.
For example:
Table A (main) <--> Table B (ancillary)
where
'A.name' == 'B.name'.
Table A is the driving table: all criteria focuses on Table A.
Table B is the ancillary table that has a particular field (column) needed for Table A's result.
Note: the tables don't have any formal relation with each other.
What is the correct Parse syntax to allow this to happen?
...or must I make two queries instead on one merged query?

How to bind multiple tables to single DM form?

I have a DM Form type page, where I would like to show and update values from different tables. By default when I created page, I have been asked for data source, specified only one of the tables, which in fact created process in After Header of type Automated Row Fetch. Also in After Submit a process of type Automatic Row Processing (DML) is created. When I add an item in the page from different table, obviously I am getting an error that the column not found in that table. How can I add more tables into that page, so they will be fetched and updated properly?
There is a common column in all tables, to identify which record I would like to show from each table.
I would like to show and update values from different tables
[...]
There is a common column in all tables, to identify which record I would like to show from each table.
Maybe are you looking for a simple JOIN on your various tables?
Based on Oracle's HR example schema, if you need to display both departments and employees names, you write something like that as your report query:
SELECT department_name as dep_name,
first_name || ' ' || last_name as emp_name
FROM HR.DEPARTMENTS JOIN HR.EMPLOYEES
USING(DEPARTMENT_ID)
In order for this to be usable from APEX form builder, you have to wrap it in a VIEW:
CREATE VIEWS MY_VIEW AS SELECT .... FROM ... JOIN ...
I can't guarantee that the view will be update-able out-of-the-box as Oracle has very specific rules for inherently updatable views -- especially for join views. However, you might be able to make your view updatable anyway by using an INSTEAD OF trigger. If you decide to go that way, maybe worth considering asking an other question on that specific topic if needed.

Resources