laravel morphToMany of parent model how to use in subquery? - mysql

Current laravel models relations: ParentModel can have many documents, ChildModel can have many documents, same Documents can be belonged to any of ParentModel and ChildModel.
Also ChildModel always belongs to one ParentModel. ParentModel can have multiple ChildModels.
App\ParentModel relationships
...
public function childmodels()
{
return $this->hasMany('App\ChildModel');
}
public function documents()
{
return $this->morphToMany('App\Document', 'documentable');
}
...
App\ChildModel relationships
...
public function parentmodel()
{
return $this->belongsTo('App\ParentModel');
}
public function documents()
{
return $this->morphToMany('App\Document', 'documentable');
}
...
App\Document
...
public function parentmodels()
{
return $this->morphedByMany('App\ParentModel','documentable');
}
public function childmodels()
{
return $this->morphedByMany('App\ChildModel','documentable');
}
...
Now I'm trying to get all records of ChildModels (1) which have documents with specific type and (2) its ParentModel can also have documents.
The (1) first goal can be reached with such ChildModel method.
/* checking GET param to join this condition to final query */
if($request->has('report') && $request->input('report') == 'on') {
/* get all documents related to CurrentModel */
$query->whereHas('documents',function (Builder $query) {
$query->where('type', 1);
});
}
But this obviously doesnt include records of ChildModels, ParentModel of which has documents with specific type.
So the question is: how to include such condition into ChildModel query builder?

Found the easy way to access relations in builder
$query->whereHas('parentmodels.documents', function(Builder $query){
$query->where('type',1);
});

Related

many-to-many relationship: order by on pivot table not working

I have these relationship between school and associate models:
// School model
public function associates()
{
return $this->belongsToMany('Associate', 'school_associate', 'school_id', 'associate_id')
->withPivot('start_date', 'end_date');
}
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date');
}
I need to get all associates of one school ordered by start_date.
This is what I tried without success (in this try I am searching in all schools):
dd(\App\Associate::with(['schools' => function ($q) {
$q->orderBy('pivot_start_date', 'desc');
}])->toSql());
And I get this sql (notice no order by clause):
select * from `associate`
I tried to edit the relationship like this:
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date')
->orderBy('pivot_start_date', 'desc'); // also tried without "pivot_"
}
And according to this post, I also tried :
// Associate model
public function schools()
{
return $this->belongsToMany('School', 'school_associate', 'associate_id', 'school_id')
->withPivot('start_date', 'end_date')
->orderBy('school_associate.start_date', 'desc');
}
But I always get the same query and the results are not ordered.
I solved using query builder in this way.
This function is in Associate model:
public function scopeLast($query, $school_ids = [])
{
$query->join('school_associate', "{$this->table}.{$this->primaryKey}", '=', 'school_associate.associate_id')
->join('school', 'school.school_id', '=', 'school_associate.school_id')
->whereIn('school.school_id', $school_ids)
->orderBy('school_associate.start_date', 'desc');
return $query;
}

hasOne with null-able in laravel not working

I have a customer table which has a field called 'policy_id', where policy_id points to policy table. It is a null-able field, ie. Some customers may not have a policy.
I have a relationship code like this in Customer.php
public function policy() {
return $this->hasOne('App\Models\Policy', "id", "policy_id");
}
But when I issue a search request I am getting error like this:
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Policy]
If I modify the function like this:
public function policy() {
if ($this->getAttribute('policy_id')) {
return $this->hasOne('App\Models\Policy', "id", "policy_id");
} else {
return null
}
}
But I am getting an error like this:
Call to a member function getRelationExistenceQuery() on null
Here is my search code:
$c = new Customer();
return Customer::doesntHave('policy')->orWhere(function (Builder $query) use ($req) {
$query->orWhereHas('policy', function (Builder $query) use ($req) {
$p = new Policy();
$query->where($req->only($p->getFillable()))
->orWhereBetween("policy_period_from", [$req->policy_period_start_from, $req->policy_period_start_to])
->orWhereBetween("policy_period_to", [$req->policy_period_end_from, $req->policy_period_end_to])
->orWhereBetween("payment_date", [$req->payment_date_from, $req->payment_date_to]);
});
})->where($req->only($c->getFillable()))->get();
Am I missing something or are there any other ways to do this?
PS: While debugging the above search code is returning successfully, but the exception happening from somewhere inside Laravel after the prepareResponse call.
Thanks in advance.
return $this->hasOne('App\ModelName', 'foreign_key', 'local_key');
Change the order, put the foreign_key policy_id in front of id
In your Customer Model, you need to use belongsTo method:
public function policy() {
return $this->belongsTo('App\Models\Policy', "policy_id", "id");
}
And In your Policy Model, use hasOne:
public function customer() {
return $this->hasOne('App\Models\Customer', "policy_id", "id");
}
First of all, you placed the wrong params.
$this->belongsTo('App\Models\Policy', "FK", "PK");
public function policy() {
return $this->belongsTo('App\Models\Policy','policy_id', 'id');
}
And for null value of policy_id you can use withDefault();
public function policy() {
return $this->belongsTo('App\Models\Policy','policy_id', 'id')->withDefault([
'name' => 'test'
]);;
}
there's a number of problems there but can you perhaps specify the namespace and the class of both your models - Customer and Policy.
By default, the models you create with php artisan make:model will use the \App namespace e.g. \App\Customer and \App\Policy.
Just double check that.
Also, with regards to the relationship, if the Laravel conventions have been followed you could just:
In the Customer model
public function policy() {
return $this->belongsTo(Policy::class);
}
In the Policy model
public function customer() {
return $this->hasOne(Customer::class);
}
of if a multiple customers can be under one policy
public function customers() {
return $this->hasMany(Customer::class);
}
Good luck

