Relationship doesn't load data one to many Laravel 5.6 - laravel-5.6

I declared relationship between two table.. one to many... but when I tried to load data the declared relationship is not viewed in the view console..
I have this in GL
public function parent_gl_sle(){
return $this->belongsTo('App\Sle_type','GLControlSLE_CODE','SLE_TypeCode');
}
then this is my SL
public function child_gl_sle(){
return $this->hasMany('App\Glcontrol','GLControlSLE_CODE','SLE_TypeCode');
}
Then, this is my controller.
$gl = Glcontrol::where('GLControlBR_CODE',$brcode)
->with('parent_glcontrol_br')
->with('parent_gl_sle')
->with('parent_cts')
->with('parent_coa')
->get();
But in my console parent_gl_sle is not included in the console.
enter image description here
What did I missed?

Try something like this
$gl = Glcontrol::with(['parent_glcontrol_br','parent_gl_sle','parent_cts','parent_coa'])->where('GLControlBR_CODE',$brcode)->get();

class Glcontrol extends Model
{
public function parent_gl_sle(){
return $this->belongsTo('App\Sle_type','foreign_key_name');
}
public function child_gl_sle(){
return $this->hasMany('App\Glcontrol','foreign_key_name');
}
}
$data = Glcontrol::with(['parent_glcontrol_br','parent_gl_sle','parent_cts','parent_coa'])
->where('GLControlBR_CODE',$brcode)->get();
dd($data);

Related

Get the ID of the first parent

I have three models that are related: Article, ArticleBlock, ArticleBlockImage
ArticleBlock is related with Article and ArticleBlockImage is related with ArticleBlock
Here are the relations
Article
public function article_blocks()
{
return $this->hasMany('App\Models\ArticleBlock');
}
ArticleBlock
public function article()
{
return $this->belongsTo('App\Models\Article');
}
public function article_block_images()
{
return $this->hasMany('App\Models\ArticleBlockImage');
}
ArticleBlockImage
public function article_block()
{
return $this->belongsTo('App\Models\ArticleBlock');
}
Further in the ArticleBlockImage model I have one function with which I need to get the ID of the current Article in the form of type $article->id
I am trying to do something like this, but I get the error
$article = article_block_images()->article_block()->article()->get();
"message": "Call to undefined function App\Models\article_block_images()",
$article = articleBlockImages()->article_block()->article()->get();
when you load your relation this way you load the relation type class, not the database records, relation classes like (HasMany, HasOne ...)
to get article_id you can a function like this:
public function article_id()
{
return $this->article_block->article_id; // without brackets
}
You wrote that ArticleBlock is related to Article and to ArticleBlockImage. Then you have the related Article ID inside the ArticleBlock.
That means if you have the ArticleBlockImage $articleBlockImage then you can write:
$articleId = $articleBlockImage->article_block()->article_id;

Add specific related field to response

I have a controller with the action:
public function getCities(): JsonResponse
{
return response()->json([City::all()], 200);
}
Entity City has relation to Country.
How I can add country.id for every item in the result City::all()?
Laravel has amazing feature for creating virtual attributes. Add these lines to your City model for this:
NOTIFICATION: I assume you have the CountryCity model
public $appends = ['country_id'];
public function getCountryIdAttribute()
{
$country = CountryCity::where('city_id',$this->id)
if($country){
return $country->id;
}
return null;
}
You should look at the documentation : https://laravel.com/docs/8.x/eloquent-relationships
The best way is to use eager loading, using :
City::with('country')->get();
Then, you can access country id like :
$city->country->id;

How to retrieve multiple relations with multiple tables in laravel eloquent

I'm using Laravel 5.8 to build a babysitting site. I have 4 tables with different relationships as below:
please see this image
The relationships are:
Babysitter->hasMany(session)
Sessions->hasOne(Review)
Sessions->hasOne(Kids)
Sessions->hasOne(Babysitter)
Sessions->hasOne(Parent)
I want to achieve 2 things:
First one
I want to show this result when listing all babysitters. I'm showing this information for each babysitter:
plsease see this image
See here what I couldn't achieve
plsease see this image
This is my code
Sitters::where('Status', 'active')->where('Verified', 1)->get();
Second one
Also, I've tried to show kids name with parent review as shown here:
plsease see this image
This is what i'm using
Sessions::select('Reviews.*', 'Sessions.Parent_id')->join('Reviews', 'Reviews.Session_id', '=', 'Sessions.id')->with('owner')->where('Trainer_id', session('user')->Id)->where('Status', '=', 'complete')->with('owner')->orderBy('Sessions.id', 'DESC')->get();
Here is Session.php Model
public function owner(){
return $this->belongsTo('App\Models\Parents', 'Parent_id');
}
As discussed change the relations:
Babysitter->hasMany(sesstion)
Sessions->hasOne(Review)
Sessions->belongsTo(Kids)
Sessions->belongsTo(Babysitter)
Sessions->belongsTo(Parent)
First one
in Babysitter.php declare the following attributes
class Babysitter extends Model
{
public function reviews()
{
$this->hasManyThrough(Review::class, Session::class);
}
public function getAverageReviewAttribute()
{
return $this->reviews()->avg('Rating');
}
}
Then you just need to call it on the model instance.
$babysitter = Babysitter::first();
return $babysitter->average_review;
Second one
Just use the relation
$babysitter = BabySitter::with(['sessions' => public function ($session) {
$session->with(['review','parent','kids']);
})->where('trainer_id', '=', session('user')->Id) //did not understand this condition
->first();
This assumes you have parent, kids and review relation declared on Session::class. (change the names if needed)
After a few days of searching & testing, this is what worked for me:
Inside (Sitters) Model, put this relation
public function sessions()
{
return $this->hasMany(Sessions::class, 'sitter_id')
->withCount('reviews')
->withCount(['reviews as review_avg' => function($query){
$query->select(DB::raw('AVG(Rating)'));
}]);
}
Also, inside (Sessions) Model, put this relation
public function reviews()
{
return $this->hasOne(Reviews::class, 'Session_id');
}
Now you query like this
return $sitters = Sitters::with('sessions')->get();
I hope this can help someone :)

