Laravel Join other tables in relation - php

I have a Situation Like This:
User Model:
public function role() {
return $this->hasOne('App\model\Roles' , 'id' ,'role');
}
public function userMetaData() {
return $this->hasOne('App\model\UserMetaData' , 'user_id' ,'id');
}
public function userBet() {
return $this->hasMany('App\model\UserBet' , 'user_id' , 'id');
}
public function userComission() {
return $this->hasMany('App\model\UserComission' , 'user_id' , 'id');
}
public function userPartnership() {
return $this->hasMany('App\model\UserPartneShip' , 'user_id' , 'id');
}
// Self Call
public function parentData() {
return $this->hasOne('App\User','id','parent_id');
}
Controller
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','role','parentData'])
->where('id',$id)
->get();
Now The Point Is In role i am getting the roles Of the User and In the parentData i am getting the creator of the user(parent) from the same user table by self calling now that parent also has a role
My Question Is How can i get that role object inside the parentData Object?
Thanks!

First of all.. the relationship you've set is wrong it should be belongsTo
public function role() {
return $this->belongsTo('App\model\Roles' ,'role', 'id');
}
public function parentData() {
return $this->belongsTo('App\User','parent_id','id');
}
Now as you want role object inside the parentData set with as below.
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','parentData.role'])
->where('id',$id)
->get();

Related

Eloquent Relationships Laravel connecting multiple tables together

i am attempting to rewrite all my joins into Elequent model relationships.
Here is what i have so far:
class SectionAndUser
{
public function sections()
{
return $this->belongsTo('App\Models\Section');
}
public function users()
{
return $this->belongsTo('App\Models\User');
}
...
class User
{
public function sectionAndUser()
{
return $this->hasMany('App\Models\SectionAndUser');
}
...
class Section
{
public function sectionAndUsers()
{
return $this->hasMany('App\Models\SectionAndUser');
}
...
With the select:
$sections = User::find($userId)->sectionAndUser()->get();
I get the result:
{
"id": 1,
"section_id": 1,
"user_id": 133
}
How do i now attach the 3 model section that carries all the data about section_id 1?
This is the join that i am hoping to achieve:
$id=Auth::id();
$results = DB::table('sections')
->join('section_and_users', function ($join) use ($id) {
$join->on('sections.id', '=', 'section_and_users.section_id')
->where('section_and_users.user_id','=', $id);
})
->get();
The expected result:
{
"id": 1,
"section_id": 1,
"section_name": 'sectionName'
"user_id": 133
}
I think the solution is to create only models Section and User, and add the relationship as BelongsToMany.
class User
{
public function sections()
{
return $this->BelongsToMany('App\Models\Section');
}
...
And
<?
class Section
{
public function users()
{
return $this->BelongsToMany('App\Models\User');
}
...
And of course, you need to create the pivot table. You can consult BelongsToMany documentation.
If you use this way, you can simple get the result with this query:
$section = Section::find(1); // This will return all your Section data
$section_related_users = $section->users; // This will return a collection of Users
You could do it this way
$id=Auth::id();
$results = SectionAndUser::where('user_id', $id)->with('users', 'sections')->get();
then you could map it to get your desired output
$sections = collect($results)->map(function ($section){
return [
'id' => $section->id,
'section_id' => $section->id,
'section_name' => $section->sections->name,
'user_id' => $section->user_id
];
});
You can create a many-to-many realtionship without the SectionAndUser-model.
With the belongsToMany-method, you can pass the name of the pivot table as a second argument. You can view Illuminate\Database\Eloquent\Concerns\HasRelationships#belongsToMany if you want to know what other arguments you can pass.
Section:
class Section extends Model
{
...
public function users()
{
return $this->belongsToMany(User::class, 'section_and_users');
}
...
}
User:
class User extends Model
{
...
public function sections()
{
return $this->belongsToMany(Section::class, 'section_and_users');
}
...
}
Then use it as this:
$user->sections->where(...
// Post Model
public function user()
{
return $this->belongsTo('App\User');
}
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
// Role Model
public function users()
{
return $this->hasMany('App\User');
}
// User Model
public function role()
{
return $this->belongsTo('App\Role');
}
public function posts()
{
return $this->hasMany('App\Post');
}
//Tag Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
// Catgory Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
// controller
public function index()
{
$posts = Post::latest()->get();
return view('admin.post.index',compact('posts'));
}
// posts tabel to user result get
foreach($posts as $post){
$post->user->name
}

Laravel eloquent insert data with multiple relationship

I have 3 tables:
User
- id
- email
UserAccount
- id
- user_id
- account_id
Account
- id
- user_id
Verification
- id
- user_id
- guid
I am trying to achieve a post whenever I try to add a user, it will automatically add an account with empty fields but with user_id in it, Verification table with user_id also, at the same time once the Account has been created it should also record UserAccount user_id and account_id but I ended up this error using many to many relationship belongsToMany and sync. How do I add the acct_id and user_id with eloquent?
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'acct_id' cannot be null (SQL: insert into `user_accounts` (`acct_id`, `user_id`) values (?, 17))
This is what I've tried so far.
Controller.php
$user = new User();
$user->name = "Not set";
$user->email = $email;
$user->password = NULL;
$user->save();
$accounts = new Account();
$accounts->email = $email;
$user->account()->save($accounts);
$userAccount = new UserAccount();
$userAccount->userAccount()->sync([
$user->id,
$accounts->id
]);
User.php
public function account()
{
return $this->hasMany(Account::class);
}
public function userAccount()
{
return $this->belongsToMany(User::class, UserAccount::class, 'user_id', 'id');
}
UserACcount.php
public function user()
{
return $this->hasMany(User::class, 'user_id', 'id');
}
public function account()
{
return $this->hasMany(Account::class, 'acct_id', 'id');
}
public function userAccount()
{
return $this->belongsToMany(Account::class, UserAccount::class, 'acct_id', 'user_id');
}
Verification.php
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
Account.php
public function user()
{
return $this->hasMany(User::class);
}
public function userAccount()
{
return $this->belongsTo(UserAccount::class);
}
I tried using this functionality and completely works fine but pretty sure this is how it works with eloquent.
$userAcct = new UserAccount();
$userAcct->create([
'user_id' => $user->id,
'acct_id' => $accounts->id
]);
Any thoughts?
I also have did found this related problem (Laravel hasManyThrough)
First of all, you should remove user_id from the account table because it is already referenced by user_account which links both tables. Moreover, if you wanna take advantages of Eloquent conventions which allow it to guess table names and fields, you should make sure your tables are named users, accounts, verifications and account_user.
User.php
public function accounts()
{
return $this->belongsToMany(Account::class);
}
Account.php
public function users()
{
return $this->belongsToMany(User::class);
}
The UserAccount model is useless if account_user exists only to links 2 tables.
Then, you may use an observer to get an event-based approach: whenever an user is created => do something
https://laravel.com/docs/5.8/eloquent#observers
<?php
namespace App\Observers;
use App\Account;
use App\Verification;
class UserObserver
{
/**
* Handle the User "created" event.
*
* #param \App\User $user
* #return void
*/
public function created(User $user)
{
(new Verification)->user()->associate($user);
$account = new Account;
$account->save();
$user->accounts()->attach([
$account->id
]);
}
}
// Post Model
public function user()
{
return $this->belongsTo('App\User');
}
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
//User Model
public function posts()
{
return $this->hasMany('App\Post');
}
//Category Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
//Tag Model
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}

How to use Multiple relationship in laravel 5.4

In my app i have define relationship (profile, user, level) but when I fetch data it is showing an error (Trying to get property 'email' of non-object) how can i solve this thank in advance.
this is User Model
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
public function user()
{
return $this->belongsTo(User::class, 'id');
}
public function level()
{
return $this->belongsTo(Level::class, 'id');
}
Level Model
public function profile()
{
return $this->hasOne(Profile::class, 'level_id');
}
This is Controller ProfileController
$users = Profile::with(['user', 'level'])->where('is_bd_partner', 'Yes')->get();
foreach ($users as $key => $value)
{
echo $value->first_name.'<br>';
echo $value->last_name.'<br>';
echo $value->user->email.'<br>';
echo $value->level->level.'<br>';
}
Note that belongsTo takes foreign_key as the first parameter.
So you should change the Profile Model as
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function level()
{
return $this->belongsTo(Level::class, 'level_id');
}
Read more here

3 models many-to-many sync in Laravel 5.4

I have 3 models with the relations many-to-many:
Module
public function permissionTypes()
{
return $this->belongsToMany(PermissionType::class, 'permissions')->withPivot('role_id');
}
public function roles()
{
return $this->belongsToMany(Role::class, 'permissions')->withPivot('permission_type_id');
}
Role
public function permissionTypes()
{
return $this->belongsToMany(PermissionType::class, 'permissions')->withPivot('module_id');
}
public function modules()
{
return $this->belongsToMany(Module::class, 'permissions')->withPivot('permission_type_id');
}
PermissionType
public function roles()
{
return $this->belongsToMany(Role::class, 'permissions')->withPivot('module_id');
}
public function modules()
{
return $this->belongsToMany(Module::class, 'permissions')->withPivot('role_id');
}
tables description:
modules
id
title
status
roles
id
title
permission_types
id
title
pivot table permissions
id
role_id
module_id
permission_type_id
My synchronization looks like:
//array of ids from request to synchronization
$permissions = $request['permissions'];
//role by id from request
$role = Role::findOrFail((int)$roleId);
//module by id from request
$module = Module::findOrFail((int)$moduleId);
//synchronization
$pivotData = array_fill(0, count($permissions), ['role_id' => $role->id]);
$syncData = array_combine($permissions, $pivotData);
$module->permissionTypes()->sync($syncData);
When trying to make the synchronization, have an error
QueryException in Connection.php line 647:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'permissions' (SQL: select permissions.*, permissions.role_id as pivot_role_id, permissions.permission_id as pivot_permission_id from permissions inner join permissions on permissions.id = permissions.permission_id where permissions.role_id = 1)
Thanks
IMHO you are trying to design a triple many to many that does not exists in Laravel. The solution, usually is to give the pivot table (in your case permissions) a Model (Permission in below code) for defining hasManyThrough relations.
If I understood well your table structure I will design the following relationships:
Module
public function permissions()
{
return $this->hasMany(Permission::class);
}
public function roles()
{
return $this->hasManyThrough(Role::class, Permission::class);
}
public function permissionTypes()
{
return $this->hasManyThrough(PermissionType::class, Permission::class);
}
Role
public function permissions()
{
return $this->hasMany(Permission::class);
}
public function permissionTypes()
{
return $this->hasManyThrough(PermissionType::class, Permission::class);
}
public function modules()
{
return $this->hasManyThrough(Module::class, Permission::class);
}
PermissionType
public function permissions()
{
return $this->hasMany(Permission::class);
}
public function modules()
{
return $this->hasManyThrough(Module::class, Permission::class);
}
public function roles()
{
return $this->hasManyThrough(Role::class, Permission::class);
}
Permission
public function permissionType()
{
return $this->belongsTo(PermissionType::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
public function module()
{
return $this->belongsTo(Module::class);
}
Tell me if it could work for you.

Laravel 4.1 eager loading

I'm having trouble on the eager loading.
Let's say I have models of Members, TrainingCategory, TrainingCategoryResult and Registration
Member Model:
public function registration() {
return $this->hasMany('Registration', 'member_id');
}
public function trainingResults(){
return $this->hasMany('trainingResult', 'member_id');
}
public function trainingCategoryResults() {
return $this->hasMany('TrainingCategoryResult', 'member_id');
}
TrainingCategory Model:
public function trainings() {
return $this->hasMany('Training', 'id');
}
public function trainingCategoryResults() {
return $this->hasMany('trainingCategoryResult', 'category_id');
}
TraningCategoryResult Model:
public function category() {
return $this->belongsTo('TrainingCategory', 'id');
}
public function member() {
return $this->belongsTo('Member', 'id');
}
Registration Model:
public function course() {
return $this->belongsTo('Course', 'course_id');
}
public function member() {
return $this->belongsTo('Member', 'id');
}
I am trying to eager load all the registration info and its related info including the TraningCategoryResult info but I not sure how to get that TraningCategoryResult which required two foreign keys (category_id and member_id), is there any way to do that?
Here is my code atm:
$members= Member::where(function($query) use ($id, $site) {
$query
->where('id', '=', $id)
->where('site', '=', $site);
});
$members= $members
->with('registration.course',
'registration.course.traningCategories',
->get(['member.id']);
Thank you.
This will not work Member::with('categoryResult')->with('registration')->get()
You can make a new relation in Member Model
public function categoryResult()
{
return $this->belongsTo('Category')->with('Registration');
}
//and then call
Member::with('categoryResult')->get();
You could use a few options to achieve that:
OPTION 1: create a variable relationship
Change your relation in the Member model
public function trainingCategoryResults($category_id = null) {
if(empty($category_id))
return $this->hasMany('TrainingCategoryResult', 'member_id');
else
return $this->hasMany('TrainingCategoryResult', 'member_id')
->where('category_id', $category_id);
}
The code above might have limitations and it doesn't take advantage of many laravel features, but it will work.
OPTION 2: Access from relationship
You can keep everything as is, and load as follow:
$members= Member::where(function($query) use ($id, $site) {
$query
->where('id', '=', $id)
->where('site', '=', $site);
})
->where('id', $member_id) // set the id of the memeber
->with(array(
'traningCategoryResults' => function($q)use($category_id){
$q->where('category_id', $category_id); // This makes sure you get only desired results
}
))
In this way you will have only what you need, assuming you know the $category_id

Categories