Multiple foreign keys from one table to same field in another table in Laravel

I am working on a project in Laravel where I have two table.
- "teams" (table) with "Team" (model)
- "players" (table) with "Player" (model)
Users can pick any combination of 12 players to make their own team so I am trying to create "one to many" relationship where players will have only one record for each player but many teams can have same player in them.
"players" table have column "id" as primary key.
"teams" table have 12 columns like "player1_id", "player2_id", "player3_id" and so on and every column will act as a foreign key of the "id" in players table.
My clear cut question is - what code do I need to put in both models (Player and Team) so I can access the information through Eloquent.
so far I have put this code in my teams table but not sure if this is the right thing to do.
for the players table, I have no clue what to write in there.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
//
public function player1()
{
return $this->belongsTo('App\Player', 'player1_id');
}
public function player2()
{
return $this->belongsTo('App\Player', 'player2_id');
}
public function player3()
{
return $this->belongsTo('App\Player', 'player3_id');
}
public function player4()
{
return $this->belongsTo('App\Player', 'player4_id');
}
public function player5()
{
return $this->belongsTo('App\Player', 'player5_id');
}
public function player6()
{
return $this->belongsTo('App\Player', 'player6_id');
}
public function player7()
{
return $this->belongsTo('App\Player', 'player7_id');
}
public function player8()
{
return $this->belongsTo('App\Player', 'player8_id');
}
public function player9()
{
return $this->belongsTo('App\Player', 'player9_id');
}
public function player10()
{
return $this->belongsTo('App\Player', 'player10_id');
}
public function player11()
{
return $this->belongsTo('App\Player', 'player11_id');
}
public function bonus_player()
{
return $this->belongsTo('App\Player', 'bonus_player_id');
}
}
There are several ways to do it:
1- You can put the team players in a separate table, let's say team_players, with columns, team_id, player_id, and position. validate in your code that always 12 players are present. This would be a many-to-many relationship.
in this scenario, you can use this code in your Team model:
public function players() {
return $this->belongsToMany(Player::class, 'team_players');
}
Handling the logic in code can be a bit troubling for a laravel beginner though.
2- You can simply perform a query based on the order:
public function player(int $position){
$playerId = $this->attributes('player'.$position.'_id');
return Player::find($player);
}
3- you can define magic methods, although I strongly discourage you from doing so.
public function __call(string $name , array $arguments){
if (Str::beginsWith($name, 'player')){
return $this->belongsTo(Player::class, $name.'_id');
}
}
there might be other ways too, but I suspect they'd be a little messier.

Laravel Relations One to One and One to Many

I have three models Class, Students and Studentinfo. Class and students are in a One to Many relationship and Sudents and studentinfo are in a one to one relationship.
While getting students from certain Class I get a list of data in an array.
What is the best way to get data from studentinfo for each student in the array?
I am trying to get this data in json format.
You'd set up relationships like the following on the models, the important one being the hasManythrough relation:
// Class.php
public function students() {
return $this->hasMany(Student::class);
}
public function studentInfo()
{
return $this->hasManyThrough(StudentInfo::class, Student::class);
}
// Student.php
public function studentInfo() {
return $this->hasOne(StudentInfo::class);
}
public function classes() {
return $this->belongsToMany(Class::class);
}
// StudentInfo.php
public function student() {
return $this->belongsTo(Student::class);
}
... you may cast a model or collection to a string, which
will automatically call the toJson method on the model or collection:
$json = (string)$class->studentInfo;
Laravel Docs: Serialization

Adding withCount to collection json

So i've got two models, Client and Project, and Client has a hasMany relationship with projects. I'm just trying to add the project count for the client into the JSON response but I can't get it to work.
My controller just returns all the projects;
public function index(Client $client)
{
return $client->all();
}
And my model contains the below;
protected $appends = ['client_projects_count'];
/**
* Create a relationship between this client and it's projects
*/
public function clientProjects() {
return $this->hasMany('App\Project');
}
public function getClientProjectsCountAttribute()
{
return $this->withCount('clientProjects');
}
This just adds client_projects_count to the response but it's an empty array. I'm close, if I dd($this->withCount('clientProjects')->get()) I can see the client_projects_count with the correct count, but if I remove the dd and keep the ->get() then I get an internal server error.
Also, it is possible to only load this for the index() method rather than every one?
From the Documentation
$clients = Client::withCount('projects')->get();
foreach ($clients as $client) {
echo $client->projects_count;
}
So I managed to resolve it myself, although I'm sure their must be a nicer way.
Client.php
protected $appends = ['client_projects_count'];
/**
* Create a relationship between this client and it's projects
*/
public function clientProjects() {
return $this->hasMany('App\Project');
}
public function clientProjectsCount()
{
return $this->clientProjects()->selectRaw('client_id, count(*) as aggregate')->groupBy('client_id')->get();
}
public function getClientProjectsCountAttribute()
{
return isset($this->clientProjectsCount()[0]) ? $this->clientProjectsCount()[0]->aggregate : 0;
}