Export Relationship data using Laravel Excel

I'm using Laravel Excel to export data. I want to get all the reminders attached to a vet, which are accessed over a hasManyThrough relationship.
I have tried the following code
RemindersExport.php
public function collection()
{
$vets = Vet::where('is_active', 1)->get();
foreach($vets as $vet){
$reminders = $vet->reminders();
}
return $reminders;
}
Controller
public function reminders()
{
return Excel::download(new RemindersExport, 'reminders30days.xlsx');
}
I get the following message...
Method Illuminate\Database\Query\Builder::all does not exist.
The problem is that you're using the collection method but you should use the query method and laravel-excel excecutes the query for you so you don't have to use the get method at the end of your query, first you sould use the FromQuery concern, after that you should replace your collection method with the query method, write your query and DON'T add the get method at the end of your query
use Maatwebsite\Excel\Concerns\FromQuery;
class VetExport implements FromQuery{
public function query()
{
$vets = Vet::where('is_active', 1);
foreach($vets as $vet){
$reminders = $vet->reminders();
}
return $reminders;
}
}
I hope this helps

Laravel oneToMany accessor usage in eloquent and datatables

On my User model I have the following:
public function isOnline()
{
return $this->hasMany('App\Accounting', 'userid')->select('rtype')->latest('ts');
}
The accounting table has activity records and I'd like this to return the latest value for field 'rtype' for a userid when used.
In my controller I am doing the following:
$builder = App\User::query()
->select(...fields I want...)
->with('isOnline')
->ofType($realm);
return $datatables->eloquent($builder)
->addColumn('info', function ($user) {
return $user->isOnline;
}
})
However I don't get the value of 'rtype' for the users in the table and no errors.
It looks like you're not defining your relationship correctly. Your isOnline method creates a HasMany relation but runs the select method and then the latest method on it, which will end up returning a Builder object.
The correct approach is to only return the HasMany object from your method and it will be treated as a relation.
public function accounts()
{
return $this->hasMany('App\Accounting', 'userid');
}
Then if you want an isOnline helper method in your App\User class you can add one like this:
public function isOnline()
{
// This gives you a collection of \App\Accounting objects
$usersAccounts = $this->accounts;
// Do something with the user's accounts, e.g. grab the last "account"
$lastAccount = $usersAccounts->last();
if ($lastAccount) {
// If we found an account, return the rtype column
return $lastAccount->rtype;
}
// Return something else
return false;
}
Then in your controller you can eager load the relationship:
$users = User::with('accounts')->get(['field_one', 'field_two]);
Then you can do whatever you want with each App\User object, such as calling the isOnline method.
Edit
After some further digging, it seems to be the select on your relationship that is causing the problem. I did a similar thing in one of my own projects and found that no results were returned for my relation. Adding latest seemed to work alright though.
So you should remove the select part at very least in your relation definition. When you only want to retrieve certain fields when eager loading your relation you should be able to specify them when using with like this:
// Should bring back Accounting instances ONLY with rtype field present
User::with('accounts:rtype');
This is the case for Laravel 5.5 at least, I am not sure about previous versions. See here for more information, under the heading labelled Eager Loading Specific Columns
Thanks Jonathon
USER MODEL
public function accounting()
{
return $this->hasMany('App\Accounting', 'userid', 'userid');
}
public function isOnline()
{
$rtype = $this->accounting()
->latest('ts')
->limit(1)
->pluck('rtype')
->first();
if ($rtype == 'Alive') {
return true;
}
return false;
}
CONTROLLER
$builder = App\User::with('accounting:rtype')->ofType($filterRealm);
return $datatables->eloquent($builder)
->addColumn('info', function (App\User $user) {
/*
THIS HAS BEEN SUCCINCTLY TRIMMED TO BE AS RELEVANT AS POSSIBLE.
ARRAY IS USED AS OTHER VALUES ARE ADDED, JUST NOT SHOWN HERE
*/
$info[];
if ($user->isOnline()) {
$info[] = 'Online';
} else {
$info[] = 'Offline';
}
return implode(' ', $info);
})->make();

Resources