Laravel - defining the right relationship - laravel

User Table: has zone_id field in it.
Zones table: has world_id field in it.
Each user can be in one zone, for example, zone_id = 1
Each zone belongs to one world, for example - world_id = 5
My desired output is returning user zone and world info.
This is how I can make it without any relationship set:
$zone = Zone::find($user->zone_Id);
$world = World::find($zone->world_id);
$data = $user;
$data['zone'] = $zone;
$data['zone']['world'] = $world;
My question is.. I'm sure relationship can be used for a cleaner code, but I'm not sure how to set it up.
Should I stick with the current code or define a relationship?
If the answer for 1 is define a relationship, Any help of what's the right relationship between these 3 models?
Solution 1:
`public function getZone(Request $request)
{
$token = $request->input('token');
$user = JWTAuth::toUser($token);
// Simplest example using relationships
$userWithZone = User::with('zone.world')->find($user->id); // You'll get the `zone` relationship info here, too
return $userWithZone;
}`
Error: returns "Call to a member function getQuery() on null"

Here's an example of how you can achieve this with Eloquent Relationships.
// User.php
class User
{
public function zone()
{
return $this->belongsTo(Zone::class);
}
}
// Zone.php
class Zone
{
public function world()
{
return $this->belongsTo(World::class);
}
}
Simplest example using relationships
$user = User::with('zone.world')->find($id); // You'll get the `zone` relationship info here, too
You can get more complex with your relationships if you want
$user = User::with(['zone' => function($query) {
$query->with('world')
->select('id', 'zone_name', 'world_id');
})->select('username', 'zone_id')->find($id);
or even...
$user = User::with(['zone' => function($query) {
$query->with(['world' => function($query2) {
$query2->select('id', 'world_name');
}])->select('id', 'zone_name', 'world_id');
})->select('username', 'zone_id')->find($id);
Your resulting $user will look something like:
'user' => [ // This is a Collection
'username',
'email',
'zone_id',
'zone' => [ // This is a Collection
'id',
'zone_name',
'world_id',
'world' => [ // This is a Collection
'id',
'world_name'
]
]
];

Related

Filter an array list result from query builder in model in Laravel

I have a query function in my model to get list user and it return an array data to my repository class:
Model.php:
public function getUser()
{
return $this->select("{$this->table}.'*'")
->get();
}
Here is an example for the data:
[
[
"user_id":1,
"fullname":"amdv",
"is_active":0
],
[
"user_id":2,
"fullname":"abc",
"is_active":1
],
[
"user_id":3,
"fullname":"zyz",
"is_active":1
]
]
Now I want to check if is_active = 1, it continue check that user with other condition in other query
In my model, I have another query function with param is user id
Model.php:
public function checkUserAvailable($userId)
{
return $this->select("{$this->table}.'*'")
->join('other_table', 'join-condition')
->where('user_id', $userId)
->get();
}
But this function return data too, I don't know how to check this.
Thank you very much!
$data = [
[
"user_id":1,
"fullname":"amdv",
"is_active":1
],
[
"user_id":2,
"fullname":"abc",
"is_active":1
],
[
"user_id":3,
"fullname":"zyz",
"is_active":1
]
];
$filtered = collect($data)->filter(function($user) {
return User::someMethodYouMentioned($user['user_id']);
});
From what I understand is that you want to check the data from the array returned by the first function and if is_active = 1 then you want to check if the user is available via the second function in your model
//Get the user_id of records where is_active === 1
$available = collect($data)
->reject(fn($user) => $user['is_active'] !== 1)
->pluck('user_id')
->map(fn($id) => User::checkUserAvailable($id));
For this to work the checkUserAvailable needs to be a static function.
However looking at both the functions - question arises why do you need to run multiple database queries to check for user available via the second function.
You can modify the query in the second function to check for is_active
public static function checkUserAvailable()
{
return static::query()
->join('other_table', 'join-condition')
->where('is_active', 1)
->get();
}
Or if you still want to first get a list of all users as array and then check for availability, you could define the checkUserAvailable as a scope on the model
public function scopeCheckUserAvailable($query, $id)
{
$ids = Arr::wrap($id);
return $query->join('other_table', 'join-condition')
->whereIn('user_id', $ids);
}
Then you can get the data from the getUser() function and perform the second check like
//Get the user_id of records where is_active === 1
$checkIds = collect($data)
->reject(fn($user) => $user['is_active'] !== 1)
->pluck('user_id')
->all();
$availableUsers = User::checkAvailableUsers($checkIds)->get();
If you want to filter out the ones that are not active you can use array_filter with a callback:
$filtered = array_filter($array, fn ($i) => $i['is_active']);
If you want to use those values to call a function and then get those results you can use array_map:
$res = array_map([$this, 'checkUserAvailable'], array_column($filtered, 'user_id'));
You can also do this with the Collection methods if you would like:
collect($array)->where('is_active', 1)
->pluck('user_id')
->map([$this, 'checkUserAvailable'])
Though, I am not sure what else you need to be checking besides that is_active is true.

Laravel - Get array with relationship

I have an ajax call that returns an array:
$reports = Report::where('submission_id', $submissionID)
->where('status', 'pending')
->get(['description','rule']);
return [
'message' => 'Success.',
'reports' => $reports,
];
From this array, I only want to return the fields 'description' and 'rule'. However I also want to return the owner() relationship from the Report model. How could I do this? Do I have to load the relationship and do some kind of array push, or is there a more elegant solution?
You can use with() to eager load related model
$reports = Report::with('owner')
->where('submission_id', $submissionID)
->where('status', 'pending')
->get(['id','description','rule']);
Note you need to include id in get() from report model to map (owner) related model
you will have probably one to many relationship with Reports and owners table like below
Report Model
public function owner() {
return $this->belongsTo('App\Owner');
}
Owner Model
public function reports() {
return $this->hasMany('App\Report');
}
your controller code
$reports = Report::with('owner')->
where('submission_id', $submissionID)->where('status', 'pending')->get()
return [
'message' => 'Success.',
'reports' => $reports,
];
This is what I ended up going with:
$reports = Report::
with(['owner' => function($q)
{
$q->select('username', 'id');
}])
->where('submission_id', $submissionID)
->where('status', 'pending')
->select('description', 'rule','created_by')
->get();
The other answers were right, I needed to load in the ID of the user. But I had to use a function for it to work.

Eager load for collections on the fly

I have this, user has many to many property via pivot property_users.
I am making somehow reusable classes in my webapp.
These are the models with their eager loading functions:
//User model
public function properties()
{
return $this->belongsToMany(Property::class, 'property_users', 'user_id', 'property_id');
}
//Property model
public function property_users()
{
return $this->hasMany(PropertyUser::class, 'property_id', 'id');
}
//PropertyUser model
public function user()
{
return $this->belongsTo(User::class);
}
//GetProperties class
public function handle()
{
return auth()->user()->properties()->get();
}
//somewhere in a feature
$properties = $this->run(GetProperties::class);
//this returns valid properties under the logged in user
I now need to get the chat_username in property_users that belongs to this user
I manage to make it work if I loop through the properties and then doing it on the fly.
$properties = $properties->map(function($property) {
$propertyUsers = $property->property_users()->get();
$chatUsername = null;
foreach($propertyUsers as $propertyUser) {
if($propertyUser->property_id == $property->id) {
$chatUsername = $propertyUser->chat_username;
}
}
return [
'name' => $property->name,
'id' => $property->id,
'chat_username' => $chatUsername
];
});
But I am trying to reduce query on loop to reduce hits especially when they are on multiple properties in the database.
The other way is that I can add the property_users in the eager loading under GetProperties class by updating it to:
$query = Property::query();
$query->with(['property_users']);
$query->whereHas('property_users', function($qry) {
$qry->where('user_id', Auth::user()->id);
});
$properties = $query->get();
return $properties;
But I do not want to rely on adding more eager loading to the original GetProperties class as the GetProperties will get fat and I do not really need those data (let's say adding property_invoices, property_schedules, etc but not really needing it in some area).
Rather, I want to do the eager loading on the fly but with a twist! This is how I would imagine it:
Collect all the ids from the properties, do the fetch using wherein and apply all the users to the properties in a single query. This way it will be even more beautiful.
Maybe something like this: (using the original GetProperties class)
$properties = $this->run(GetProperties::class);
//this does not work. The error is: Method Illuminate\Database\Eloquent\Collection::property_users does not exist.
$property->property_users = $properties->property_users()->get();
Would be great if someone can show me how to do it.
What about eager loading only the fields you actually need?
$query->with('property_users:id,user_id');
The model will not get fat, and you will not need to do separate queries in the loop.
It is documented in the official documentation: https://laravel.com/docs/5.8/eloquent-relationships#eager-loading , see Eager Loading Specific Columns
Edit: if you want to perform the query after the GetProperties class, you will need to collect all the ids and perform a second query. I honestly don't like this second approach, because it is far slower, less performant and I consider it less elegant then adding a single line in the GetProperties class, but it is gonna work:
$properties = $this->run(GetProperties::class);
$ids = $properties->pluck('id'); // Get all the ids as an array
$propertyUsers = propertyUsers::whereIn('property_id', $ids)->get(); // Get the propertyUsers model
foreach($properties as $property) {
$property->property_users = $propertyUsers->where('property_id', $property->id); // Not even sure you can do that, proerty_users may not been writable
}
Alright, after reading here and there.
I found this article: https://laravel-news.com/eloquent-eager-loading
The solution is really nice. Simply:
$properties = $this->run(GetProperties::class);
//lazy load
$properties->load('property_users');
$properties = $properties->map(function($property) {
$user = $property->property_users->first(function($user) {
if($user->user_id == Auth::user()->id) {
return $user;
}
})->only('chat_username');
return [
'name' => $property->name,
'id' => $property->id,
'chat_username' => $user['chat_username']
];
});
After checking query logs:
//auth query done by middleware
[2019-05-21 07:59:11] local.INFO: select * from `users` where `auth_token` = ? or `temporary_auth_token` = ? limit 1 ["token_removed_for_security_purpose","token_removed_for_security_purpose"]
//These are the queries made:
[2019-05-21 07:59:11] local.INFO: select `properties`.*, `property_users`.`user_id` as `pivot_user_id`, `property_users`.`property_id` as `pivot_property_id` from `properties` inner join `property_users` on `properties`.`id` = `property_users`.`property_id` where `property_users`.`user_id` = ? [8]
[2019-05-21 07:59:11] local.INFO: select * from `property_users` where `property_users`.`property_id` in (2, 4)
This way, I can keep my GetProperties as small as possible, then just lazy load it whereever I need it.

Laravel 5.6 - Using model functions in ModelFactory

I am working with Laravel 5.6 and found myself a weird problem while extending the functionality of my project.
Right now i need to create two new models: order and item. It was quite easy to fill the items table with dummy data using Faker and Laravel Factories/Seeders. The biggest problem is while working with the order model.
This little fellow is related to a company with a foreign key named company_id and user with a foreign key named seller_id. The company field is okay, the trouble is behind my seller_id
This seller needs a role related to the company my factory will randomly pick for it because the user is not related to the company (directly) and i can't just look for it with a company_id.
In order to get all the users "related" to my company, i've created the next function on my Company model:
public function users()
{
$roles = $this->roles;
$users = [];
foreach ($roles as $role) {
foreach ($role->users as $user) {
$user->makeHidden(['pivot']);
array_push($users, $user);
}
}
$users = array_unique_objects($users);
return $users;
}
btw: I'm using laravel-permissions, a library made by Spatie.
What this functions does is get every role from a company and then it pushes it to an array of users.
This custom helper: array_unique_objects tracks any repeated user on my array and removes them.
That function works find because i've tested on a couple of controllers so i know there is no problem with it. Either way, my OrderFactory.php looks like this:
<?php
use Faker\Generator as Faker;
use App\Models\User;
use App\Models\Company;
$factory->define(App\Models\Order::class, function (Faker $faker) {
$company = Company::get()->random(1);
$users = $company->users();
$user = array_random($users);
return [
'company_id' => $company,
'seller_id' => $user->id,
'code' => strtoupper(str_random(10)),
'description' => $faker->sentence($nbWords = rand(2, 4), $variableNbWords = true),
'created_at' => $faker->dateTimeBetween($startDate = '-1 year', $endDate = 'now', $timezone = null)
];
});
But when i run the php artisan db:seed command, it throws the next error in console:
BadMethodCallException : Method Illuminate\Database\Eloquent\Collection::users does not exist.
at >/home/ironman/Documentos/Sandbox/Proventas/Backend/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:99
95| */
96| public function __call($method, $parameters)
97| {
98| if (! static::hasMacro($method)) {
99| throw new BadMethodCallException(sprintf(
100| 'Method %s::%s does not exist.', static::class, $method
101| ));
102| }
103|
Exception trace:
1 Illuminate\Support\Collection::__call("users", [])
/home/ironman/Documentos/Sandbox/Proventas/Backend/database/factories/OrderFactory.php:10
2 Illuminate\Database\Eloquent\Factory::{closure}(Object(Faker\Generator), [])
/home/ironman/Documentos/Sandbox/Proventas/Backend/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:274
Please use the argument -v to see more details.
Is there anything I can do to fix this problem? I know that using Laravel Relationships will fix my problem but the specifications of this project says that i have to keep things just as the are.
Your call to
$company = Company::get()->random(1);
does not return a single company. It returns a Collection, which does not have a users dynamic function. Try
$company = Company::get()->random(1)->first();

UpdateExistingPivot for multiple ids

In order to update single record in pivot table I use updateExistingPivot method. However it takes $id as the first argument. For example:
$step->contacts()->updateExistingPivot($id, [
'completed' => true,
'run_at' => \Carbon\Carbon::now()->toDateTimeString()
]);
But how can I update multiple existing rows in pivot table at once?
There's an allRelatedIds() method in the BelongsToMany relation that you can access, which will return a Collection of the related model's ids that appear in the pivot table against the initial model.
Then a foreach will do the job:
$ids = $step->contacts()->allRelatedIds();
foreach ($ids as $id){
$step->contacts()->updateExistingPivot($id, ['completed' => true]);
}
You can update only by using a looping statement as there updateExistingPivot function only accept one dimensional params, See the core function for laravel 5.3.
File: yoursite\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\BelongsToMany.php
Function: updateExistingPivot
public function updateExistingPivot($id, array $attributes, $touch = true)
{
if (in_array($this->updatedAt(), $this->pivotColumns)) {
$attributes = $this->setTimestampsOnAttach($attributes, true);
}
$updated = $this->newPivotStatementForId($id)->update($attributes);
if ($touch) {
$this->touchIfTouching();
}
return $updated;
}
So, You should follow the simple process:
$step = Step::find($stepId);
foreach(yourDataList as $youData){
$step->contacts()->updateExistingPivot($youData->contract_id, [
'completed' => true,
'run_at' => \Carbon\Carbon::now()->toDateTimeString()
]);
}

Resources