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 !
Related
i have problem in laravel. i have created multiple morph in one table.
table structure
table_name : morphicable
id:
model_id: eg: '1'
model_type: eg 'model/comment'
model_one_id: eg: '1'
model_one_type: eg 'model/order'
in above relationship we can saving the data from comment to order and order to comment like many to manay.
Now the problem is that how to get the both data from one ORM method. like some time we save comment in model type and some time in model typeOne.
i want to get the data from both models in one go.
public function morphicable(): MorphMany
{
return $this->morphMany(morphicable::class, 'model');
}
I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.
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();
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
Is there any way to get all relational data with hasmanyThrough of third model that has many records with the relation with second model?
As example Main model : Exam id batch_id
relation- belongTo-Batch
second model : Batch id batch_name relation-hasMany-Exam and hasMany-Student
third mode :Student id name batch_id relation- belongTo-Batch
So using Exam model when I try to get batch that belong to it then I can retrieve it but How can I get all student through Exam model related to that batch? I have tried with hasManyThrough relation but not working...
If you were to use this in Exam:
public function students() {
return $this->hasManyThrough('Student', 'Batch');
}
This won't work, as hasManyThrough assumes that both Student and Batch belong to Exam (Exam belongs to batch)
The easiest way would be something like this: Exam::find(1)->batch->students