laravel hasMany on two columns - php

hi im using laravel 7 i've the relation in the user model called get_journal_entry_lines
public function get_journal_entry_lines()
{
return $this->hasMany('App\Models\Journal_entry_line','user_id','id')->orderBy('date','asc');
}
sometimes the user in column called user_id and sometimes on other column called partner_id
so is it possable to do something like this in laravel
public function get_journal_entry_lines()
{
return $this->hasMany('App\Models\Journal_entry_line','user_id','id')->orderBy('date','asc')->orWhere('partner_id','=','id');
}
get Journal_entry_line from columns user_id and partner_id at same relation .

Can't you just create 2 different relations?
For example, you could use the one below for your users:
public function get_journal_entry_lines_for_users()
{
return $this->hasMany('App\Models\Journal_entry_line','user_id','id')->orderBy('date','asc')->orWhere('partner_id','=','id');
}
and this one for your partners:
public function get_journal_entry_lines_for_partners()
{
return $this->hasMany('App\Models\Journal_entry_line','partner_id','id')->orderBy('date','asc');
}

Related

How to define three table relationship in models in laravel 5.6

In my project there are three table like (teacher, attendance,user) and i have define the relationship in there model. in teacher table relation with both attendance and user but user has no relationship with attendance when i want to fetch data it show an error like Property [attendance_status] does not exist on this collection instance. how can i solve this problems.
This is User model
public function teacher() {
return $this->hasOne('App\Teacher');
}
this is Teacher model
public function attendance() {
return $this->hasMany(Attendance::class,'teacher_id','user_id');
}
this is attendance model
public function teacher() {
return $this->belongsTo(Teacher::class,'teacher_id','user_id');
}
and in my attendance controller I define like this
$staffAttendances = Teacher::with(['attendance','user'])->get();
foreach ($staffAttendances as $attd) {
echo $attd->user->first_name .'<br>';
echo $attd->designation.'<br>';
echo $attd->attendance->attendance_status.'<br>';
}
Try
$attd->attendance[0]->attendance_status

LARAVEL ELOQUENT - I want to add column from user table to a relation table

I am not much familiar with eloquent orm in laravel
I have 3 tables they are
-- leads
|
Lead_appointments
and users table
since lead_appointments belongs to leads references id on leads by lead_id
the leads_appointments has a column called created_by with user's id in it
I am trying to query user's name and email along with the result as another column when query using eloquent
Lead Model
class Leads extends Model
{
public function appointments()
{
return $this->hasMany('App\Models\LeadsAppointments', 'lead_id');
}
}
My eloquent query in controller
return $this->lead->with('appointments')->find($id);
the result is like this
In under appointments i also want user email and name along with created by in it
But I couldn't figure it out
Add a relation to LeadAppointment model like this:
class LeadAppointment extends Model
{
public function users()
{
return $this->belongsTo('App\Models\User', 'created_by');
}
}
and change leads model like this:
class Leads extends Model
{
public function appointments()
{
return $this->hasMany('App\Models\LeadsAppointments', 'lead_id')->with('users');
}
}

Laravel 5.4 : Many to many pivot table records

I have User and Project models.
The relationship is many to many.
public function projects()
{
return $this->belongsToMany('App\Project')->withPivot('id');;
}
And in Project Model I have :
public function users()
{
return $this->belongsToMany('App\User')->withPivot('id');;
}
Now what I am doing :
$projectsAsFreelancer = App\Project::where('assignedTo',$id)->get();
What i want is to get the username from the users table of the user, Who have posted the project. The Project means projects table have the forign_key->employeer_id.
Currently $project->pivot->username gives me error.
pivot table name is project_user.
What i am missing ?
$project->pivot->username requires the username field in your pivot table. ->withPivot('id'); is only required if you wish to get id column of the pivot table, $project->pivot->id wil give the value.
If you want to get username from users table you don't require pivot.
$projectsAsFreelancer = App\Project::where('assignedTo',$id)->get();
foreach($projectsAsFreelancer as $project)
{
echo $project->name;
foreach($project->users as $user)
{
echo $user->username;
}
}
Don't use get(). It could not fetch the relations. You could use first() or find() (if you want to select with primary key)
$projectsAsFreelancer = App\Project::where('assignedTo',$id)->first();
foreach ($projectsAsFreelancer->users as $user) {
//var_dump($user->username);
}

Laravel belongsToMany not returning results

I have the following schema set up:
users:
id
departments:
id
department_user:
id
department_id
user_id
I also have the following relationships set up:
User Model
public function departments()
{
return $this->belongsToMany('App\Resources\Eloquent\Models\Department', 'department_users');
}
Department Model
public function users()
{
return $this->belongsToMany(User::class, 'department_users');
}
For some reason, when I am trying to access through the user model $user->departments, it doesn't work - but $department->users does.
Outputting the eloquent query is as follows:
select `departments`.*, `department_users`.`user_id` as `pivot_user_id`, `department_users`.`department_id` as `pivot_department_id` from `departments` inner join `department_users` on `departments`.`id` = `department_users`.`department_id` where `department_users`.`user_id` is null
I can't seem to figure out why it is looking to see if department_users.user_id is null, when it should be looking for the user's id.
Any ideas?
Why don't you set up your models like it is suggested in the documentation here:
So your models would look something like this:
User Model
public function departments()
{
return $this->belongsToMany('path\to\your\model\Department');
}
Department Model
public function users()
{
return $this->belongsToMany(path\to\your\model\User);
}
Eloquent will join the two related model names in alphabetical order.So you don't need extra arguments when defining your relationship and Laravel also by default, makes model keys present on the pivot object. And then you can do something like this:
$department = path\to\your\model\Department::find(1);
foreach ($department->users as $user) {
echo $user;
}
For some reason, if I make the relationship the following - it works.
return $this->belongsToMany(Department::class, 'department_users')->orWhere('department_users.user_id', $this->id);
If anyone knows why, please let me know

Retrieving a single colum from a pivot table - Laravel 5

I am using a pivot table genre_user to relate user to genre.
table contains the following fields
id
user_id
genre_id
Following are the model definitions
User.php
public function genres() {
return $this->belongsToMany('App\Genre');
}
Genre.php
public function artists() {
return $this->belongsToMany('App\User');
}
I am getting the results as a collection when I use the following code
$user = auth()->user();
dd($user->genres);
I want to show the selected genres in a dropdown field of genres. Is it possible to get only the current users genre_id as an array from the pivot table without using a foreach loop.
I think what will help you achieve this behavior is the lists() method.
Try something like
$user_genres = auth()->user()->genres()->lists('name','id');
If you are using Forms & HTML package you can just do
{!! Form::select('genres',$user_genres,null) !!}
And here is your dropdown
More info here (scroll down to "Retrieving A List Of Column Values")
A user should have one genre.
Therefore, your models should contain the following relations:
User.php
public function genre() {
return $this->hasOne('App\Genre');
}
Genre.php
public function artists() {
return $this->belongsToMany('App\User');
}

Categories