I'm working on classifieds site and I will have different types of ads. There will be cars, real estate and so on. All types will have type specific attributes. For example, realestate ads will have square meter attribute which cars doesnt need.
I created database model for that situation but have no idea how to tell that relations to eloquent. Also are there any better approach to this problem?
Ad table
-id
-date
-ad_type_id
Cars table
-id
-title
-horse_power
Realestate table
-id
-title
-squares
What you are looking for is called "Polymorphic Relations".
If you follow the example at https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations and simply replace your tables with theirs with something like this...
cars
id - integer
title - string
horse_power - integer
realestates
id - integer
title - string
squares - integer
ads
id - integer
description - text
adable_id - integer
adable_type - string
You'l be able to do what you want. So you would only need to do
$ads = Ad::with('adable')->get()
that would then be a collection of all ads (with cars and realestates). then you would want to create 3 blades, something like
ad_list.blade.php
ad_car.blade.php
ad_realestate.blade.php
In your ads_list view you would have something like
#foreach ($ads as $ad)
#include('ad_'.snakecase(classbasename($ad->adable_type)))
#endforeach
then in ad_car and ad_realestate format the content how you want
//car view
$ad->adable->title
$ad->adable->horse_power
It's a weird concept and it took me a few tries to get it right. I found https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/10 helpful in explaining the concept of showing polymorphic relations in different views (starts around 8:30). Give that a look over if you get confused.
Related
first image
second image
I want to get Id from students table where i want insert this id in differs table . I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
my student controller
this my differ controller but I do not know any idea about if this code true or no
first: 'app\Differ'? but Differ is in App\Models
you can use hasOne(Differ::class); when Student model and differ in the same folder
the second thing you should know, when using functions of relationship in laravel such as hasOne, a foreign key should be named by student_id in Differ or
you show it in function like hasOne(Differ::class,'id_student','id')
I need to design the structure of the tables with the product data to meet the following requirements:
1. A product consists of the following fields: EAN, CN, description, pvp
2. There are several types of users that access the products. The user types are not stable, they can be added or deleted at any time.
3. Any of the fields of the products may vary depending on the type of user who views it. For example:
We have three users:
1 - John - guest
2 - David - client
3 - Vicent - vip
We have this product with this data by default:
8470001234567 - 123456 - Iphone X - $799
Guest users instead of seeing this data see the following:
8431239876547 - 987654 - Iphone X - $849
The client users see the data by default and the vip users see:
8470001234567 - 654321 - Iphone X Sale - $709
This means that a user sees the default data of a given product unless there is an exception for its type. The exception can affect any field (except the id).
I can think of this structure:
PRODUCTS: id, ean, cn, description, pvp
PRODUCT_EXCEPTION: product_id, user_type_id, ean, cn, description, pvp
I have verified that with this structure many queries are made, do you think of a way to optimize this so that it is not necessary to make so many queries?
Note 1: the products are contained within offers that have a certain number of products.
Note 2: I use Laravel. The Offer model has a relationship with products, that is, I obtain the products in the following way: $offer->products()
Looking at just the problem description, I ended up with this, which is equivalent to what you are proposing with your Product_Exception:
Reasoning:
Since user types vary, you cannot put the values directly in the Product table. Ex. 3 user types == 3 prices. Not here. So you need a link table between Product and UserType.
The link table will contain the characteristics of 1 Product, for 1 UserType.
If you want to have a default value, you can put the characteristics in the Product table as well. But then your queries become bigger! Check if there is an exception value, if not use the default.
So your solution makes sense to me.
I have tables as seen here
An Athlete has many test days (e.g. one for each year), an Institution has teams e.g. Redham High School Rugby U14 Level. The team table is composed of the Institution, a sport and a level. Sport and Level are maintained in Codelist, which is just a table to maintain entities that are not big enough to be a table. Almost like replacing the hard coding of droplists.
Question 1: I don't see that the Codelist is related to the Team table, since Code list store unrelated info as well, like Country names, province names etc. Example of Codelist data below. Should they be related?
Question 2:
When creating a Test day record, I need the user to select for which team this athlete is getting tested for. I'm getting the Team records, but how do I now access the sport and level names from the Codelist?
I can't do $team->sport->name as they're not related tables.
Below is my current code. How do I get user friendly values for sportcode and levelcode e.g. Rugby U14.
$teams = Team::whereHas('Institution', function($query){
$query->whereClient_id(1);
})->get();
I can with $teams loop get the sportcode and level code and now write separate queries to fetch the names from the codelist. This would send 3 collections back to the view and doesn't seem the eloquent way. Advice appreciated.
Hello everybody I'm making a "Bulletin board", like this: http://stena.kg/ad/post, I'm using Laravel 5.0, and don't know how to store different fields in database table, for example if I choose "Cars" category I should to fill Mark, Model, Fuel (etc fields for cars category), If I choose Flats category I should fill fields like Area, Number of rooms etc...How to organize all of this? I tried some ideas but nothing helped me(
Try to save data as json in table. Parse json format to string and save it in db, but it will cause many problems in future, so not recommend that solution. I recommend to store data in separate tabels, each one for category. For optimise process it is possible to create catregory table, and category_item table with fields like name, description and so on. Different category demands sp=ecific fields, so best solution is to create table per category.
I'm trying to implement something similar to the iTunes browser, to browser a simple database of Books. I have the following entities - Author, Genre and Book. I would like to display an author list and a genre list, which act to filter the main Book list.
I have tried doing this in 2 different ways - modeled as:
Author ( has many ) Genres ( has many ) Books
...with multiple instances of the same Genre so each author has their own for a given genre name, I have a nice drill-down hierarchy to display in my table views (albeit a bit illogical to duplicate Genres). However, when I select multiple Authors, I end up displaying dupes of the same Genre, because they are, in fact, distinct objects.
So, I tried doing it, more sensibly, with these relationships:
Author ( has many ) Books
Book ( has one ) Genre
I can get the Genre array by taking the distinct union of Genre's in the current selected Author(s) book array, but now i'm left with the problem of filtering the book list displayed based on the selected Genre(s). Because the Genre's are shared, I can't just use CurrentGenre.books, or I lose the selected Author filtering. I have noticed the 'filter predicate' field in interface builder, available on the object controllers, but am stuck working out how to actually use it to apply the selected Genre as a filter to an episode list. The apple documentation says:
"You can type a predicate directly
into the predicate editor text field
in the inspector panel of Interface
Builder or you can set it
programmatically using
setFetchPredicate:.
which gives me the impression i'm on the right track, but that's about the end of it. I'm trying to lock down the model in a nice Cocoa-esque fashion now, so as to minimize 'glue code' bits and changes later down the track. It seems like a fairly simple problem I should be able to sort out graphically in IB, but so far it's eluded me!
Thanks in advance.
I am a bit confused by your introducing "episodes" in the middle of the discussion, but I will assume you just mean "books" still.
You are definitely on the right track. You want a data model like this:
Author <-->> Book
Genre <-->> Book
Or maybe even:
Author <<-->> Book (if you support reference books, etc)
Genre <<-->> Book (if you want multi-genre support)
Once a user has selected author(s) and genre(s) you will want a Book array controller to use a filter predicate that only shows books with those author(s) or genre(s).
UPDATE
This should work:
Bind the Book array controller's filter predicate to a new predicate property "bookFilterPredicate" in your app delegate.
Add outlets for the Author and Genre array controllers.
Observe changes to the selectedObjects properties of both array controllers.
When either changes, update the filter predicate property like this: self.bookFilterPredicate = [NSPredicate predicateWithFormat:#"author IN %# && genre IN %#",authorArrayController.selectedObjects,genreArrayController.selectedObjects];