Laravel 5 querying relationship models - php

I am trying to query one of my model relationships in a timesheet system I am building. I have the following models setup:
User can have many timesheets and can belong to many employee types
Timesheet can have many rows
The models are setup like so:
User Model:
<?php namespace App\Models\User;
....
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
/**
* The employee types that belong to the user.
*
* #return Object
*/
public function employeeTypes()
{
return $this->belongsToMany('App\Models\User\EmployeeType')->withTimestamps();
}
Timesheet Model:
<?php namespace App\Models\Timesheet;
....
class Timesheet extends Model
{
/**
* The user that owns the timesheet.
*
* #return Object
*/
public function user()
{
return $this->belongsTo('App\Models\User\User');
}
My question is, how can I query the User relationship and get timesheets by employee_type. This means I need to access timesheet table, then get users associated with the timesheet and then get the users by an employee type that I specify.
I have tried the following...
$timesheets->with('user')->whereHas('employeeTypes', function ($query) use ($request) {
$query->where('name', 'my_employee_type');
});
...but it gives an error...
Error:
Call to undefined method Illuminate\Database\Query\Builder::employeeTypes()
Does anyone know how I achieve this?

Timesheets doesn't have that method. That method (relationship) exists on User, not Timesheet.
You can try something like this and see if it would work.
$timesheets = Timesheet::whereHas('user.employeeTypes', function ($q) {
$q->where('name', 'something');
})->get();

Related

Query a model using a relationship

I have a model called unit that has this relationship
/**
* Get the users associated with the unit
*/
public function users()
{
return $this->hasMany('App\Models\User\UserData');
}
In the UserData model there is a column called user_id which I am trying to put in my condition in my query. I am trying to do a query like this
Unit::where('user_id', Auth::id())->first()
but there is no user_id column in the Unit table, only though the users relationship
Ended up doing this
Unit::whereHas('users', function($q) {
$q->where('user_id', Auth::id());
})->first();

Property [users] does not exist on this collection instance

This error has been posted here several times but I'm encountering something a little different. I have two Tables named users and user_type. And users uses a foreign key from user_type. I have to fetch all users and their types, I'm using Laravel's Eloquent ORM to define relationships, this is a one to one relation.
Users Model:
/**
* Get the user type that user has.
*/
public function users(){
return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
UserType Model:
/**
* The primary key associated with the table.
*
* #var string
*/
protected $primaryKey = 'ut_id';
/**
* Get the user associated with the user type.
*/
public function users(){
return $this->hasOne('App\Models\Users', 'user_type_id', $this->primaryKey);
}
Fetching Controller:
$users = Users::all()->users;
According to Laravel ORM one-to-one I can access this method as a property, but it's showing me the defined error. I've also tried to access it as a method but it's saying:
Method Illuminate\Database\Eloquent\Collection::users does not exist.
I've also tried to fetch them by join() but it's returning only a few users, I don't know why:
$users = Users::where('id', '>', '0')
->join('user_type', 'user_type.ut_id', '=', 'users.id')
->select([
'user_type.ut_name',
'users.*'
])->get();
Can someone tell me what I'm doing wrong?
P.s: I just want to show all the users with their respective types
You had missed the exact foreign key between your users table and usertypes table.
First, you defined the that the foreign key of your user table is 'ut_id' base of what you had in your belongsTo relationship. On this one
/**
* Get the user type that user has.
*/
public function users(){
return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
Second is that, in your user type model, you used a foreign key to user table named 'user_type_id', which is at first you named it as 'ut_id' in your users table. On this one
/**
* The primary key associated with the table.
*
* #var string
*/
protected $primaryKey = 'ut_id';
/**
* Get the user associated with the user type.
*/
public function users(){
return $this->hasOne('App\Models\Users', 'user_type_id', $primaryKey);
}
You have to match this foreign keys you used to solve your problem.
Now, to fetch your all user with their types, your query should look like this.
$users = Users::with('users')->get();
assuming that your user table has this relationship
public function users(){
return $this->belongsTo('App\Models\UserType', 'ut_id', 'id');
}
and your user types model has this relationshio
public function users(){
return $this->hasOne('App\Models\Users', 'ut_id', $this->primaryKey);
}
in User model
public function type(){
return $this->hasOne(UserType::class, 'id');
}
in UserType Model
public function users(){
return $this->belongsToMany(User::class, 'id');
}
Your relations seem to be wrong.
Users links to UserType with id to ut_id, but userType links to User with id to user_type_id
I'm pretty sure that it should be this for userTypes
/**
* Get the user associated with the user type.
*/
public function users(){
return $this->hasMany('App\Models\Users', 'id', 'user_type_id');
}
and then this for Users
public function userTypes(){
return $this->belongsTo('App\Models\UserType', 'user_type_id', 'id');
}
Then you can eager load for all the results you want...
$users = Users::where('id', '>', '0')
->with('userTypes')
->get();

How to use orderby on element that was joined with Laravel Eloquent method WITH

The problem is that the query can't find the specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...), that should been joined with the method WITH from Laravel Eloquent. Any ideas how to solve it?
My code:
//SpecificModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class SpecificModel extends Model {
protected $guard_name = 'web';
protected $table = 'SpecificTable';
protected $guarded = ['id'];
public function specificMethod(){
return $this->belongsTo('App\Models\AnotherModel','AnotherModel_id');
}
}
//AnotherModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AnotherModel extends Model {
protected $guard_name = 'web';
protected $table = 'AnotherTable';
protected $guarded = ['id'];
}
//Query method
$model = app('App\Models\SpecificModel');
$query = $model::with('specificMethod:id,title');
$query = $query->orderBy('specific_method.title','desc');
return $query->get();
//Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'"specific_method.title"' in 'order clause' (SQL: select * from
`SpecificModel` where `SpecificModel`.`deleted_at` is null order by
`specific_method`.`title` desc)
This happens because the belongsTo relationship does not execute a join query as you expect it to (as you can see from the error you get). It executes another query to get the related model(s). As such you will not be able to order the original model by related models columns.
Basically, 2 queries happen:
Fetch the original model with SELECT * from originalModel ...*
Fetch the related models with SELECT * from relatedModel where in id (originalModelForeignKeys)
Then Laravel does some magic and attaches the models from the 2nd query to the correct models from the first query.
You will need to perform an actual join to be able to order the way you want it to.

