简体   繁体   中英

Laravel display data from one database table by using the id of that row

So I have a table pages , that looks like this:

public function up()
    {
        Schema::create('pages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('name');
            $table->integer('category')->unsigned();
]        });

        Schema::table('pages', function($table) {
            $table->foreign('user_id')->references('id')->on('core_users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('category')->references('id')->on('ecommerce_payment_pages_categories')
                ->onUpdate('cascade')->onDelete('set null');

        });
    }


    public function down()
    {
        Schema::drop('pages');
    }

And a table categories looking like this:

public function up()
    {
        Schema::create('ecommerce_payment_pages_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('core_users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('categories');
    }

The pages model looks like this:

class Pages extends Model
{

    protected $table = 'pages';


    public function user() {
        return $this->belongsTo("\App\User");
    }

    protected $fillable = [

    ];

    protected $casts = [

    ];

    public function categories() {
        return $this->belongsTo("\App\Models\Categories");
    }
}

And the categories model looks like this:

class PaymentPagesCategories extends Model
{
    protected $table = 'categories';

    public function user() {
        return $this->belongsTo("\App\User");
    }

    protected $fillable = [

    ];

    protected $casts = [

    ];

    public function pages_categories() {
        return $this->hasMany("\App\Models\\Pages");
    }
}

So in my pages table I save in the column category the id of the category that the user selects from a dynamic select that echoes the category id as value and category names as options. But I cannot save it as simple string because in another menu the user has the option to delete categories, so I want that when a category gets deleted to get deleted from the category column in the page table as well. Right now I display the data from the database like this:

public function getGet()
{
    return response()->json(['success' => true, 'payment_pages' => \Auth::user()->payment_pages()->orderBy('id', 'ASC')->get()]);
}

So for the currently authenticated user I display all the data from the page database table. Right now after the user selects a category from my select I save on the category colum in pages table the ID of the category. I need to make a query in order to display in a different view in html the corresponding to the category id I saved in pages table

Category column in page migration is not set to allow null values. To allow null values append nullable column modifier to category column. Rest of the code seems OK.

Page Migration:

public function up()
{
    Schema::create('pages', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('name');
        $table->integer('category')->unsigned()->nullable(); // <= add this modifier
    });

    Schema::table('pages', function($table) {
        $table->foreign('user_id')->references('id')->on('core_users')
            ->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('category')->references('id')->on('ecommerce_payment_pages_categories')
            ->onUpdate('cascade')->onDelete('set null');

    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM