Laravel 8 relationship of three models which belongs to Parent model - php
Hello everyone I'm currently working on a laravel project where I have a parent table that has the id's of three tables referenced to it. These table migrations also have their models respectively. Here are the table migrations files respectively:
create_products_table.php
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_id', 10);
$table->string('product_name');
$table->string('image');
$table->string('images');
$table->string('product_description');
$table->bigInteger('size_id')->unsigned();
$table->string('color');
$table->string('product_quantity');
$table->string('old_price');
$table->string('discount');
$table->string('product_price');
$table->bigInteger('user_id')->unsigned()->nullable();
$table->bigInteger('category_id')->unsigned();
$table->bigInteger('gender_id')->unsigned();
$table->timestamps();
$table->foreign('size_id')->references('id')->on('sizes')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('gender_id')->references('id')->on('genders')->onDelete('cascade');
});
create_genders_table.php
Schema::create('genders', function (Blueprint $table) {
$table->id();
$table->string('gender_class');
$table->timestamps();
});
create_categories_table.php
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('cat_name');
$table->timestamps();
});
create_sizes_table.php
Schema::create('sizes', function (Blueprint $table) {
$table->id();
$table->string('sizes');
$table->timestamps();
});
Also this is how I defined the relationships on their models respectively
Product.php
public function category()
{
return $this->belongsTo(Category::class);
}
public function gender()
{
return $this->belongsTo(Gender::class);
}
public function size()
{
return $this->belongsTo(Size::class);
}
Category.php
public function products()
{
return $this->hasMany(Product::class);
}
Gender.php
public function products()
{
return $this->hasMany(Product::class);
}
Size.php
public function products()
{
return $this->hasMany(Product::class);
}
I'm actually a laravel beginner and I studied eloquent model relationships at laravel.com so what I did was just based on my understanding of one to many relationships. When I check all my request with dd($request), category_id, gender_id, size_id all show null and I believe it's because I didn't define the relationship properly. Now this is where I seriously need your assistance.
So please my experienced developers I seriously need your help I'll really be grateful if I get your replies today. Thanks in advance.
=>Everything is right, just make changes in the products migration add this code.
$table->foreign('size_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('gender_id')->references('id')->on('products')->onDelete('cascade');
=>and migrate table
Related
Laravel paginate polymorphic relationship, topic->comments()
I'm building a basic forum, there are many topics and each topic has comments, a comment or "commentable" can be a a comment or a reply (commented a comment), this is because I want to have nested comments, that's where parent_id is useful. I'm wondering if my polymorphic relationship is the best way to achieve this so I'm open to any suggestions. The first issue I came up with is I don't know hw to do ajax pagination of topic->comments ..., I thought with something like Comment::whereHas('topic') ... but my comments dont' have a topic relationship defined in the model. I'm using laravel advanced ajax pagination, how can I paginate comments? Topic model: class Topic extends Model { protected $table = 'topics'; public function author() { return $this->belongsTo('App\Models\User', 'user_id'); } public function comments() { return $this->morphMany('App\Models\Comment', 'commentable')->whereNull('parent_id'); } public function up() { Schema::create('topics', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('user_id')->index(); $table->foreign('user_id')->references('id')->on('users'); $table->string('title'); $table->text('body'); $table->string('url')->unique(); $table->string('slug')->unique(); $table->boolean('isVisible')->default(false); $table->timestamps(); }); } } Comment model: class Comment extends Model { protected $table = 'comments'; public function author() { return $this->belongsTo('App\Models\User', 'user_id'); } public function replies() { return $this->hasMany('App\Models\Comment', 'parent_id'); } } public function up() { Schema::create('comments', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('user_id')->unsigned(); $table->integer('parent_id')->unsigned(); $table->integer('commentable_id')->unsigned(); $table->string('commentable_type'); $table->text('body'); $table->timestamps(); }); } This is where I took inspiration:
Eloquent many to many relationship is always empty
I know this question has been asked a lot but all answers didn't seem to work for me - or at least the questions I found were about the pivot table. I have a many to many relationship (User - Appointment) which is joined by the pivot table "apointment_user", see migrations below. Schema::create('appointment_user', function (Blueprint $table) { $table->unsignedInteger('user_id')->nullable(); $table->foreign('user_id')->references('id')->on('users'); $table->unsignedInteger('appointment_id')->nullable(); $table->foreign('appointment_id')->references('id')->on('appointments'); $table->primary(['user_id','appointment_id']); $table->timestamps(); }); Schema::create('appointments', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->dateTime('date'); $table->string('location'); $table->dateTime('departure'); $table->timestamps(); }); Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->date('last_login')->nullable(); $table->rememberToken(); $table->timestamps(); $table->softDeletes(); }); class User extends Model { protected $with = ['appointments']; public function appointments() : BelongsToMany { return $this->belongsToMany(Appointment::class); } } class Appointment extends Model { public function users() : BelongsToMany { return $this->belongsToMany(User::class); } } I have a user with the ID 1 and about 10 appointments, which I do attach to the relationship in a seeder. The pivot table has 10 records, as intended (User ID is always 1). However, if I dump my User object using dd(User::find(1)), the relationship is always an empty collection. However, a 1:n relationship (between a role works well). Does anybody see what I'm missing? Any help is appreciated. Many thanks and kind regards Edit I just tried some other kind of dumping. I've simply returned my User-Object as JSON-response and there the relationship is filled with 10 appointments... strange.
Though it seems that your table and column names are as Laravel would guess, have you tried expliciting the names? class User extends Model { protected $with = ['appointments']; public function appointments() : BelongsToMany { return $this->belongsToMany(Appointment::class, 'appointment_user', 'user_id', 'appointment_id'); } } class Appointment extends Model { public function users() : BelongsToMany { return $this->belongsToMany(User::class, 'appointment_user', 'appointment_id', 'user_id'); } }
Implementing relationship in models
I have two models: Dish and DishCategory. I decided to implement a "One to many" relationship. Here's a migration for Dish model: Schema::create('dishes', function (Blueprint $table) { $table->increments('id'); $table->string('dish', 50); $table->string('photo'); $table->double('price', 8, 2); $table->integer('category_id'); $table->integer('type_id'); /* 1 - menu for delivery; 0 - general menu */ }); And a migration for DishCategory model: Schema::create('dish_categories', function (Blueprint $table) { $table->increments('id'); $table->string('category'); }); I've created a method called dish() in DishCategory model: public function dish() { return $this->hasMany('App\Dish'); } And dish_category() in Dish model:3 public function dish_category() { return $this->belongsTo('App\DishCategory', 'category_id'); } I'm trying to set up a foreign key in my relationship, so it's been set up in dish_category() method as a second parameter of belongsTo(). But it doesn't work. What is the workaround?
Change the dish() relationship definition to: public function dish() { return $this->hasMany('App\Dish', 'category_id'); } And dish_category() is defined correctly. If you also want to add a constraint, add this to the dishes table migration: Schema::table('dishes', function (Blueprint $table) { $table->foreign('category_id')->references('id')->on('dish_categories'); });
one to many And one to many relationship laravel
This is projects migrate Schema::create('projects', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('start_date'); $table->string('end_date'); $table->string('con'); $table->timestamps(); }); and this is timesheets migrate Schema::create('timesheets', function (Blueprint $table) { $table->increments('id'); $table->string('user_id'); $table->string('project_id'); $table->string('day'); $table->string('month'); $table->string('year'); $table->string('jalali'); $table->string('timesheet_h'); $table->string('timesheet_m'); $table->timestamps(); }); and this users migrate Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('mobile'); $table->string('salary_base'); $table->string('salary_base_h'); $table->string('start_contract'); $table->string('end_contract'); $table->string('start_insurance'); $table->string('end_insurance')->nullable(); $table->string('first_salary'); $table->string('date_birth'); $table->string('melli_code'); $table->string('s_number'); $table->string('nda'); $table->string('work_rules'); $table->string('end_work')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); This my project model public function timesheets() { return $this->hasMany(timesheet::class,'project_id'); } This my timesheet model : public function projects() { return $this->belongsTo(project::class,'project_id'); } public function users() { return $this->belongsTo(User::class,'user_id','id'); } and This my User model public function project_peoples() { return $this->hasMany('App\project_people'); } public function timesheets() { return $this->belongsTo(timesheet::class); } public function projects() { return $this->belongsTo(project::class); } Now I return my query from projects public function allProject() { $projects=project::with(['timesheets','users'])->get(); return $projects; } This is ok but user_id Users in timesheets.user_id And I can not get it out timesheets and get it This controller return project and timesheet by project_id in timesheet but user_id in timesheet I do not know how to get this into the system
Use dot syntax to load nested relationships: project::with('timesheets.users')->get();
[{"id":1,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0627\u06cc\u0645 \u0634\u06cc\u062a","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 10:54:11","updated_at":"2018-01-02 10:54:11","timesheets":[{"id":7,"user_id":"2","project_id":"1","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"5","timesheet_m":"24","created_at":"2018-01-02 12:40:12","updated_at":"2018-01-02 13:47:09","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":8,"user_id":"1","project_id":"1","day":"13","month":"10","year":"1396","jalali":"1396\/10\/13","timesheet_h":"10","timesheet_m":"10","created_at":"2018-01-03 05:59:13","updated_at":"2018-01-03 05:59:13","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":9,"user_id":"2","project_id":"1","day":"14","month":"10","year":"1396","jalali":"1396\/10\/14","timesheet_h":"10","timesheet_m":"15","created_at":"2018-01-04 07:17:44","updated_at":"2018-01-04 07:17:44","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":10,"user_id":"2","project_id":"1","day":"16","month":"10","year":"1396","jalali":"1396\/10\/16","timesheet_h":"10","timesheet_m":"60","created_at":"2018-01-06 07:17:21","updated_at":"2018-01-06 07:17:21","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}}]},{"id":2,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0633\u062a\u06cc","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 11:28:03","updated_at":"2018-01-02 11:28:03","timesheets":[{"id":3,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/11","timesheet_h":"8","timesheet_m":"12","created_at":"2018-01-02 11:39:46","updated_at":"2018-01-02 11:39:46","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":4,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/10","timesheet_h":"4","timesheet_m":"11","created_at":"2018-01-02 11:40:41","updated_at":"2018-01-02 13:49:45","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":6,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"1","timesheet_m":"12","created_at":"2018-01-02 12:06:31","updated_at":"2018-01-02 13:49:37","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}}]}] This is my return i have 2 users but repeat my 2 users :(
Laravel 5.5 BelongsToMany returns empty
I'm trying to use a belongsToMany relation, i never had any problems with it but for some reason it returns empty. I've set up my relation in the Store model like this: public function images() { return $this->belongsToMany('App\Images', 'images_stores', 'image_id', 'store_id'); } When i test the relationship it returns no error just a empty collection although it's not. This is the way i access the data: public function edit($id) { $store = Store::find($id); dd($store->images); return view('admin.pages.store.edit', compact('store')); } But it would just return a empty collection, i hope someone could help me out with this. These are my migration file's but i don't think the problem is inside the migration files. Schema::create('images', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('path'); $table->timestamps(); }); // pivot table Schema::create('images_stores', function (Blueprint $table) { $table->increments('id'); $table->integer('image_id')->unsigned()->nullable(); $table->foreign('image_id')->references('id')->on('images')->onDelete('cascade'); $table->integer('store_id')->unsigned()->nullable(); $table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade'); $table->timestamps(); }); // store table Schema::create('stores', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('location'); $table->string('email'); $table->timestamps(); }); Thanks guys.
I think the ids are inverted. Try this: public function images() { return $this->belongsToMany('App\Images', 'images_stores', 'store_id', 'image_id'); }