I have a bookmark system. Where user can bookmark a forum & feed.
I am trying to use a polymorphic relationship. But I can't figure out how to use user_id with polymorphic relations. I want to use eloquent to fetch all feed bookmarks & forum bookmarks separately for a user. I am confused about what relationship to use where!
Business rules I want to achieve -
User can Bookmark Many Feed & Forum
Feed & Forum can be Bookmark by User
//Users table
id ----- name ----- email ----- password
//Feeds table
id ----- title ----- description
//Forums table
id ----- title ----- description
//Bookmarks table
id ----- user_id ----- bookmarkable_id ----- bookmarkable_type
User model
class User extends Authenticatable
{
public function bookmarks(){
return $this->hasMany('App\Models\Bookmark');
}
}
Bookmark model
class Bookmark extends Model
{
public function bookmarkable(){
return $this->morphTo();
}
}
Feed model
class Feed extends Model
{
public function bookmarked_by(){
return $this->morphToMany('App\Models\User', 'bookmarkable');
}
}
Related
I want to display data in laravel.
This is the classrooms table:
This is the students table:
this is the classroom_student table :
I expect a result like this:
With class from the classroom table, year from the student table (the year the student was created), and data from the student table (data for each month, sample data:[2,4,6] means Jan: 2, Feb: 4, Mar: 6). I don't know how to make it. Can you help me? thank you
You need to setup a many to many relationship both of your models (classroom model and student model. It will look something like this:
class Classroom extends Model {
public function students(){
return $this->hasMany(Student:class, 'student_id');
}
}
class Student extends Model {
public function classrooms(){
return $this->hasMany(Classroom:class, 'classroom_id');
}
}
Here is also a reference to laravel docs for the exact solution:
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many
You Should use HasMany Relation in both Models and while fetching data call this relation function to get data related to other model and set expected title for those data in Transformer.
I have 3 tables:
structurals
id
name
indicators
id
name
groupings
id
structural_id
indicator_id
And Result:
#structural name
indicator name
indicator name
#structuralname
indicator name
I've used method hasMany & hasManyThrough but errors.
In this case, an indicator may belong to one or more structural. And a structural can have one or more indicators.
What this describes is a many-to-many relationship where groupings is only a pivot table. So you should be using the belongsToMany instead:
class Indicator extends Model
{
public function structurals()
{
return $this->belongsToMany(Structural::class, 'groupings');
}
}
class Structural extends Model
{
public function indicators()
{
return $this->belongsToMany(Indicator::class, 'groupings');
}
}
Docs: https://laravel.com/docs/eloquent-relationships#many-to-many
You can also remove the id column from the groupings table, as it is unnecessary.
I have this user table
id | name | email | department_id
1 user 1 xyz#gmail.com 2
and this is department table
id | department_name
1 Admin
2 Account
there is salary table :
id | user_id | basic_pay
1 1 5000
I have this employee relation in salary model
class Salary extends Model
{
public function employee(){
return $this->belongsTo('App\User','user_id','id');
}
}
I want to get department name also to which user is associated
$Data = Salary::where('id',$id)->with('employee')->first();
but presently i can only get department_id with the help of employee relation.
Any help is highly appreciated.
You could add another relationship to the User model called department which would look like this:
public function department()
{
return $this->belongsTo(Department::class);
}
and then user dot notation documented here: https://laravel.com/docs/8.x/eloquent-relationships#nested-eager-loading. Which would look like this
Salary::where('id', $id)->with('employee.department')->first();
Additionally, you can simplify this by using the find method, which would look like this:
Salary::with('employee.department')->find($id);
You would need to reverse the other of methods due to the fact that find will return the model, in this case a Salary model.
If you do not know if the Salary will exist you may want to use findOrFail documented here https://laravel.com/docs/8.x/eloquent#not-found-exceptions
Data = Salary::where('id',$id)->with('employee.department')->first();
In Your Employee model add relation like Below
public function department(){
return $this->belongsTo('App\Department','department_id');
}
I have the following table structure:
Tokens
- type ( Flag Indicating that if its a Users token or company's token )
- user_company_id (User or Company Id )
Users
- id
- username
- password
Companies
- id
- username
- password
Im trying to create the relationship from the token to get the user or the company based on the type flag.
I know that I should have a different modeling, but this database is up and running and I cant change their structure.
How can I do this?
Im trying to figure it out but no success until now =(
You may try something like this (make sure the namespace and model names are correct):
Token Model:
class Token extends Model
{
public function tokenable()
{
return $this->morphTo();
}
}
User Model:
class User extends Model
{
public function tokens()
{
return $this->morphMany('App\Token', 'tokenable', 'type', 'user_company_id');
}
}
Company Model:
class Company extends Model
{
public function tokens()
{
return $this->morphMany('App\Token', 'tokenable', 'type', 'user_company_id');
}
}
Notice the third (type) and fourth (user_company_id) argument in the morphMany method call, those are optional but in your case, these are required because you've custom field names in your database. Check the documentation to learn how to retrieve relations.
I have a table named project. In this table I have a column named student_id. Model for this table is Project. And I have another table named user. And model name is User. Now I am retrieving all project table details in a page including student_id. Now I want to show user name instead of student_id. I mean I want to show this student name instead of student id. For example if student_id is 10 then I want to print name for that user from user table whose id is 10.
Any one please help.I have read document from laravel. But i don't know why I am not getting the eloquent concept properly.
Define belongsTo() relationship in Project model class:
public function student()
{
return $this->belongsTo(User::class, 'student_id');
}
And use it:
$project->student->name;