I seem to have a hard time understanding how to properly query as the logic of it gets confused as I try to use different operators.
Anyways, I have three tables: Customer, Cpe, Equipment. I want data from equipment but in order to do that, I must use Customer to relate to Cpe, then relate that table to Equipment.
The relationships are as follows:
Customer has many Cpe customerid > customerid
Cpe has one Equipment equipid > equipid
There is nothing relating Customer and Equipment except the Cpe table.
I specifically want to select the Customer.customerid, Customer.name, Equipment.nickname, Customer.customerstatus, Customer.installationdate. But I also want to only select the customers who have Equipment.nickname LIKE %SIM%. So what I have is:
$baicell_customers = Customer::with('Cpe')
->with('Cpe.Equipment')
->select('Customer.customerid', 'Customer.name',
'Equipment.nickname', 'Customer.customerstatus',
'Customer.installationdate' => function($query){
$query->where('Equipment.nickname', 'LIKE', '%SIM%');
})
->orderBy('Customer.name', 'asc');
->get();
But this gets me nothing. I'm not great at the whole relating one table to another and to throw in the fact that I have to use a related table to get info from another is just bonkers for me so any help would be greatly appreciated.
This is basically a many to many, you will be able to get it with the following. In general in Laravel you get out the models and do something with the data, do not do it with selects unless you are in a highly enterprise oriented system. Your naming conventions is off also since cpe is a pivot table basically see Laravel Many to Many.
public class Customer
{
public equipments()
{
return $this->belongsToMany('App\Equipment', 'INSERT CPE TABLE NAME', 'customerid', 'equipid ');
}
}
$customer = Customer::with('equipments')->all()->first();
foreach($customer->equipments as $equipment)
{
$equipment->nickname;
}
Even thou you say cpe only can have one equipment the design is a many to many, to circumvent this you can do.
$equipment = $customer->equipments->first();
So I gave up on doing this in the Eloquent style for the time being. It took some time but I was able to get a DB::connection('table') query to work. Here's the code:
$baicell_customers = DB::connection('azotel')->select("SELECT distinct customers.name, customers.customerid as id, customers.installationdate, customers.customerstatus[1] as status, equipment.nickname as ap FROM cpe INNER JOIN customers on customers.customerid = cpe.customerid INNER JOIN equipment on equipment.equipid = cpe.equipid WHERE equipment.nickname LIKE '%SIM%' order by name ASC");
This worked perfectly as intended and yet I can't get it work correctly in Eloquent at this point but that's alright for now. If this helps anyone figure out what the Eloquent version would be, I would still love to hear it. Otherwise, this has been solved.
Related
I have a DB, "views," with many, many entries. I also have a "Courses" table, which these views are one-many related to. In Laravel Nova, I can get a metric of all views over time for a course with some code like this:
public function calculate(Request $request)
{
return $this->countByDays($request, view::where('viewable_id', $request->resourceId));
}
In this case, viewable_id is the id of the course, and $request->resourceId gives the ID of the course to sort by. Pretty simple.
However, now things get a little difficult. I have another model called Teachers. Each Teacher can have many courses, also in a one-many relationship. How do I get a metric of views over time for all the courses that teacher teaches?
I assumed the simplest way to do this would be to create a Laravel Collection with all courses the Teacher teaches (not exactly efficient), and then select all views in the database where viewable_id matches one of the courses in that list. Of course, by posting this, I couldn't figure out how to do that.
Of course, once this is figured out, I'd love to do the same thing for Categories (though that should function in a very identical manner to Teachers, so I don't need to ask that question).
How do I get a metric of views over time for all the courses that teacher teaches?
This should be the "countByDays" of views where the viewable_id is in the list of course ids that the teacher teaches.
An SQL query statement to achieve that is given below:
select * from "views"
where "viewable_id" in (select "id" from "courses" where "teacher_id" = ?)
The Eloquent query should be similar to:
$this->countByDays($request,
view::whereIn(
'viewable_id',
Course::with('teacher')
->select('id')
->where('teacher_id', $request->resourceId)
)
);
I have three tables: users, purchase_orders and approvals.
One purchase_order has to be approved by multiple users.
When a new purchase_order gets created, I also create 3 pending approvals belonging to that PO.
The approvals table has a field allowed_user_type that determines who can approve it.
I can't figure out, what is the Eloquent way of selecting the pending purchase orders that can be approved by a specific user, as these are determined from the approvals table.
So far I can pull the pending approvals from the approvals table for a user with the following in the User model.
public function approvals_pending()
{
return $this->hasMany('App\Approval', 'allowed_user_type', 'user_type')
->where('approved', '=', 0);
}
The question is, how do I combine this with a theoretical filter?
I mean ideally, I would love to write:
return $this->hasMany('App\PO')->whereIn('id', '=', $this->approvals_pending()->get()->po_id);
Or something like that...
Any ideas would be greatly appreciated.
OK, for anyone interested I found a solution:
It's very close to what I thought I would have to write.
The lists method basically creates a single array out of the selected field, so it can be plugged-in directly to a whereIn method like so:
return \App\PO::whereIn('id', $this->approvals_pending()->lists('po_id'));
I don't know if this is the most Eloquent way of doing this but it does work.
I have 2 tables and I need to join those tables.
I need to select id and name from galleries, where share gal.id = galleries.id and user_id = auth::id().
Tables:
Galleries: id, name
Share: gal_id, user_id
Please show me example in laravel. And I need to display it.
To achieve this, you have Relationship in Eloquent ORM. The official website states :
Of course, your database tables are probably related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy. Laravel supports many types of relationships:
You can read all about eloquent relationship over here
If you do not want to use relationship, following is an example on how to join two tables :
DB::table('users')
->select('users.id','users.name','profiles.photo')
->join('profiles','profiles.id','=','users.id')
->where(['something' => 'something', 'otherThing' => 'otherThing'])
->get();
The above query will join two tables namely users and profiles on the basis of user.id = profile.id with two different conditions, you may or may not use where clause, that totally depends on what you are trying to achieve.
This question is about selecting data from multiple tables, joins, Doctrine2, ResultSetMapping, DQL and such stuff.
I have 4 tables:
user
contact
contact_phone
call
With relations as shown on the image: http://i.stack.imgur.com/762Jw.png
Every user can have many contacts, each contact can have many phones and each user can have many calls to/from his contacts. Just like in the real world... I've limited the number of fields in each table just for clarity.
So my problem is that I don't know how exactly to map call numbers to contact names when showing a list of calls for a specific user.
If I want to list all calls of user 1 I do:
$callRepository = $this->getDoctrine()->getRepository('MyBundle:Call');
$calls = $callRepository->findAll(array('user' => 1));
But this will give me just the list of all calls for this user and will not associate number (call.number) with names (contact.name).
I can achieve what I want with plain SQL with this query:
SELECT
c.number,
contact.name
FROM
`call` c
JOIN contact_phone cp ON
cp.number = c.number
JOIN contact ON
contact.id = cp.contact_id
WHERE
c.user_id = contact.user_id
AND c.user_id = 1
Please note that I don't want to select all calls (with SQL) and then map numbers to names with another query from the PHP layer because this way I won't be able to search the calls by name for example.
I was thinking that ResultSetMapping could help me in this case but I have no luck putting the SQL query and the ResultSetMapping together.
Please help,
Thanks!
As per my knowledge, you can acheive by using the below methods. Please go to bottom of the page. you can find Joins... try once..
http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html
I have 3 tables.
users,
campaigns,
links.
They have one-to-many relations. User have many campaigns. Campaign has many links.
I calculate top users by counting the total number of links each user have. So I join 3 tables and group by user and select user and sum(links).
This query involves 3 tables. I have already 3 model class for each of these tables. Now my question is in which model should I put this query? should it go in User model (as I call it top10users? Or there is other way to get this info by utilizing already existing 3 models. I want to know what is most suitable from MVC's point of view.
Additional Information
I am using orm in kohana.
Query:
SELECT u.id,
u.name,
SUM(l.num_visited) total_num_visited
FROM users u
JOIN campaigns c
ON ( u.id = c.user_id )
JOIN links l
ON ( c.id = l.campaign_id )
GROUP BY u.id
ORDER BY total_num_visited DESC
LIMIT 10;
There's no strict reason that your model absolutely has to map 1-to-1 with a table. It may make the most sense in this case to provide a model specifically for Top Users, especially because it is dependent on joining data from several related tables.
Then if there is specific business logic related to Top Users that isn't relevant to the standard User class, you can keep it separated and clean. You can always provide helper / factory methods from TopUser that return instances of User, Campaign, or Link if you need to drill down.
I would say go for topusers(). There is no "correct" answer to this question, not in a technical context. But it should be clearly to understand. So if you want to get top users, why would you put such a method in one of the other models than users? Think of a new team member who gets your code: Where would you look for top users when not in the users model first?