After a lot of searching i couldn't find a real solution to my "problem", so if there any one how can help me, i’am all listening
…
I have tree models, Post, User and Profile
Post belongsTo user – user hasMany posts == One –to-Many relation
Profile belongsTo user – user hasOne profile == One-to-One relation
Database structure
User
---- id : primary key
---- email : string
---- name : string
…
Profile
---- id : primary key
---- user_id : foreign key
---- biography : text
---- facebook : strings
…
Post
---- id : primary key
---- user_id : foreign key
---- title : string
---- slug : string
---- body : text
…
User model
class User extends Eloquent {
public function posts(){
return $this->hasMany('Post');
}
public function profile(){
return $this->hasOne('Profile');
}
}
Profile model
class Profile extends \Eloquent {
public function user(){
return $this->hasOne('User');
}
}
Post model
class Post extends \Eloquent {
public function user(){
return $this->belongsTo('User');
}
}
HOW can i get the users biography (or any other properties) on profiles table through the posts
I hope i was clear enough
There's no hasManyThrough in your setup.
First fix this relationship:
//Profile
public function user(){
return $this->belongsTo('User');
}
Then simply:
$post->user->profile->biography;
$user->profile->biography;
There is no profiles data through the posts, since both post and profile belong to the user.
There's also another way to link posts and profiles tables:
// Post model
public function profile()
{
return $this->hasOne('Profile', 'user_id', 'user_id');
}
// Profile model
public function posts()
{
return $this->hasMany('Post', 'user_id', 'user_id');
}
Then:
$post->profile->biography;
$profile->posts; // collection of Post models
This way you save one query by not fetching the User model.
Related
I am trying to make many to many relationships b/w three table I don't know how to make that please confirm me my relationship correct or not.
Project table
id | name
user table
id | name
project_assign
id | user_id | project_id
User Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','user_id','id');
}
project_assign Model
public function user()
{
return $this->belongsToMany('App\User','Project_assign','user_id','id');
}
Project Model
public function project_assign()
{
return $this->belongsToMany('App\Project_assign','project_assign','project_id','id');
}
you don't need to make a relation in project_assign model. only Project and User will have relation,
Project Model
public function users(){
return $this->belongsToMany('App\User','project_assign','project_id','user_id');
}
User Model
public function projects(){
return $this->belongsToMany('App\Project','project_assign','user_id','project_id');
}
In controller you can get like this
User::with('projects')->get();
Project::with('users')->get();
I am trying to get some data through relationships and foreign keys but have gotten myself in a tangle.
I have a Batsmen who user can comment on,the comments can also be edited by the users, the comments have their own page aswell with the option to delete or edit the comment. This is where my problem comes, i am trying to show the nationality of the batsmen but can only retrieve the ID of his nationallity and not the name of the country.
Here are my tables
Batsmen: ID, Name, bio, Nationality_ID,
Nationality: ID, country_name,
User ID, name ,
Comment: ID, comment, Batsmen_ID, User_ID
My Relationships
Comment:
public function batsmen(){
return $this->belongsTo('App\Batsmen');
}
public function author()
{
return $this->belongsTo('App\User','user_id');
}
Controller:
public function show($id)
{
$comment = Comment::find($id);
return view('comments.show')->with('comment', $comment);
}
View :
<h4>{{$comment->batsmen->Nationality_id}}</h4>
So how would i get the country name to display instead of the nationality_ID?
Make another relationship method on your Batsman model class as -
public function nationality(){
return $this->belongsTo('App\Nationality','Nationality_id');
}
And then in your view-
<h4>{{$comment->batsmen->nationality->country_name}}</h4>
try this
public function show($id)
{
$comment = Comment::with('batsmen')->find($id);
return view('comments.show')->with('comment', $comment);
}
You need to add the relationship between bastman and nationality
class Comment extends Model
{
public function nationality(){
return $this->belongsTo('App\Nationality',Nationality_id,id);
}
}
Then you can easily get his country
<h4>{{$comment->batsmen->Nationality->country_name}}</h4>
I'm doing a project for one of my University courses. But I'm finding it difficult to make relationship between User to User model.
Table: users
id
name
email
type
password
On this table the column type is used for classifying user types.
For example: User can be either distributor or dealer type.
And a distributor may have many dealer
But a dealer may have only one distributor.
To implement this I've created another table and named as dealers_of_distributor.
Table: dealers_of_distributor
id
distributor_id (id of users table)
dealer_id (id of users table)
Although I've used an additional model DelaersOfDistributor, the exact relationship should be between user to user.
How can I make this relationship. I'm confused!
I've tried so far:
Model: User
public function dealersOfDistributor(){
$this->hasMany('App\DealersOfDistributor');
}
Model: DealersOfDistributor
public function user(){
return $this->belongsTo('App\User');
}
The DistributorsController
public function index()
{
$distributors=User::where('type', 'distributor')->get();
$dealers=User::where('type', 'dealer')->get();
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index')->withDistributors($distributors)->withDealers($dealers)->with('assignedDealers', $assignedDealers);
}
In blade file: I've tried $assignedDealer->dealer_id->user->name. But It's showing error.
Model DealersOfDistributor:
public function distributorUser()
{
return $this->belongsTo('App\User', 'distributor_id', 'id');
}
public function dealerUser()
{
return $this->belongsTo('App\User', 'dealer_id', 'id');
}
than you just get the user's data from DealersOfDistributor model, call function dealerUser if you want get user's data from dealer_id, and call function distributionUser if you want get user's data from distribution_id
Controller:
public function index()
{
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index',compact('assignedDealers'));
}
Blade get username:
#foreach($assignedDealers as $assign)
{{$assign->distributorUser->name}}
{{$assign->dealerUser->name}}
#endforeach
I have the following Models with corresponding tables:
User (users), UserProfile (user_profiles), Review (reviews)
Schema
table: users
user_id (primary key)
first_name
last_name
table: user_profiles
user_id (foreign key)
country
phone
table: reviews
reviewer_id (the user who posted the review)
user_id (foreign key)
body
I query the database with the following:
$user = User::with('profile', 'review')->find(Auth::User()->user_id);
Part of User Model
public function profile(){
return $this->hasOne(UserProfile::class, 'user_id');
}
public function review(){
return $this->hasOne(Review::class, 'user_id');
}
Part of UserProfile Model
public function users() {
return $this->belongsTo(User::class, 'user_id');
}
Part of Review Model
public function users() {
return $this->belongsTo(User::class, 'user_id');
}
How can I access the details of the reviewer using the reviewer_id from the users table
As a sunup of what was discussed in this question, I think the major concern of OP was regarding a table that have 2 foreign keys with the same table. Both user_id and reviewer_id are foreign keys linked to the users table.
One important thing to understand about Laravel relationships is that the relationship name is not hard bound, it can be anything as long as it makes sense to you. The related object will be available in the ATTRIBUTE with the exact name of the METHOD.
class User extends Model {
public function review() {
return $this->hasOne(Review::class);
}
}
A user's review will be available at User::find(1)->review;.
class Review extends Model {
public function owner() {
return $this->belongsTo(User::class, 'user_id');
}
public function reviewer() {
return $this->belongsTo(User::class, 'reviewer_id');
}
}
From a review, you will be able to access both users:
Review::find(1)->owner; // This is the relationship with user_id column
Review::find(1)->reviewer; // This is the relationship with reviewer_id column
I've a User model and a likes table. I applied polymorphic relation to it.
likes table has the following structure:
user_id --- likeable_id ---- likeable_type
eg: 1 ---- 5 ---- App\User //This indicates user 1 follows 5
eg: 3 ---- 1 ---- App\User //This indicates user 3 follows 1
Now I'm trying to get the followers and following list.
In the User model I added
public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}
public function followers()
{
return $this->belongsTo('App\Like', 'likeable_id', 'user_id');
}
and from the controller, I've tried to do this:
$user1 = User::with('followers')->where('username', $username)->first();
But it's returning null for the followers relation. Am I doing it right?
Got the answer by myself: In User model -
public function followers()
{
return $this->morphMany('App\Like', 'likeable')->with('user');
}
public function following()
{
return $this->morphMany('App\Like', 'user', 'likeable_type')->with('user');
}