Laravel 5.4 models relationship - laravel

I'm struggling to find a way to output all NORMS (last table on the right) where JURISDICTIONS (last table on the left) is equal to a parameter given by a select (i did this using eloquent hasManyThrough, and i did output all norms related to that specific Jurisdiction) and where, and here comes the problem, these Norms belongs to a specific category also passes by a select (the smallest table in the image, DETAILREDACS).
NORMS and DETAILSREDACS are ManyToMany related.
Here are the two main attempts:
How can i achieve this? I've tried joining tables, but i'm probably doing something wrong.
I'm using Laravel 5.4.
Thanks!!!

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

Using Eloquent Laravel to show country's levels with big data

I have a table locations (about 35000 rows and more than in the future).
I search on the internet and try it: Eloquent: Recursive hasMany Relationship with Unlimited Subcategories
I try to run it, it kept loading and did not stop. I think so data is big.
In addition, the above solution shows the tree. I want to show the table
I want to set up a query using eloquent model or anything to pass to the view (can use DataTables package)
Thanks so much!

Laravel / Eloquent - Many to many pivot model with morphToMany relation

I have a manyToMany relationship set up and have modelled the pivot table also. Also, in the pivot model I have a morphToMany relationship set up.
Here is a diagrammatic representation of the setup.
The trouble I am having is that I can't pull in the joins attribute on the pivot model.
I have this code in my Dimension model;
return $this->belongsToMany('Datasource', 'dimension_datasource')->withPivot('joins');
But I get this error: Unknown column dimension_datasource.joins
I have tried setting up an accessor on the pivot model but it appears that it is being ignored as I get the same error.
Thanks,
Geoff
This is working now. I'm distressed to admit I am not 100% sure why, but I'm fairly certain it was to do with the character encoding of backslashes in the fully qualified class names in the joinable_type column of the joinables table. So, if you are having the same issue, try looking there (there are indications elsewhere on the internet that Laravel should be escaping those backslashes and also vague suggestion of server settings being relevant).
Sorry I can't be more specific!!

how adding a fields to relationship Table in pivot table method using laravel

I want to design an online store. For each category of productions, we would have its own fields.
Connections between tables of fields and categories are done and displayed in the part of registration of productions.
At the end, the value of each field should be stored that it should contains the related table between table of fields and productions (in addition to/plus/+) the value of that field.
My problem is in this part and I want that this related table has an additional field that I could value it.
In the following figure, the picture of table and my connections are shown, if there is (any problem/ something wrong with it), I would appreciate you to help me.
View Relationship Images :
http://ir-up.ir/uploads/1416568805731.jpg
You'd simply need to add a withPivot when declaring the relationship:
return $this->belongsToMany('Role')->withPivot('foo', 'bar');
Extra attributes in pivot tables are covered in the docs, here - http://laravel.com/docs/4.2/eloquent#working-with-pivot-tables

GAS ORM many-many relation with attributes

I'm using CodeIgniter and I'm starting to work with gas orm.
One of my m-n-relationship-tables using a composite key has also some additional attributes to the releation.
For Example:
Table teams, Table employees, and a m-n table which binds them together + adding the attribute role
Is it possible to get the attribute using GAS ORM?
Yes, it is possible.
Simply create a new relationship in one of the two tables you are going to link with the pivot table that refers to the pivot table itself as a has_many relation. (But dont do the linking stuff in the model file, eg:
ORM::has_many('\\Model\\User\\Role')
instead of
ORM::has_many('\\Model\\User\\Role => \\Model\\Role')
See http://ellislab.com/forums/viewreply/1050559/ for exact the same question.

Resources