Laravel Validate ForeginKey - laravel-5

I have a table that is fk from another table. when I try to delete the record, I get an error because it is not possible to delete a record that is used as fk in another table. Has anyone ever used laravel's validator to validate this situation?

Related

Join database table based on Json colum having multiple key

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.
You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

How to insert data into destination table without having any primary key in Talend

I am using talend for ETL I don't have enough experience in this, I am having two tables for example- account and account_roles account table is having id, name, password etc fields and account_roles table is having account_id which is f.k to account table's pk. and one more field.
Both the fields in account_roles are having duplicates, I want to save account_roles in destination with update and insert logic using talend.
But I am getting error as I don't have any table that can be treated as primary key in the account_roles table, so talend can't update or insert it.
How I deal with this situation I tried tDBOutput advance option use_field_option but still it requires unique entries.
Is there any possible solution to this issue, I also want to know if I can make table Foreign key in the account_roles table will it work then? If yes then How to make F.k in talend OPen studio is my second question.
Attaching Snapshots of my tables and tMap below -
I want to know the way I can put my tables into database if I don't have any primary key! Kindly help me.
First question
I think you should place the primary key in the physical account_roles table. Talend will use the key indication of the dbOutput component, and the physical key of the table.
In order to delete duplicates rows, you can also use a tUniqRow before the dbOutput: The key you indicate in the UniqRow is not directly linked to the database; this is only the key on which tUniqRow will be based.
Second question
It's not possible to delegate the f.k. verification to Talend. But you can do this verification in your database by placing foreign keys in your table. If an id is not present in the reference table, the database returns an error that is returned by Talend.

Can't update the record because foreing keys

I can't UPDATE (not delete) a record because foreign keys from auxiliar tables doesn't let me do it.
I know I can disabled the foreign keys but I do not consider a well practice if just I'm updating "customer" table, which has not any FK to auxiliar ones.
Any solution to this issue?
My DB-Schema
One of the auxiliar table FK
The error what I'm getting
And this is the code I'm using to do that
$customer = Customer::where('id', $id)->update($data);
if($customer)
return redirect('/customers/);
Thanks to #TheFallen to help me.
In the array I was including the ID of the own record, so I took it out from the array and the update order is working with active foreing keys.

How to delete an entry in a table along with its data in another table?

I have a table cars with attributes: id, name. I also have another table specs with id, car_id, name. This tables are related with one to many relations from the Models. I also have set up the foreign keys in.
I have a controller manageData where i have a function insertCar which i use to insert data and update both tables. I wan to create another function deleteCar from where i can delete the car along with its specs from the other table
Use onDelete() method in migration when defining foreign key:
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
In this case when you'll delete a car record, related data will be automatically deleted from another table.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints

Codeigniter insert and select a field in one query

I am inserting a record successfully in the database table. The primary key (member_id) is set to auto_increment. I want when I insert a member record and a new member_id is created, to retrieve that same record id immediately from the same query. Is that possible?
Regards,
$this->db->insert_id() will get the latest id from the database.
See: http://ellislab.com/codeigniter/user-guide/database/helpers.html
$this->db->insert('...');
$member_id = mysql_insert_id();
This would work also, its the standart PHP function.
http://www.w3schools.com/php/func_mysql_insert_id.asp

Resources