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.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire