Laravel 5 and EAV relationship - php

I'm in trouble with eav model. Basically; i have three tables. User, UserInfo, Country.
User userinfo Country
--------- ------------ ------------
id id id
username user_id country_name
pass option_id
etc option_value
I connected user and userinfo models succesfully. There is a no problem.
I retrieving user infos in user model with this:
$this->info->where('option_id', $key)->first()->option_value;
My problem is right there. I'am storing "country_id" in option_id and option_value.
In example:
userinfo
--------
id 1
user_id 5
option_id country_id
option_value 3
I am running this query from user model
$this->info->where('option_id', 'country_id')->first()->option_value;
It returns 3. There is a no problem. My problem is i couldn't get country_name.
I don't know which relationship to use for this.
I can make this using "DB" class but then i need use another queries for all of theese types. It's not the best solution.
Can someone help me?
Edit : I am looking for like this solution (if it's possible):
$this->info->where('option_id', $key)->first()->option_value->country_name;
Edit : I am using laravel 5.1

The Country model belongs to the userinfo.
On your userinfo model, you should define the relationship.
UserInfo Class
public function country()
{
return $this->belongsTo('App\Country', 'local_key', 'parent_key');
}
In this case, your 'local_key' would be 'option_id' and your 'parent_key' would be 'id'.
Once you've defined the relationship, you can get all attribute of country like this:
$this->info->where('option_id', $key)->first()->country()->get();

Related

intermediary model to extends laravel model

Hello, this is my first post in stack overflow. If i made any typo or type something wrong, im very sorry for the bad english.
So i have this stiuation : I have 1 table user have id, parent_id and every other table have user_id. So when the user with parent_id = 0 can see all the activities that other user have that user id as parent_id.
Problem 1 :
If the user A has parent_id = 0 query to like example : product table, i want to get all product that have user A id and all product with any other user that has parent_id = user A id.
Table users :
table user
Table econtracts :
econtracts table
I can handle problem 1.
Problem 2 :
Because problem 1 exists like that so that every model i need to do based on problem 1. So now i need an intermediary model to extend laravel model to custom every method that laravel model offer. Currently stuck on get() and paginate().
What i need is replace the laravel model so when i create model i can extends that custom model for my model.
The logic is when i query using query builder or eloquent, if i using paginate() or all() or get() i want to include problem 1 in that query without touch core framework.
Thanks for reading this !

Laravel Eloquent Relationship between users table and states table with states_id in users table

I am trying to figure out relationships in laravel and have come to the end of my rope so I thought I would post my very first question here in hopes of figuring it out. I can generally find the answer here but not this time.
I have the users table like so:
id
first_name
last_name (etc.)
address_1 (etc.)
state_id
city (etc.)
Then I have a states table
id
state_abrv
state_name
Now from what I gather Eloquent wants the users_id stored in the states table instead of states_id stored in the users table but that does not make sense because you would have to repeat the states over and over.
On my User model I have tried
public function state()
{
return $this->belongsTo(State::class);
}
and
public function state()
{
return $this->hasOne(State::class);
}
but I think it is looking for the foreign key on the states table and not the users table. What am I missing here?
I want to get a list of all users for example in the show user details view with their state without a second query to get all the states into an array and getting their state from that array.
Like
$users = User::with('state')->get();
If I really understand what you want, you really will have to update the state method in your User Class by adding a second parameter.
public function state()
{
return $this->belongsTo(State::class, 'states_id'); // if the column name in your users migration is state_id then you might not need the second param.
}
Then you can get a user with the state relationship with
$users = User::with('state')->get();
I added states_idas a second param because that is the column name you have provided as in your migration

Laravel: Complicated Eloquent Relationship - hasManyThrough or belongsToMany approach?

I have three Models (Organization, User, Visit) and 4 tables (organizations, users, organization_user, visits). I'm trying to get the accumulative total of user visits for an Organization.
Organization
------------
id,
name
User
---------
id,
name
Organization_User
----------------
id,
organization_id,
user_id
Visit
--------------
id,
user_id
views
Just to clarify, there is no Organization_User model, that is just the pivot table used by User and Organization:
$organization->belongsToMany('User');
$user->belongsToMany('Organization');
I could query all the user_ids from the pivot table by group_id, and then get all the visits for each user_id, but what's the more Eloquent approach?
A User hasMany Visits and a Visit belongsTo a User. A Visit doesn't belong to an Organization.
Solved it by using whereIn(). Basically with no change to my current relationship setup...to get accumulative views I did this:
$org = Organization::find($org_id);
return DB::table('visits')->whereIn('user_id', $org->users->modelKeys())->sum("views");
The modelKeys() returns all the user ids tied to that Organization. Then I get the sum of all the views for those users.
*Note it's also important to use Organization::find and not DB::table('organization') in order to maintain the Eloquent relationship. Otherwise $organization->users will give an error.
I think you might want a 'Has Many Through' relationship like so:
class Organization extends Model
{
public function visits()
{
return $this->hasManyThrough('App\Visit', 'App\User');
}
}
Then you could call count().
Laravel 5.1 doc

Laravel get model by hasManyThrough

I have 3 classes - User, Business and BusinessRoleUser (and respective tables)
(user) id | first_name | last_name
(business) business_id | business_name
(business_role_user) user_id | business_id | role_id
I have combined the role of the user in the same table as their are multiple roles that user's can have towards a business i.e. admin, staff, etc. which are in the business_roles table.
I would like to access the Business Model through a logged in User to update fields i.e.
$business = User::find(Auth::id())->business();
$business->fill(Input::all());
$business->save();
At present in my User class I have the following (really each user will only be associated to one business):
public function business()
{
return $this->hasManyThrough('App\Business', 'App\BusinessRoleUser', 'business_id', 'user_id');
}
But If I do a simple dd(User::find(Auth::id())->business->business_name);
it throws an error
business name is undefined
It's not actually finding the business model associated to the user. I've looked at many examples of hasManyThrough but am failing to get this to work, appreciate the help!
(note user table uses id where as other tables use user_id, not sure if this is causing an issue)

How to get courses through transactions table Laravel

I want to get all users with courses through transaction table.
Here is my db structure
course
id | name | description | price
transactions
id | course_id | user_id | name | description | expires
users
id | username | email
http://laravel.io/bin/da08n
I have tried with joins but it returns the same user multiple times.
DB::table('transactions')->join('users', 'transactions.user_id', '=', 'users.id')->join('courses', 'transactions.course_id', '=', 'courses.id')->distinct()->get();
How can I achieve this with Eloquent or in an other way?
Well.
you have a table called transactions, and a table of courses. Now, each transaction is related to a course.
Pay Attention: You have one record in one table which is related to another one record in another table, this is called one-to-one relationship.
In laravel, you achieve this by using Model's relationship.
You have one model called 'Transaction', and another model which is called 'Courses".
You get, for example one record from courses table by using Cources::find($myId).
Now, if you want to get a record of transactions by relating it to courses table., you should do this (in Laravel):
In your translation model define a method called course:
public function course ()
{
$this->hasOne("course"); // course is the name of another model which should be related
}
And then you use it like this:
$result = Transaction::find($myCourseId)->course();
Though for your case, you want to get many records through matching one record of a table to the another record of another table. Well:
In Transaction model add the following method:
public function Users()
{
return $this->hasManyThrough('User', 'Course');
}
With the above method you should be able to get the Users through Course method based on transactions.
More on that, please visit:
http://laravel.com/docs/eloquent#relationships

Categories