A Venue hasMany Subscription
A Subscription belongsToMany User
How can I retrieve all User from Venue through Subscription?
I imagine something like:
$venue->subscribers // Return all `User`
My database scheme: http://www.laravelsd.com/share/AAjuYS
I have tried
class Venue {
public function subscribers() {
return $this->hasManyThrough('App\User', 'App\Subscription');
}
}
Fails with error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.subscription_id' in 'on clause' (SQL: select `users`.*, `subscriptions`.`venue_id` from `users` inner join `subscriptions` on `subscriptions`.`id` = `users`.`subscription_id` where `users`.`deleted_at` is null and `subscriptions`.`venue_id` = 1)
// In Venue class
public function subscribers()
{
return $this->hasManyThrough('App\User', 'App\Subscription');
}
You can read about it in the documentation, it's pretty straight-forward: http://laravel.com/docs/5.1/eloquent-relationships#Has-Many-Through
Related
I store the data correctly but when I try to get a data in edit function it shows me this
error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'staff.staff_id' in 'field list' (SQL: select users.*, staff.staff_id as pivot_staff_id, staff.user_id as pivot_user_id, staff.staff_type as pivot_staff_type, staff.role as pivot_role, staff.created_at as pivot_created_at, staff.updated_at as pivot_updated_at from users inner join staff on users.id = staff.user_id where staff.staff_id in (2) and staff.staff_type = App\Models\Investor)
//investor relationship
public function staff()
{
return $this->morphToMany(User::class, 'staff')
->withPivot(['role'])
->withTimestamps();
}
//user relationship
public function investors()
{
return $this->morphedByMany(Investor::class, 'staff');
}
you don't follow naming conventions so you should determine the foreign key and "other key" for the relationship:
public function staff()
{
return $this->morphToMany(User::class, 'staff','name of table',
'foreignPivotKey','relatedPivotKey);
}
I don't know your table columns name but in your case can be something like below:
public function staff()
{
return $this->morphToMany(User::class, 'staff','staffabels',
'staffabel_id','staff_id')
->withPivot(['role'])
->withTimestamps();
}
public function investors()
{
return $this->morphedByMany(Investor::class, 'staff','staffabels',
'staff_id','staffable_id');
}
I want get all merchant users for a specific merchant. Here is my setup;
merchant
- id
merchant_user_permission
- merchant_id (equal to merchant.id)
- user_id (equal to merchant_user.id)
merchant_user
- id
In my code I have tried:
class Merchant extends Model
{
public function merchant_users()
{
return $this->hasManyThrough(MerchantUser::class, MerchantUserPermission::class, 'user_id');
}
}
But the resulting sql is invalid.
string(374) "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'merchant_user.merchant_user_permission_id' in 'on clause' (SQL: select count(*) as aggregate from merchant_user inner join merchant_user_permission on merchant_user_permission.id = merchant_user.merchant_user_permission_id where merchant_user_permission.user_id = 123-3456-7789-qwerty)"
Here is a working sql example:
SELECT mu.`id`, mu.`email`
FROM `merchant` m
LEFT JOIN `merchant_user_permission` mup ON m.id = mup.merchant_id
LEFT JOIN `merchant_user` mu ON mu.id = mup.user_id
WHERE mup.`merchant_id` = '123-3456-7789-qwerty'
How can replicate that using Eloquent 5.4?
try this
Make many to many relationship
class Merchant extends Model
{
public function merchant_users()
{
return $this->belongsToMany('App\ MerchantUser', 'merchant_user_permission', 'merchant_id', 'merchant_user_id');
}
}
then
$permissions = App\Merchant::find($merchant_id)->merchant_users()->get();
I have a record model and status model. Which each record has status can be "approved", "disapproved" or "in process" which is stored in the status model that connected trough status_id in the record model.
This is my model & their relationship.
Records(id, user_id, status_id, note, date, timestamp)
function status()
{
return $this->hasOne('App\Status');
}
Statuses(id, name)
function record()
{
return $this->belongsToMany('App\Record');
}
I would like to callback like this on my view $records->status->name. But it gives error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Statuses.record_id' in 'where clause' (SQL: select * from `statuses` where `statuses`.`record_id` = 3 and `statuses`.`record_id` is not null limit 1)
I try to to play the relationship & the callback but it not work. Is it possible to do that? Or better i just save the status directly on the record table without using the status record. Thank you.
You have the relationships reversed. Can you try the following instead:
class Record extends Model
{
public function status()
{
return $this->belongsTo('App\Status');
}
}
class Status extends Model
{
public function records()
{
return $this->hasMany('App\Record');
}
}
I think you are going for a 'One To Many', rather than a 'Many To Many' relationship.
Try the following instead:
Records(id, user_id, status_id, note, date, timestamp)
function status()
{
return $this->belongsTo('App\Status');
}
Statuses(id, name)
function record()
{
return $this->hasMany('App\Record');
}
Define hasOne Relationship on Records
Records(id, user_id, status_id, note, date, timestamp)
function status(){
return $this->hasOne('App\Status','id','status_id');
}
and hasMany Relationship on Status
Statuses(id, name)
function record(){
return $this->hasMany('App\Record','status_id','id');
}
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Statuses.record_id' in 'where clause' (SQL: select * from `statuses` where `statuses`.`record_id` = 3 and `statuses`.`record_id` is not null limit 1)
According to the Error! It says that you don't have records_id in your status table. So add it.
For this to work on You should have a unsigned integer named as record_id in your Status Migration.
$table->unsignedInteger('record_id')->references('id')->on('records'); //This will work as the foreign key to find the respective Status.
After editing the migration file you'll have to rollback migrations.
In terminal at your project directory,
php artisan migrate:rollback
If you could add the migrations as well, It will be easier to point out.
model:Record
Records(id,user_id,status_id,note)
function status(){
return $this->belongsTo('App\Status','id);
}
model:Status
Status(id,name)
function record(){
return $this->hasmany('App\Record','id','status_id');
}
Try this relation it will work
I have the following table relationship:
organizations
id - integer
organization_users
organization_id - integer (FK)
user_id - integer (FK)
users
id - integer
I am trying to get all the users of an organization through eloquent relationships. Here is my Organization.php model with its relationships:
class Organization extends Model
{
public function Users(){
return $this->hasManyThrough('App\User', 'App\OrganizationUser',
'organization_id', 'user_id', 'id');
...
}
I have tried many combinations of that relationship such as
return $this->hasManyThrough('App\User', 'App\OrganizationUser',
'user_id', 'organization_id', 'id');
But all turn up somewhat the same error (this one is from the first query):
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not
found: 1054 Unknown column 'organization_users.id' in 'on clause' (SQL: select
`users`.*, `organization_users`.`organization_id` from `users` inner join
`organization_users` on `organization_users`.`id` = `users`.`user_id` where
`organization_users`.`organization_id` = 1)'
Is it possible that I can have the relationship retrieve the user_id to query on the users table instead of Laravel trying to retrieve organization_users.id? If not is there another way around this?
This is many to many relationship.
User Model:
public function organizations()
{
return $this->belongsToMany('App\Organization','organization_users');
}
Organization Model:
public function users()
{
return $this->belongsToMany('App\User','organization_users');
}
To, get all the users with their organizations:
$users=User::with('organizations')->get();
foreach($users as $user)
{
print_r($user->name);
foreach($user->organizations as $organization)
{
print_r($organization->name);
}
}
What you are describing looks like it may be a 'Many-to-Many' relationship.
https://laravel.com/docs/5.3/eloquent-relationships#many-to-many
I have classes User, User_Role and Role_Permission with correspondent tables
users (id)
users_roles (user_id, role_id, one user_id has one role_id)
roles_permissions (role_id, permission_id, one role_id has many permission_id)
I want to get permissions for user via $user->permissions. For this I wrote function
public function permissions()
{
return $this->hasManyThrough('User_Role', 'Role_Permission', 'user_id','role_id');
}
I get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles_permissions.user_id' in 'field list' (SQL: select `users_roles`.*, `roles_permissions`.`user_id` from `users_roles` inner join `roles_permissions` on `roles_permissions`.`id` = `users_roles`.`role_id` where `roles_permissions`.`user_id` = 1)
What am I doing wrong? Thanks!
Switch the order of the first two arguments:
public function permissions()
{
return $this->hasManyThrough('Role_Permission', 'User_Role', 'user_id', 'role_id');
}
Eloquent is looking for the user_id column in the Role_Permission table, which it won't find..