dijkr / Copia

Laravel-project with CMS
0 stars 0 forks source link

Change column from a native field to foreign key #1

Closed dijkr closed 1 year ago

dijkr commented 1 year ago

Route::get('/{slug}', function ($slug) { return view('producten', [ 'products' => Product::all() ]); });

For example this. I want the product-method have foreign keys to link data from other tables e.g. category. The reason for this problem to happen is a view that is showing product information, but also need the name and banner / image from the category above the products, linked to that specific category.

dijkr commented 1 year ago

For now it works by also using the category-model within the productController. However, I'm not sure if this is a common / best-practice solution.

dijkr commented 1 year ago

Works with this syntax:

            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories');
            $table->unsignedBigInteger('subcategory_id');
            $table->foreign('subcategory_id')->references('id')->on('subcategories');
            $table->unsignedBigInteger('subsubcategory_id');
            $table->foreign('subsubcategory_id')->references('id')->on('subsubcategories');

To make it work, the foreign key should exist, before the table can be created. So it might be nessecary to change the sequence of a migration.