Laravel 5.4 relationship 2 tables 3 id - php

I work with laravel 5.4, and I want receive information from my relations tables.
I have 3 tables in phpmyadmin
ATHLETES
ID
FIRST_NAME
LAST_NAME
TYPES
ID
NAME
SLUG
ATHLETES_TYPES
ID
TYPE_ID
ATHLETE_ID
I have 3 models
ATHLETE
TYPE
ATHLETEBYTYPE
How do I need to make the relations models to have name and slug from my table TYPES, with my id from my table athletes?
Thank you.

For this you just need 2 model file ATHLETE and TYPE and use many to many relation.and then you can use ATHLETES_TYPES table as pivot table.
for implement this :
first add this method to ATHLETE model file :
public function types()
{
return $this->belongsToMany(TYPE::class,'ATHLETES_TYPES','ATHLETE_ID','TYPE_ID');
}
secode add this method to TYPE model :
public function athletes()
{
return $this->belongsToMany(ATHLETE::class,'ATHLETES_TYPES','TYPE_ID','ATHLETE_ID');
}
and in last remove ID filed from ATHLETES_TYPES table.
done.
now if you have variable from ATHLETE type with $ATHLETE->types you can get types of than.
read more here :
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many

Here are the relationships, buddy
In App/Athlete.php
class Athlete extends Model
{
public function types(){
return $this->belongsToMany('App\Type');
}
}
In App/Type.php
class Type extends Model
{
public function Athletes(){
return $this->belongsToMany('App\Athlete');
}
}

Related

How to add relationships for additional columns in pivot table in Laravel and Eloquent?

I have 3 tables:
users
-id
-name
relation_types
-id
-type
user_relation
-user1_id
-user2_id
-relation_type_id
In User model I have:
public function relatedParties()
{
return $this->belongsToMany('App\User', 'user_relation', 'user1_id', 'user2_id')->withPivot('relation_type_id');
}
I can get the relation_type_id by App\User::find(1)->relatedParties[0]->pivot->relation_type_id.
In order to get relation type instead of id, I added this relationship in the model of user_relation table
public function type()
{
return $this->belongsTo('App\RelationType', 'relation_type_id');
}
but App\User::find(1)->relatedParties[0]->pivot->type returns null
Any thoughts would be appreciated.
You can use Nested Eager Loading
$user=User::with('relatedParties','relatedParties.type')->first();
foreach($user->relatedParties as $relatedParty)
{
foreach($relatedParty->type as $type)
{
print_r($type->type);
}
}
You can't access type from pivot table because type is not a column in the proviot table. so your code for access type is giving null for acessing type.
App\User::find(1)->relatedParties[0]->pivot->type
Again relationship in user_relation table is between relation_types, user_relation table which can not be called in the realtion of users,user_relation.
To get the type of "relation_type_id" just simply get the type from relation_types table as written below.
$relationTypeId = \App\User::find(3)->relatedParties[0]->pivot->relation_type_id;
$rtype = \App\RelationType::find($relationTypeId)->pluck('type');
echo $rtype;

Relationships using Eloquent

I'm using Eloquent in Laravel 5 and am a bit confused in relations between tables. These are existing tables of a software and cannot be altered.
I have the following tables that shows historical details of a student. All tables have model classes created.
student
-------
student_id (PK)
first_name
last_name
class
-----
class_id (PK)
name
section
year //2012, 2013 etc
student_id (FK)
result
------
result_id (PK)
class_id (FK)
month // jan, feb etc
status //pass or fail
Below are the details of each table
Student studies in a class for a particular year
Each class for that year will have 10 results (1 result for each month in an year)
Each result will contain the state for a test in a month (pass or fail)
I want to access objects based on the below examples. Can you guide on setting up relations in the model classes
student->class(2013)->result(jan)
result(feb)->class(2016)->student
First of all you should define the relationships between your models:
Result.php model
public function class(){
return $this->belongsTo('Class::class');
}
Class.php model
public function results(){
return $this->hasMany('Result::class');
}
public function student(){
return $this->belongsTo('Student::class');
}
Student.php model
public function classes(){
return $this->hasMany('Class::class');
}
Then, you can use some queries. For your first example student->class(2013)->result(jan):
$results = Student::find($student_id)->classes()->where('year', '2013')->results()->where('month', 'jan')->get();
Also, if you will need to search classes by year and results by month a lot of times, you can easily add some methods like this in your models:
Student.php model
public function classesByYear($year){
return $this->classes()->where('year', $year)->get();
}
Class.php model
public function resultsByMonth($month){
return $this->results()->where('month', $month)->get();
}
So the previous example will become:
$results = Student::find($student_id)->classesByYear('2013')->resultsByMonth('jan');
EDIT: Your tables are using a custom identifier, so you must implement protected $primaryKey = 'custom_id'; on each model.

