Laravel retrieve model with best match query - php

I have three tables seekers, jobs and skills. a user has many skills and also a job has many skills so i have these relations,
class Job extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'jobs_skills', 'job_id', 'skill_id');
}
}
and
class Seeker extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id');
}
}
and
class skills extends Model
{
public function jobs()
{
return $this->belongsToMany(Job::class, 'jobs_skills', 'skill_id', 'job_id')->withPivot('level1');
}
public function seekers()
{
return $this->belongsToMany(Seeker::class, 'seekers_skills', 'skill_id', 'seeker_id');
}
}
I wrote this query to get the related jobs to the seekers based on skills
public function relatedJobs()
{
$skills = $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id')->pluck('skill_id');
$job_ids = DB::table('jobs_skills')->whereIn('skill_id', $skills)->pluck('job_id');
$jobs = Job::whereIn('id', $job_ids)->with('skills')->get()->sortBy(function ($job) {
return $job->skills2->groupBy('pivot.job_id')->count();
});
return $jobs;
}
This function return the related jobs but not sorted with the best matched with the skills of the seeker
any help ?
btw I need the return to be an object of Job

Related

How can I get the combined query for two hasManyThrough

I have two relationships on the users' table:
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
and
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
How can I merge both queries into one?
If you want to have the records for both relationships, this is gonna work:
class Entity extends Model
{
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
}
$records = $entity->tokens_records
->merge($entity->websites_records);

How to get a single element with a model with relations Laravel

I have three models Driver Worker and Group. The models are like this:
class Group extends Model
{
.....
public function driver()
{
return $this->belongsTo('App\Driver');
}
public function worker()
{
return $this->belongsTo('App\Worker');
}
}
Drivers
class Driver extends Model
{
public function groups()
{
return $this->hasMany('App\Group');
}
}
Worker
class Workerextends Model
{
public function groups()
{
return $this->hasMany('App\Group');
}
}
But how can I get a single element with the relationships that I have defined in my models. I have this in my controller but I only get the first element of the table. I need to get the element with the id and also the relations.
public function show(Group $group)
{
return $group->with(['worker','driver'])->first();
}
try this
public function show(Group $group,$id){
$group = $group->where('id',$id)->with(['worker','driver'])->first();
return $group
}
in your Route
Route::get('/group/show/{id}', 'GroupController#show')->name('group.show');

Laravel Polymorphic Relations returns NULL

I've read many posts about this issue but none of them works for me. I have a 'ISA' relationship in my database. A person can be either a Patient or a Nurse:
class Person extends Model
{
protected $table = 'persons';
public function commentable()
{
return $this->morphTo();
}
}
class Patient extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
class Nurse extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
This is my tables and the data inside them:
And this is my Route:
Route::get('person', function () {
$person = Person::find(1)->commentable();
return json_decode(json_encode($person), true);
});
I get an empty array!
You have to access the relationship as a property:
$person = Person::find(1)->commentable;

Laravel 5.6: Relationship with multiple tables and conditions

Consider I'm a hotel owner, and I have to know the bookings related to my hotel.
I need all bookings that which associated with my hotel.
Is there any eloquent functions to join? How?
How to get bookings of a hotel with owner(userid) with using eloquent relationship functions?
Hotel.php
class Hotel extends Model
{
public function userId(){
return $this->belongsTo(User::class,'user_id');
}
public function rooms(){
return $this->hasMany(Room::class,'hotel_id','id');
}
}
Room.php
class Room extends Model
{
public function hotelId(){
return $this->belongsTo(Hotel::class,'hotel_id');
}
public function bookings(){
return $this->hasMany(Booking::class,'room_id','id');
}
}
User.php
class User extends Model
{
public function hotels(){
return $this->hasMany(Hotel::class,'user_id','id');
}
}
Below code add in your respective controller.
$user = \App\User::find(Auth::id());
$hotels = $user->hotels;
foreach ($hotels as $hotel) {
$rooms = $hotel->rooms;
foreach ($rooms as $room) {
print_r($room->bookings);
}
}
Define the relationship with user in model
class Booking extends Model{
/**
* Get the user that owns the booking.
*/
public function owner()
{
return $this->belongsTo(App\User::class,'user_id,'id');
}
}
Then in your controller
$bookings = Booking::with('owner')->all(); // or get() or whatever to get
the booking list
foreach($bookings as $booking){
echo $booking->user->name; // you have access to owner fields
}
Finally i found this.
Booking model
public function room( )
{ return $this->belongsTo('App\room');
}
public function hotel( )
{
return $this->room()->with(['hotel'=> function($query){
$query->with('user');
}]);
}
BookingController
public function owner(Booking $booking ) {
return $booking->with('hotel')->get()
Next thing is Filter with owner

Laravel + Eloquent : combine 2 tables in one query

I am trying to perform a query with Eloquent where I can get all the courses of an user.
Users have their courses because they are in some grade. So all courses of this grade will be included.
$user->grade->courses
Now I'd like to include some courses that are not in the user's grade thanks to the pivot table : users_courses. But I don't know how to query all tables at once using Eloquent.
EDIT : Here is what have
class Users extends Eloquent {
public function courses()
{
return $this->belongsToMany('Courses', 'users_courses', 'student_id', 'course_id');
}
public function grade()
{
return $this->belongsTo('Grades', 'grade_id');
}
}
class Courses extends Eloquent {
public function grade()
{
return $this->belongsTo('Grades');
}
public function users()
{
return $this->belongsToMany('Users', 'users_courses', 'student_id', 'course_id');
}
}
class Grades extends Eloquent {
public function courses()
{
return $this->hasMany('Courses', 'grade_id');
}
public function users()
{
return $this->hasMany('Users', 'grade_id');
}
}

Categories