New to Laravel and MVC so I'm a bit stuck and hoping I can get a solution from you.
I have 3 tables:
Users
Companies
Assets
These are all joined by pivot tables (I think that's the right term)
company_user (A company may belong to many users)
asset_user (A user may have many assets)
asset_company (A company may have many assets)
What I would like to do is get all assets for the current users company.
I manage to get the users ID. I then somehow need to use the ID to find out which company they belong to, and then retrieve all assets for that company.
I have setup the models as I think they should be. I just need to know how to get the relational data.
Any help or advice is greatly appreciated.
Thanks
You can probably use eager loading :
http://laravel.com/docs/eloquent#eager-loading
In your case:
$user = User::with('compagny.assets')->find($user_id)->get();
Where compagny and assets are the relationship function names.
Related
i am struggling with setting up my database and eloquent relationships in a certain scenario.
This certain part of my application will be handling online orders.
basically i want an order to consist of multiple configured items.
i want configured items to consist of a base item (ex. a cheesburger) and also of toppings.
i have gone through several scenarios, but I am trying to make this as simple as possible. here is the quick and dirty story of what I have now.
I want a configured item to consist of three things. 1. the order id of the order it is associated with. 2. the menu item that it relates to (ex. cheeseburger, hotdog ) 3. and the toppings.
I am considering two tables that are full of relatively static information about the menu items and the toppings to be referenced from the configured item table.
I had originally considered creating a new menu item on every configured item, but I like the idea of just being able to look up items/toppings and applying them to a configured item. Im sorry if this is unclear. I am three days into this and my brain is absolutely in pain by now.
here are the relationships i am considering.
configured_item: belongsTo Order; hasOne menu_Item;
Menu_item: belongsToMany configured_item; hasMany toppings;
Toppings: belongsToMany configured_item;
I guess in a way my configured item table is a pivot table of sorts, but then it will need to be referenced by an order as well.
i know questions have been asked about three way relationships, but I cant find any info on tables that are relatively static like i am trying to use.
I finally caved and used two pivot tables. it all works, but i cannot help but feel there is a better way to handle this. It seems a lot of people have similar issues and there is no clear cut solution.
I'm currently playing around with spark for laravel. I'd like to limit the amount of teams per user to 1. So basically I wantto forbid to create new teams or join other teams. I found the
CanJoinTeams Trait
But I'm actually not sure where to keep looking for changes. I'd love if someone could tell me where I have to look at and where I could overwrite necesseray functions.
By looking at code that someone forked on Github for Spark, it looks like one option is going to be to change that relationship between the User Model (code in CanJoinTeams Trait) and the Teams Model from Many to Many to One to Many, which is going to require you to any spot that uses information in the pivot table.
Another, possible option is that you perform a check in one or both models to see if the user already belongs to a team and if they do either ask if they want to change teams or throw some kind of error.
I am using an MVC framework and have a photo SQL table and an album SQL table. Queries for the photo table go in the photoTableModel class, and queries for album table go in the albumTableModel class.
Where do I put queries that use both tables?
For example, the following query uses both tables. Do I place the query in the photoTableModel, albumTableModel, a new table model altogether, or maybe even a service?
SELECT `photo`.`id`
, `photo`.`name`
FROM `photo`
JOIN `album`
ON `album`.`album_id` = `photo`.`album_id`
AND `album`.`album_id` = 1
;
The center of the architecture is models, not tables. Semantically, which model is being described or operated on here?
This appears to be retrieving a list of photos, with some album information included in the elements of that list. So I suspect that this is related to the Photo logical model. So wherever you put this in your architecture and framework, it should be semantically related to that model.
How your framework defines things is up to that framework, so I really can't be more specific. Different frameworks do different things in different ways. In general, however, in the MVC pattern you want to build your logic and architecture around the models. And what you get from this query is a list of photos (with some extra information), so it's related to the Photo model.
Models can be direct one-to-one representations of tables. But they don't need to be. It looks like you're artificially limiting your models to be exactly that, which has led to this issue. Maybe you extend the Photo model concept to include album information? Maybe you create a DTO that's a hybrid of the two? Maybe you add an Album property to your Photo model? There are many ways to go about organizing models. The point is to keep the semantics of those models clear. Models represent domain concepts (like Photo), not database tables (like photoTableModel). The database tables simply persist the model information to be re-constructed later. As such, the database is an edge concern for the domain, not a central one.
The project that I am working on requires a sort of sharing functionality meaning that when a person creates an exercise they can choose to share that exercise with another person and append a certain permission to that exercise (i.e read, write, or execute).
I have three tables(all of which have models): users, exercises, and permissions. In the middle I have an exercise_permission_user table that only has three columns: exercise_id, permission_id, and user_id all of which are foreign keys that point back to their respective tables.
The problem comes with establishing a three way many to many relationship among these tables in Laravel 5. More specifically, when a person shares an exercise, I need to input the id's of the exercise being shared, the user it is being shared with, and the permission that is being appended into the exercise_permission_user table. I then need to be able to query the user_id of this table and see all exercises that are being shared with a certain user. If the user Mike has an ID of 3, then I would like to query the middle table for that ID and find the exercise he has access to as well as the permission that he is being granted.
I am still in the learning process when it comes to eloquent so any help would be greatly appreciated. I am not necessarily looking for someone to build this for me, just some help that will give me the information necessary to do it on my own. Thanks to all that help!
I've struggled with this issue a couple of times. As far as I could research, I didn't find a Laravel native way of coding this kind of three way many to many relationship. What I generally do is to create a model for the pivot table. So, a SharedExercise model (or the name you want to use) with a protected $table property set as 'exercises_permission_user'. Inside that model you set the relationships with user, exercises and permissions. Then, you can write:
$sharedExercises = SharedExercise::where('user_id', $userId)->get();
Pay attention to the table and model naming. I usually name tables using laravel's conventions, but when I have this 3 way many to many, I try to find a more describing name than the convention. So, for example instead of exercises_permission_user and ExercisePermissionUser model, maybe shared_exercises and SharedExercise names are better.
Note that this isn't THE way to do it. It's how I do it as a result of not finding a convention in the documentation.
I'm new to ZF and apigility.
I've tried to create DB-connected service with almost zero-configuration.
Everything works well, I've got simple CRUD api, but there's one thing that I did not find solution for.
I have two tables: venues and categories
and a pivot table categories_venues to manage many_to_many relationship.
When I'm trying to create the services from tables everything works fine excepting the pivot tables. As I understood somehow because of the underscore in the name of pivot table.
So I have two questions.
How can I solve this so apigility could discover the table and import it?
How can I setup custom routes to get the listing using the following pattern /category/venue ? Is there any magic like in Rails? Can I get all the venues in the particular category? And vice versa?
Thanks.
please check here: issue:many2many relations