Laravel Model - should I cast foreign index integer keys? - laravel

When I make a new table in Laravel, I generally use the Laravel default id column definition: $table->bigIncrements('id'); Say for example I have a clients table with that in my migration.
If I need a foreign relationship on another table, I 'll do it like this:
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
However, if I get model instances of both of those tables and render them with ->toJson();, the id of the first table will be an integer in JSON, while the client_id of the second table will be a string in the json output.
Now I can make a $casts=[] array in the Model class for the second table to cast it to an integer, but I don't know if there's any reason I shouldn't do that. Or maybe I should ALWAYS do that. Is there any guidelines or suggestions about this sort of thing?

Related

how to store foriegn key ids in new table in mvc in many to many relationship

the problem is that i have made three tables and first two table's primary keys are declared as foreign keys in the third table. now i want to store data in the third table but the problem is that no ids are being passsed to the foreign Key values thats why i am getting errors
as you can see i have shown to drop down lists, one for student and second for courses but when i try to register the record it is not getting ids of primary keys which are declared as foreign keys in the new table

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()

Laravel 5 Eloquent bidirectional foreign key migrations

I've and interesting scenario, I would like to implement in Laravel 5. I have 2 tables.
Users ( Holds the sites users )
Media ( Holds the sites images, documents, svg-s, etc... )
My technical constraints are:
Every user has a profile picture and only one profile picture
Every media has an Author
These contraints I interpret as logical columns on
Users table has a column
- profile_image_id (reference to the Media table id column)
Media table has a column
- author_id (reference to the Users table id column)
This works out well, but becomes problematic when I want to use artisan migrations.
my migrations run in this order
Migrating USERS table
Migrating MEDIA table
My migrations have foreign keys being set as
USERS MIGRATION
$table->foreign('profile_image_id')->references('id')->on('media')->onDelete('restrict')->onUpdate('cascade');
MEDIA MIGRATION
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict')->onUpdate('cascade');
The command I use is
php artisan migrate:refresh --seed
The problem is that when creating the users table, the media table doesn't exist as of yet. And when trying to add the foreign key relation to media table, it produces a SQL error that the media table doesn't exist.
What I did?
I created a function on Users table migration that I ran after the seeding functions had finished
/**
* Veyzon
*
* To be run after the regular run functions on all tables
* have run
*/
public static function delayed_up() {
Schema::table('users', function (Blueprint $table) {
$table->foreign('profile_image_id')->references('id')->on('media')->onDelete('restrict')->onUpdate('cascade');
});
}
This worked out fine and dandy, but when remigrating, then Media table will not delete due to the fact that the user table still has records, where there are profile_image_id-s with values.
Now I thought I will add a few lines of code to the Media tables "down" function, that will set all the USERS table profile_media_id-s to null, but I don't really like this as this seems to tie my separate migration files together and when I declare this as architecturally normal, then this will become a bad practice.
What would you guys do, to create bidirectional One-to-One foreign key relations and integrate them into Laravel migrations?
Easy way to make foriegn key in laravel
public function up()
{
Schema::create('bank_user_details', function (Blueprint $table) {
$table->foreign('country')->references('id')->on('country')
->onUpdate('cascade')->onDelete('cascade');
});
}

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

grocery crud get relation table fields

I have a problem with grocery CRUD and "set_relation_n_n" function:
http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n
I want the fields from "This Table", "Relation Table" and "Selection Table". There are any way to do that?. This is my schema (MySQL):
EDITED:
A few things (by the moment):
This table: timeline_events;
Relation table: timeline_events_options;
Selection table: timeline_events_tags;
id_timeline is the foreign key from another table (not showing in the image above), event_id is a field required for the widget I am using now. id_event is the foreign key from timeline_events, id_event_tags is the foreign key from timeline_events_tags.
Thank you very much for your answers :).
I believe you should repair your tables first. All three tables has primary key named id which could be id_timeline in timeline_events table; id_event in timeline_events_options table and id_tag in timeline_events_tags table. Considering this as your table fields, this should be the function.
$crud->set_relation_n_n('eventtags', 'timeline_events_options', 'timeline_events_tags', 'id_timeline', 'id_tag', 'tags','priority');
I don't guaranty it will work because I didn't test it but you can try this one.

Resources