Eloquent hasMany Not Returning Any Results

I have a noob question about how Eloquent generates SQL for the following.
DB:
Customer Table:
ID (Public Key)
... (Some general columns)
Group Table:
ID (Public Key)
FK_CUSTOMER_ID (Foreign Key)
... (Some general columns)
I have the following code in Customer Model:
public function groups()
{
return $this->hasMany(Group::class, 'fk_customer_id');
}
I am trying to get all groups (in the groups function above), that I can narrow down the groups later, to a particular customer.
The above code generates the following SQL (which results an empty result set, which is understandable by looking at the SQL). I have no idea, why the where clause (see the SQL below) gets generated, does not makes much sense.
select * from `group` where `group`.`fk_customer_id` is null and `group`.`fk_customer_id` is not null limit 1
I would like the following SQL to be generated :
select * from `group`
also, how to get the following SQL generated (If I need to select groups based on customer_id, I believe I'll need to add some where clause somehow)
select * from `group` where `group`.`fk_customer_id`= SOME_VALUE
Thanks!
--- Customer Model
<?php
namespace App;
use App\Role;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
/**
* Get ALL groups for the customer.
*/
public function groups()
{
return $this->hasMany(Group::class, 'fk_customer_id');
}
/**
* Get ONE group for the customer.
*/
public function group($groupId)
{
return $this->hasMany(Group::class, 'fk_customer_id')->where('id', $groupId);
}
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'customer';
}
Group Model
<?php
namespace App;
use App\Group;
use App\Customer;
use Illuminate\Database\Eloquent\Model;
class Group extends Model
{
/**
* Get ONE customer for the group.
*/
public function customer()
{
return $this->belongsTo(Customer::class, 'fk_customer_id');
}
/**
* Get ONE directory configuration for the group.
*/
public function directoryConfiguration()
{
return $this->belongsTo(DirectoryConfiguration::class, 'fk_directory_configuration_id');
}
/**
* Get ONE user for the group.
*/
public function user($userId)
{
return $this->hasMany(User::class, 'fk_role_id')->where('user_id', $userId);
}
/**
* Get ALL user for the group.
*/
public function users()
{
return $this->hasMany(User::class, 'fk_role_id');
}
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'group';
}

Laravel distinct result

I'm building a PM system, and I have a problem.
This is my PM table:
id, user_id, to, content
Now, in my inbox page I'm fetching all the users that sent me a message.
$pms = DB::table('pm')->select('user_id')->distinct()->where('to', Auth::id())->get();
The problem is if I add more columns to the select method, it won`t be distinct anymore..
You can easily do that using Eloquent and its whereHas() method.
First, define models and the relation in your model:
class Message extends Model {
protected $table = 'pm';
}
class User extends Model {
public function sent_messages() {
return $this->hasMany(Message::class);
}
}
Now, fetch all users that have a related Message models where to column matches your ID:
$usersThatSentMeMessages = User::whereHas('sent_messages', function($query) {
$query->where('to', Auth::id());
});

Categories