I am new to laravel and using Eloquent ORM. Now the issue is I am trying to get records using relationships with with() function. Now this issue is that eloquent generates and applies the right query but does not returns any result. But if I test the same generated query on mysql than it works fine.
Following are the two tables that are involved in this:
Properties:
id , name , locality_id
Localities:
id, name , type , adjoining
Now the relationship between above mentioned tables is one to many relationship.
Property Model:
protected $table = 'properties';
protected $guarded = array('id');
public function localityAreaAndCity() {
return $this->belongsTo('Locality','locality_id')
->leftjoin('localities as ls', function($join)
{
$join->on('localities.id', '=', 'ls.adjoining')
->where('localities.type', '=','area');
});
->select(array('localities.name as localityPrimaryName',
'localities.type as localityPrimaryType',
'ls.name as localitySecondaryName',
'ls.type as localitySecondaryType'));
}
Locality Model:
public $timestamps = false;
protected $table = 'localities';
protected $guarded = array('id');
public function properties()
{
return $this->hasMany('Property');
}
Eloquent Query:
$properties = Property::with('localityAreaAndCity')->get();
DB::getQueryLog() Result:
select `localities`.`name` as `localityPrimaryName`, `localities`.`type` as `localityPrimaryType`, `ls`.`name` as `localitySecondaryName`, `ls`.`type` as `localitySecondaryType` from `localities` left join `localities` as `ls` on `localities`.`id` = `ls`.`adjoining` and `localities`.`type` = ? where `localities`.`id` in (?, ?, ?, ?, ?)
Know if I use the above mentioned query in mysql then it returns data but using with Eloquent ORM it returns NULL.
Please Help..
Just added locality_id to the select and it worked. Actually eloquent works like you have 2 arrays with related items and you want to match them - if there's no foreign key in one of them, that points to the primary key in the other one, then you can't do that. Eloquent does exactly the same.
You must always select both keys of the relation (most likely PK on one model and FK on the other one).
Related
This question already has answers here:
laravel BelongsTo relationship with different databases not working
(13 answers)
Closed 2 years ago.
I have two models using different tables on two different connections, User and UserInfo.
User has a UserInfo hasMany relation:
public function userInfo()
{
return $this->hasMany('path\to\UserInfo','User_ID');
}
and UserInfo has a User belongsTo relation:
public function user()
{
return $this->belongsTo('anotherpath\to\User', 'User_ID', 'User_ID');
}
I would like to find the first user with more than one UserInfo, however I believe that because they are on different tables on the database I am having issues.
This call
$patient = Patient::with('UserInfo')
->withCount('UserInfo')
->having('UserInfo_count', '>', 1)
->first();
Works as expected for other relations and is what I am trying to achieve, but not with this one. The difference being that the two models are on different tables. When I call this in Tinker, I get the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]:
Base table or view not found: 1146 Table '(TableName).UserInfo'
doesn't exist (SQL: select `User`.*, (select count(*) from `UserInfo`
where `User`.`User_ID` = `eob`.`User_ID`) as `UserInfo_count `User`
having `UserInfo_count` > 1 limit 1)'
Any ideas? I'm very new to eloquent and Laravel in general, sorry if I've gotten any terminology wrong or am missing something simple. Thanks!
maybe your table names are not defined properly as standard. so you can use table property to bind table name in model.
what is the standard to define table name.
Illuminate/Database/Eloquent/Model.php
/**
* Get the table associated with the model.
*
* #return string
*/
public function getTable()
{
if (isset($this->table)) {
return $this->table;
}
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this))));
}
Example
Table Model
users User
user_profiles UserProfile
Alternative
in your UserInfo model
protected $table = 'your table name';
In more thing you don't need to add with() method and with withCount() method.
$patient = Patient::withCount('UserInfo')->having('UserInfo_count', '>', 1)->first();
I have model Users
class Users extends Model implements AuthenticatableContract
{
use Authenticatable,HasApiTokens;
protected $primaryKey = 'user_id';
protected $fillable = ['email','password','email_token', 'verified'];
public function user_info()
{
return $this->hasOne('App\Models\UserInfo','user_id');
}
}
In controller I want to query all users where relation table column "kind" equal to 1 .
So Users table has user_info_id(one to one) . User_info has kind_id.
I want query users by relation table column kind_id.
I can do it by left join . But How can I do it with relation?
I tried like this but it doesnt work
$univer_list = Users::with('user_info')->where('is_university',1)
->where('kind_id',1);
You can use whereHas to query based on the existence of a relationship:
$univer_list = Users::where('is_university', 1)
->whereHas('user_info', function ($query) {
$query->where('kind_id', 1);
})->get();
Get only Users, which should be named User, that have a relationship with UserInfo where kind_id is equal to 1.
Laravel 6.x Docs - Eloquent - Relationships - Querying Relationship Existence
You can make this condition in with
$univer_list = Users::with(['user_info' => function($q) {
$q->where('kind_id', 1);
}])->where('is_university', 1)->get();
I assumed that your is_university exists in users table and your kind_id column exists in user_info table.
I've just started using Eloquent ORM (Without Laravel) and I am having issues with the many to many relationships.
I have a table where I store Families (Article categories), another one for the Articles, and a third one as a "pivot". I want to be able to get all the articles a Family has, and all the families an article belongs to. So I have coded this models.
Families
class Families extends Model {
public $table = 'Families';
public function Articles() {
return $this->belongsToMany('Articles', 'articles_families', 'families_id', 'articles_id');
}
}
Articles
class Articles extends Model {
public $table = 'Articles';
public function Families() {
return $this->belongsToMany('Families', null, 'articles_id', 'families_id');
}
}
Then I am trying to retrieve the data like this:
$families = Families::all();
echo $families[1]->Articles;
However, it just returns an empty array, when it should return a couple of articles. I have tripled checked that all the values are correct in the three tables. If I echo the Eloquent query debugger I can see that it is looking for a null value and I'm pretty sure that's the problem, but I don't quite know how to fix it. Here:
{"query":"select * from `Families`","bindings":[],"time":49.13},{"query":"select `Articles`.*, `articles_families`.`families_id` as `pivot_families_id`, `articles_families`.`articles_id` as `pivot_articles_id` from `Articles` inner join `articles_families` on `Articles`.`id` = `articles_families`.`articles_id` where `articles_families`.`families_id` is null","bindings":[],"time":38.93}
The null value is at the end of the last query.
I just found the solution myself. As my primary key columns are called Id, and Eloquent by default assumes the primary key is called id, I needed to override that by adding a class property protected $primaryKey = "Id"; and it now retrieves the data properly.
I am learning relationships in Laravel php framework and I am trying to build this query
SELECT * FROM users u INNER JOIN link_to_stores lts ON u.id=lts.user_id INNER JOIN stores s ON lts.store_id=s.store_id WHERE lts.privilege = 'Owner'
I built this in Model
Link_to_store.php
public function store()
{
return $this->belongsTo('App\Store');
}
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function store_links()
{
return $this->hasMany('App\Link_to_store');
}
Store.php
public function user_links()
{
return $this->hasMany('App\Link_to_store');
}
I tried this query but this only joins user and link_to_store table
$personal_stores = Auth::user()->store_links->where('privilege','=','Owner');
Now I am confused how to join store table too. Can anyone help with this?
Schema is like this
Stores Table
store_id store_name
Users Table
id name
Link_to_stores Table
id store_id user_id privilege
I suppose store_links is actually a pivot table. In this case, you can use belongsToMany(), this will automatically take care of the pivot table.
To do this, in your User model you change the store function to this:
function stores() {
return $this->belongsToMany('App\Store', 'store_links', 'user_id', 'store_id')->withPivot('privilege');
}
Because the primary key of stores is not id, you will have to define this in you Store model with the following line:
protected $primaryKey = 'store_id';
Now to get the stores for a user, you simply call
$stores = Auth::user->stores()->wherePivot('privilege', 'Owner')->get();
I am learning relationships in Laravel php framework and I am trying to build this query
SELECT * FROM users u INNER JOIN link_to_stores lts ON u.id=lts.user_id INNER JOIN stores s ON lts.store_id=s.store_id WHERE lts.privilege = 'Owner'
You are trying to do a join here. You can do a join like this:
$stores = User::join('link_to_stores as lts', 'users.id', '=', 'lts.user_id')->join('stores as s', 'lts.store_id', '=', 's.id')->where('lts.privilege', 'Owner')->get();
But like Jerodev pointed out, it seems like Many to Many relationship might make more sense in your case. The difference is that relationship will actually execute 2 queries (1 for original model, 1 for relationship). It will then attach the related models to the original model (which is extremely handy).
So the belongsToMany relationship is a many-to-many relationship so a pivot table is required
Example we have a users table and a roles table and a user_roles pivot table.
The pivot table has two columns, user_id, foo_id... foo_id referring to the id in roles table.
So to do this we write the following in the user eloquent model:
return $this->belongsToMany('Role', 'user_roles', 'user_id', 'foo_id');
Now this looks for an id field in users table and joins it with the user_id field in the user_roles table.
Issue is I want to specify a different field, other than id to join on in the users table. For example I have bar_id in the users table that I want to use as the local key to join with user_id
From laravel's documentation, it is not clear on how to do this. In other relationships like hasMany and belongsTo we can specify local key and foriegn key but not in here for some reason.
I want the local key on the users table to be bar_id instead of just id.
How can I do this?
Update:
as of Laravel 5.5 onwards it is possible with generic relation method, as mentioned by #cyberfly below:
public function categories()
{
return $this->belongsToMany(
Category::class,
'service_categories',
'service_id',
'category_id',
'uuid', // new in 5.5
'uuid' // new in 5.5
);
}
for reference, previous method:
I assume id is the primary key on your User model, so there is no way to do this with Eloquent methods, because belongsToMany uses $model->getKey() to get that key.
So you need to create custom relation extending belongsToMany that will do what you need.
A quick guess you could try: (not tested, but won't work with eager loading for sure)
// User model
protected function setPrimaryKey($key)
{
$this->primaryKey = $key;
}
public function roles()
{
$this->setPrimaryKey('desiredUserColumn');
$relation = $this->belongsToMany('Role', 'user_roles', 'user_id', 'foo_id');
$this->setPrimaryKey('id');
return $relation;
}
On Laravel 5.5 and above,
public function categories()
{
return $this->belongsToMany(Category::class,'service_categories','service_id','category_id', 'uuid', 'uuid');
}
From the source code:
public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
$parentKey = null, $relatedKey = null, $relation = null)
{}
This is a recently added feature. I had to upgrade to 4.1 because I was also looking for this.
From the API documentation:
public BelongsToMany belongsToMany(string $related, string $table = null, string $foreignKey = null, string $otherKey = null, string $relation = null)
The $otherKey and $relation parameters were added in 4.1. Using the $foreignKey and $otherKey parameters allows you to specify the keys on both sides of the relation.
The best way is set the primary key.
class Table extends Eloquent {
protected $table = 'table_name';
protected $primaryKey = 'local_key';
belongsToMany allows to define the name of the fields that are going to store che keys in the pivot table but the method insert always the primary key values into these fields.
You have to:
define in the method belongsToMany the table and the columns;
then using protected $primaryKey = 'local_key'; you can choose which value store.
I recently went through the same problem where I needed to have an associated table that used ID's to link two tables together that were not Primary Keys. Basically what I did was create a copy of my model that models the pivot table and set the Primary Key to the value that I wanted it to use. I tried creating a model instance, settings the primary key and then passing that to the relation but Laravel was not respecting the primary key I had set ( using the ->setPrimaryKey() method above ).
Making a copy of the model and setting the primary key feels a little bit 'hackish' but in the end it works as it should and since Pivot table models are generally very small I don't see it causing any problems in the future.
Would love to see a third key option available in the next release of Laravel that lets you get more specific with your linking.