Laravel - Eloquent Relationships (Query made; Not fetching value) - laravel-4

I am trying to get some database information, and I can see the query is being done, but I am unable to come up with content... I have the follows:
class Vendor extends Eloquent {
public function spider()
{
return $this->hasOne('Spider');
}
}
class Spider extends Eloquent {
public function report()
{
return $this->hasMany('Report');
}
}
So... now I am trying to use it as:
$vendor = Vendor::find(1);
$vendor->spider->report->id;
This is not giving me any result - the id is not being fetched. However, I can tell a correct query is being made to the database...
select * from `report` where `report`.`spider_id` = ?
Why is it not giving me anything? I have tried several ways such as... $vendor->spider->first()->report->first()->id... and so on... but all I get is errors.
Thank's.

Report is of type hasMany so a Collection will be retrieved. Process that Collection to find the instance you want.

Related

Laravel model to get specific piece of data

I am trying to make a function that will help me get needed data quickly.
With all the trials I have been able to get to the following
Tables:
Users (id,name)
Projects (id,name)
User-Project (id, user_id, project_id, manager) where manager is a boolean , there can only be one manager for each project (but employees can still see the project reason why we have a pivot table, manager = 0 for other normal users that can access that project)
In the Project Model I have:
public function Manager(){
return $this->belongsToMany('App\User')->wherePivot('manager', true);
}
In the View I have:
<p><strong>Project Manager:</strong> {{$project->manager}}</p>
On the actual page I get:
Project Manager: [{"id":4,"name":"Daniel Doe","email":"danieldoe#hotmail.com","phone":"70846556","email_verified_at":null,"created_at":"2020-12-20 21:05:50","updated_at":"2020-12-20 21:05:50","pivot":{"project_id":1,"user_id":4,"manager":1}}]
When I change the view to:
<p><strong>Project Manager:</strong> {{$project->manager[0]->name}}</p>
I get:
Project Manager: Daniel Doe
This is what I actually want but I would like to do it from the model if possible. So I tried:
public function Manager(){
return $this->belongsToMany('App\User')->wherePivot('manager', true)->first()->name;
}
But I get the following error:
must return a relationship instance
Can this be done from the model's function?
You can keep your defined relationship, but to access ->first()->name, you'll need to use an "Accessor":
public function manager() {
return $this->belongsToMany('App\User')->wherePivot('manager', true);
}
public function getManagerNameAttribute() {
return $this->manager->first() ? $this->manager->first()->name : 'No Manager';
}
Then, in your code, you simple access:
{{ $project->manager_name }}
If your manager() function returns a Collection of at least 1 record, it will return the name, otherwise it will display 'No Manager' as a fallback.
If you don't want to change the structure of this you can use an accessor to get this information, roughly something like this:
class Project ...
{
public function users()
{
return $this->belongsToMany(...)->withPivot(...);
}
public function getManagerAttribute()
{
return $this->users()->wherePivot('manager', 1)->first()?->name;
}
}
You can do this in different ways, you could use the loaded users relation and use a the Collection methods to filter the manager. You could create another relationship called managers that uses the wherePivot off of users(), etc ...
The only thing to worry about with this setup is that every call to $model->manager would be causing that query, so it may be a good idea to create another relationship manager so that you can load that once and keep using it without the need to keep querying the database:
public function managers()
{
return $this->users()->wherePivot(...);
}
public function getManagerAttribute()
{
return $this->managers->first()?->name;
}
Though, as mentioned already it is probably better to have something like a manager_id on the Project itself.

How can I make a relation from Table A to table C where A related to B and B is related to C in LARAVEL

How can I able display the info from "set Penalties" Table when I do something like: $loanApplication->duration->setPenalties->penalty?
LoanApplication Model:
public function loanDuration()
{
return $this->hasOne('App\LoanDuration', 'id','loan_duration_id');
}
LoanDuration Model:
public function loanApplications()
{
return $this->hasMany(LoanApplication::class);
}
You are almost there. $loadApplication->loadDuration should already give you information from the loan_durations table.
In your LoanDuration Model you should add a new relationship method like this:
public function setPenalties(): BelongsTo
{
return $this->belongsTo(<Your penalty model>);
}
This will allow you to retrieve the penalties from a LoadDuration:
$loadDuration->setPenalties;
Or with the full example, the result will be a collection of setPenalties:
$loanApplication->loadDuration->setPenalties
It could be possible that you wont get any results from the setPenalties. This is because of the name of the related column name in the database. You could either change the name of the column duration_id to loan_duration_id or specify the duration_id in the $this->belongsTo(<Your penalty model>, 'duration_id')

Show all entries where relationship exists (eloquent)

Trying to build an eloquent query to return all results in the database where a relationship exists.
$deliveries = Deliverer::has('deliveries')->get()->toArray();
dd($deliverer);
This is returning results which include multiple deliverers without any deliveries.
Deliverer Model includes:
public function deliveries() {
return $this->hasMany(Deliveries::class)->latest();
}
Delivery Model includes:
public function deliverer() {
return $this->belongsTo(Deliverer::class);
}
Can anybody see what is going wrong here please?

Join 3 tables using Laravel Eloquent

So I have 3 tables
size_sets - id, name
sizes - id, name
size_set_sizes - size_id, size_set_id
I want to define a relationship in size_set model that would retrieve all sizes available for that sizeset
Something like:
public function sizes()
{
//define relationship here
}
Method sizes should retrieve the names from the size table, through size_set_sizes table in the size_set model...
My application is very dynamic and thus I needed to go with this structure. I tried the hasManyThrough relationship, but couldn't get that to work.
100% use a pivot table
https://laravel.com/docs/5.4/eloquent-relationships
This link will give you all you need
Use belongsToMany() relations like:
class Size extends Model
{
public function sizeSets()
{
return $this->belongsToMany(SizeSet::class, 'size_set_sizes');
}
}
class SizeSet extends Model
{
public function sizes()
{
return $this->belongsToMany(Size::class, 'size_set_sizes');
}
}
Then you can do:
$sizeSet = SizeSet::with('sizes')->find($id);
Then $sizeSet->sizes will return a collection of sizes for that size set.
I Think I found what I was looking for The answer is a pivot-table
http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

Laravel eager loading result

I'm having trouble with eager loading in laravel
I have these models:
class Page extends Eloquent {
public function translations() {
return $this->has_many('Pagestl', 'page_id');
}
}
and
class Pagestl extends Eloquent {
public static $table = 'pages_tl';
public function page() {
return $this->belongs_to('Page', 'id');
}
}
I want a specific page with its translation data of a specific language.
I retrieve the data like this:
$page_data = Page::with(array('translations' => function($query) {
$query->where('lang_id', '=', 'nl')->first();
}))->find($id);
De result is ok-ish. I get all the page data and the translation of 1 language, dutch (nl). But in order to get a field from the language data I have to this:
$page_data->translations[0]->attributes['content_or_whatever'];
..which i find ugly. I feel i should only have to do something like:
$page_data->translations->content;
..but that gives me an error (Trying to get property of non-object).
Am I missing something or is this just the way it is?
I don't find it necessary to do eager-loading here.
Both ways of doing it will result in 2 queries: select the Page with the given id and then find its first translation.
So, I would do:
$page = Page::find($id);
and then
$page_data = Pagestl::where_page_id_and_lang_id($page->id, 'nl')->first();
Then, assuming the content field exists in pages_tl table, we'll get it by doing:
echo $page_data->content;
Finally, your code looks alright; you don't have to have the second parameter when defining the relationships unless they are different from what Laravel expects.
I'll let you find where the problem is with your code (I couldn't figure out from the information given - could be something to do with strings.php)

Resources