Laravel 4 Eloquent Relationship

I have 2 tables:
`tasks {id, task_name, status_id}`
`statuses {id, status_name }`
A task can only have one status at a time eg Pending, Active, Completed, Aborted, etc
This is what I have so far.
class Task extends Eloquent
{
public function status
{
return $this->hasOne('Status', 'id');
}
}
When I try $task->status->status_name, I get 'Trying to get a property of non object' error!
My Status class looks like this
class Status extends Eloquent
{
protected $table = 'statuses';
}
Could you explain why I'm having this error?
Since you got the foreign key in Task table, you can use belongsTo and Eloquent will look for column status_id in tasks table for you.
public function status
{
return $this->belongsTo('Status');
}
for more information http://laravel.com/docs/4.2/eloquent in One to One section
For the error Trying to get a property of non object
Please make sure there is a value in column status_id on table tasks on very record.

How to Call function on laravel pivot model

I was trying to get the attendance of each user in a course where the users and the courses have many to many relationship and the attendance table is based on the relationship.
But i couldn't call $user->pivot->attendance(); directly i had to call for the pivot object it self so i can call a function to it as you can see in the answer
The Following is a sample of my Database scheme
I need to run
$users=$course->users;
foreach($users as $user)
{
$user->pivot->attendance(); //error in this line
}
this line gives and error
Call to undefined method
Illuminate\Database\Query\Builder::attendance()
users
id
name
etc
courses
id
name
startDate
endDate
course_user
id
course_id
user_id
payment
regDate
status
userattendance
id
course_user_id
time
inOrOut
And here is the CourseUserPivot Class
class CourseUserPivot extends Eloquent {
protected $table ="course_user";
public function course()
{
return $this->belongsTo('Course');
}
public function user()
{
return $this->belongsTo('User');
}
public function attendance()
{
return $this->hasMany('Userattendance','course_user_id');
}
}
P.S: $user->pivot->payment works and displays the attribute but i cant call methods
You can't use $user->pivot->attendance(); as this calls attendance on the user object not on the pivot object
You will have to fetch the pivot object then call the function like so
CourseUserPivot::find($user->pivot->id)->attendance();
make user that where you used withPivot() function you include the id in the array
like in
class Course{
...
...
public function users()
{
return $this->belongsToMany('Candidate')
->withPivot('id',
'summary',
'status',
'payment'
);
}
}
I was trying to accomplish the same thing, and I was having trouble getting it to work like I wanted until I included the 'id' column of my custom pivot model in the withPivot() method.
For some context, my database structure is:
User table
id
Items Table
id
Sets table (defines a set and the items that can be in an instance of it)
id
Set_User table (Custom Pivot Model, instances of a set created by a user)
id, set_id, user_id
Items_SetUser table (items that belong to a set instance that belong to a user)
item_id, setuser_id
I do this in the controller to get all the sets that user has made an instance of, with custom pivot table data:
$user_sets = \Auth::user()->sets()->withPivot('id', 'name')->get();
And then in blade I can access the items associated with each set instance like this:
$set->pivot->set_items()->count()
Without including 'id' in the call to withPivot(), the above is not possible. Note that set_items() is a method on the custom pivot model that defines a relationship with the items table.

laravel eloquent relationships queries

I have two tables 1)users
{ id, password }
2)expertise { id, expertise}
the relationship I have is
Models
Expertise.php
function User()
{
$this->hasOne('Expertise');
}
User.php
function Expertise()
{
$this->hasOne('User');
}
So how can I query using Eloquent to get the first 10 users with a certain expertise?
I want to join users.id = expertise.id and get the first 10 people with a specified expertise (Where clause).
Beginner to laravel, I've checked other sources but was not successful
Right now you are having a problem with the way that you modeled your data. If you have a one-to-one relationship the best practice to model it is to have one entity store the id of the other. The Laravel convention for this is to have a column named <model>_id:
Users
| id | password |
Expertises
| id | expertise | user_id |
Then in your models you can do this:
Models
Expertise.php
class Expertise extends Eloquent
{
public function User()
{
// because expertise has a column user_id
// expertise belongs to user
return $this->belongsTo('User');
}
}
User.php
class User extends Eloquent
{
public function Expertise()
{
// because expertise is the one with the column
// user_id, user has one expertise
return $this->hasOne('Expertise');
}
}
The Query
After you have all this set up, to be able to query the first 10 users with a certain expertise you can do this.
$users = User::whereHas('Expertise', function($q)
{
$q->where('expertise', '=', <expertise you are looking for>)
})
->take(10)
->get();
To get a further reading in querying relationships in Laravel please take a look at this:
Laravel - Querying Relationships
Keep in mind
keep in mind that the tables name must be plural, if not then you should specify the name of the table inside the model:
protected $table = 'expertise';

Categories