Relationships "has-one-through" alternative solution Laravel Voyager - laravel

I need to make a relation "has-one-through" but Voyager doesnt support it
The "Consum view" have to show the owner name.
https://i.imgur.com/nDrUgU2.jpg (DB Example)
I've already tried belongsToMany with pivot table but didn`t work!
Show a column from the requested table.

Why donĀ“t you change your relation, having an owner_id or a house_id instead of an owner_house_id in the consum table? Probably that solves it all.

Related

AfterValidation Hook on BelongsToMany Field in Laravel Nova

I have a table "articles", a table "images" and a pivot table article_image with unique constraint on 2 columns: 'article_id' and an additional column 'order_nr'. Now I need to check the uniqueness of article_id/order_id in Laravel Nova when attaching an image to an article.
I already found a solution for a unique validation rule with 2 columns with a AfterValidation hook here:
https://grrr.tech/posts/2021/nova-multi-column-validation/
The problem is that this solution is referring to a resource by itself and not to a "pivot resource".
I tried to put the afterValidation method into the resources 'Article' and 'Image', but the method is simply not accessed from here.
How can I set a afterValidation hook on a pivot resource, thus a BelongsToMany field? Or am I completely a on the wrong track and anybody knows a better solution for that? Thank you in advance!

Is it possible to use inline_create with pivot fields with Laravel Backpack?

Is it possible to use Laravel Backpack relationship field type with both inline_create and pivot fields enabled?
I'm trying to make this happen:
I have 2 entities, Products and Productions, and both have a belongsToMany relation defined in the model. Therefore they have a pivot table with product_id and production_id.
In this pivot table I have a count column.
With Backpack I'm trying to solve the following:
I want to fetch products on productions create operation, or create product from this view and specify the pivot field count with it.
If I remove the 'subfields' property from the field definition, the add field button shows up.
I've attached 3 screenshots of the mentioned functionality.
Product with Add, but without count field
Clicked on "Add" - Modal opened
Product with count pivot field and without Add
Hello #roamingTurkey !
You should be able to do it, but you need to configure your pivotSelect key in your n-n field definition : https://backpackforlaravel.com/docs/5.x/crud-how-to#extra-saving-additional-attributes-to-the-pivot-table
Let me know!

"solved" laravel eloquent model on table with condition

I want to ask if is possible with eloquent making model with condition on column.
for example, i want to make table users with column role enum('teacher','student')
how to make models for student and teacher?
thanks.
use single Table Inheritance (STI) or Sub-classing through this package
caleb porzio parental

How to use field of pivot table

I've two tables user and product, along with their relationship user_product. The relationship table contains an extra field category. Now I want to find
the number of products selected by a user of a particular category.
I've done it in my page through coding. Is there any easier way to do this?
How to use field of pivot table?
you can use withPivot
Ex:
$this->belongsToMany('Role')->withPivot('foo', 'bar');

Eloquent select specific columns

I am trying to select specific columns using Laravel's Eloquent but I am having some issues. I just want to SELECT quantity, prices from prices. I have tried
$prices = Price::select(array('quantity', 'price'))->whereMonthAndYearAndType($month,$year,'cpi')
->with(array('product'=>function($query){ $query->select('id','name'); }))
->with(array('market'=>function($query){ $query->select('id','name'); }))->get();
but I am getting Trying to get property of non-object
There is nothing in your code snippet that would produce that error. However, I suspect you are getting the error when you are trying to access fields on the eager loaded product or market relationships without checking for their existence.
A common problem with specifying select statements and eager loading relationships is that the local and foreign keys usually get left out. However, these are the values that Laravel uses to match up all the related models, so they need to be selected, as well.
If price belongsTo a product and price belongsTo a market, then you also need to select the prices.product_id and prices.market_id fields. If price hasOne product or price hasOne market, then you'll need to select the prices.id field, and the corresponding foreign keys on the eager loaded relationships (products.price_id or markets.price_id).
Though, even once this is resolved, it is still a good idea to check to make sure the related record exists before trying to access it. In a hasOne/belongsTo relationship, if the related record doesn't exist, the relationship property will be NULL. If you try to access a field on NULL, you'll get the error you're seeing.

Resources