Joining table created_at and updated_at values not being set - php

I have two models, Product and Attribute. There is a 'product_attributes' joining table between 'products' and 'attributes'. The relationship is defined in both models using belongsToMany(), and the key of the table is a compound one of product_id and attribute_id.
I can successfully retrieve and store records in the joining table - so, as such everything is functioning as expected, with the exception that the created_at and updated_at in product_attributes is not being set.
Is the above by design, and is there anything I can do to rectify this?

You should use the belongsToMany function with the timestamps declaration like this:
$this->belongsToMany(<entity>)->withTimestamps();

Related

Laravel: fetch pivot relationship through another relationship

Using Laravel, I'd like to fetch an order status (order_status table) from an order (orders table) based on the
locale relationship stored in the orders table using a pivot table (order_status_locale table).
The pivot table contains the "localised" order status from the order.
Of course, I could fetch the localised order status (order_status_locale) directly from the database, but I'd like to
fetch the localised order status through a Laravel relationship.
Something like this:
$order = Order::find(123);
$order->localisedOrderStatus->label;
This is an example of the database structure:
orders:
id: int
locale_id: int
...other columns
locales:
id: int
...other columns
order_statuses:
id: int
...other columns
order_status_locale:
order_status_id: int
locale_id: int
label: varchar
its sometimes better to make model of your pivot table. like OrderStatusLocale.php
then you can make the relation through it.
lastly if you want to achieve
$order->localisedOrderStatus->label;
you can use laravel has one through. or i think its better to go through the relation like $order->order_status->locale->label

How to use whereIn query for hasMany associated model using laravel 5.4 Eloquent

I have one table employee_profile, I am using this table for searching employees, it's columns are id, user_id, experience, salary.
I have two more tables
employee_keyskills with columns user_id and keyskills_id
employee_preferred_cities with columns user_id and city_id
Now I have to find employees with experience, salary, keyskills[], salaries[]
Just I need help in finding results from hasMany associated tables, means employees should be filtered only with specified keyskills[] and cities[] from employee_keyskills and employee_preferred_citites tables.
you should use whereHas which allow you to add customized constraints to a relationship constraint.
check this
https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence

Pluck some rows of a table in laravel 4

I have subject table that contains
id name
and languages table that contains
id subject_id
and division table
id name
finally subject-division table (pivot table)
id subject_id division_id
now exist one-To-one relationship between subject table and languages table and many-To-many relationship between subject table and division table, i need to pluck only the subjects of subject table without the languages by using the relationship function
now i can get the languages only of the subject table of the relationship function In Division Model as the following
public function langSubject ()
{
return $this->belongsToMany('Subject' , 'subject_division','division_id','subject_id')
->join('lang_subject', 'lang_subject.subject_id' ,'=', 'subject.id')->get();
}
But till now I can't get the subjects only without the languages
Any Suggestions ?
You need to add the clause ->select('tableName1.fieldName1','tableName2.fieldName2','tableName3.fieldName3') to your statement after the join statement to get the tableName.fieldName and you may need to use leftJoin instead of join to get the results whether if there is a match or not.
Check out Eloquent relationship documentation -> sub headline "Querying Relationship Existence". There it mentions ::has method, which you could use in fashion
Division::has('subject.lang', '<', 1)->get()
Or at least that's the theory. Haven't had the need for it yet ;-)
It should be supported by Laravel 4 as well.

Update table without primary key

Im building a shop and each product has a unique set of attributes.
the product db looks like this:
products_id, relation_id, product_name, description,
price, glass, shipping, img, cat, subcat, model
Since every product has several (~40) different attributes unique to that product only ive created a second table to store them.
products_id, att_name, att_val, att_head, att_standard, att_order
This works fine, because there will never be two unique rows.
The problem, however, is when i need to modify the attributes content.
using MySQL Workbench i can modify a row using something like
UPDATE product_attributes SET att_val='1500' WHERE products_id='112' AND att_head='threshold'
This however, doesn't seem to work when i update from my php script.
Is there an easy way to modify the table to support updating?
Im well aware of the stupidity not having an unique column.
But im not sure how to make the two tables relate.
Where should i store the unique id of the attributes?
One choice,
add a primary key "auto_incremented" into the product_attributes table...
ALTER TABLE `product_attributes` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
This Id is just for CRUD (Create Read Update Delete) task.
The only relation you can have between your two tables is the products_id wich allow you to have few product_attributes for one product
Since 1 product has more than 1 unique attributes that u store in a second table, you should use the ID of the table product and store it in the second table with the attributes.
Hope this is what u need?

phpactiverecord one to many assosiation

I'm using phpactiverecord for this project, I have this db structure:
Tables: Tickets, Labels
I'm currently using a has_many association for these two tables: "Tickets has many labels", however I need to assign each label to different ticket, which is not possible at the moment without having duplicated rows with different id and ticket_id values.
Any idea on how to achieve this ?
Cheers
You need create additional table
table Label2Tickets (`label_id`, `ticket_id`, PRIMARY KEY (`label_id`, `ticket_id`)
\* there you may store date of create, status and etc *\
`created`, `status`, `user_id` ... )
And update existing AR and create new AR for this table:
Ticket HAS MANY Label2Tickets where Label2Tickets.ticket_id = Tickets.id
Label HAS MANY Label2Tickets where Label2Tickets.label_id = Labels.id

Categories