lundi 24 février 2020

Laravel 5.8 custom primary keys that allows hasMany belongsTo relationships

I am trying create two database tables (boxes and items) that will eventually be coded into 2 models with a hasMany belongsTo relationship in Laravel 5.8

These are the migrations I hope to make (below).

create_boxes_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBoxesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('boxes', function (Blueprint $table) {
            //$table->bigIncrements('id');
            $table->string('box_barcode');      //**want this to be my id that can increment**
            $table->string('sort_description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('boxes');
    }
}

create_items_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateItemsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            //$table->bigIncrements('id');
            $table->string('item_barcode');  //**want this to be my id that can increment**
            $table->string('its_box_barcode');
            $table->string('item_quality');
            $table->timestamps();


        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('items');
    }
}

How do I make Laravel recognize these as ids that I would want to make relationships on in my CRUD? For example:

  • show only the item_barcodes that belong to box_barcode TRB0001 when someone clicks on its box link
  • show only the item_barcodes that belong to box_barcode TRB0002 when someone clicks on its box link
  • etc etc

Unless there is a better way to structure these data tables for relationships

See example below:

enter image description here



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2v1Uy9C
via IFTTT

Aucun commentaire:

Enregistrer un commentaire