Category : many-to-many

IlluminateDatabaseQueryException with message ‘SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘db_testmasjid.jamaah_model_kajian_model’ doesn’t exist (SQL: select kajian.*, jamaah_model_kajian_model.jamaah_model_id as pivot_jamaah_model_id, jamaah_model_kajian_model.kajian_model_id as pivot_kajian_model_id from kajian inner join jamaah_model_kajian_model on kajian.id = jamaah_model_kajian_model.kajian_model_id where jamaah_model_kajian_model.jamaah_model_id = 4)’ Sourc..

Read more

Trying this for hours now and I don’t see the error. I have a model ‘User’ and a model ‘Round’. I want to define a n:m-relation with a model ‘Flight’ as pivot model. User.php <?php namespace AppModels; use IlluminateContractsAuthMustVerifyEmail; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use SpatieMediaLibraryHasMedia; use SpatieMediaLibraryInteractsWithMedia; use SpatieImageManipulations; use SpatieMediaLibraryMediaCollectionsModelsMedia; class User ..

Read more

Trying this for hours now and I don’t see the error. I have a model ‘User’ and a model ‘Round’. I want to define a n:m-relation with a model ‘Flight’ as pivot model. User.php <?php namespace AppModels; use IlluminateContractsAuthMustVerifyEmail; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use SpatieMediaLibraryHasMedia; use SpatieMediaLibraryInteractsWithMedia; use SpatieImageManipulations; use SpatieMediaLibraryMediaCollectionsModelsMedia; class User ..

Read more

I am new to laravel and trying the following: I have these tables: disciplines: id | name specialties: id | name categories: id | name discipline_specialty (pivot table): id | discipline_id | specialties_id Discipline model: public function specialties() { return $this->belongsToMany(Specialty::class); } Specialty model: public function disciplines() { return $this->belongsToMany(Discipline::class); } My question is: how ..

Read more

I’m trying to implement a many-to-many relationship with more than two tables in Laravel. Migration For Relationship Table public function up() { Schema::create(‘lab_form_detail’, function (Blueprint $table) { $table->unsignedBigInteger(‘color_id’); $table->unsignedBigInteger(‘lab_form_id’); $table->unsignedBigInteger(‘pontics_design_id’); $table->unsignedBigInteger(‘teeth_design_id’); $table->integer(‘quantity’); $table->string(‘teeth_number’); $table->foreign(‘lab_form_id’)->references(‘id’)->on(‘lab_forms’); $table->foreign(‘color_id’)->references(‘id’)->on(‘colors’); $table->foreign(‘pontics_design_id’)->references(‘id’)->on(‘pontics_designs’); $table->foreign(‘teeth_design_id’)->references(‘id’)->on(‘teeth_designs’); }); } Lab Form Model class LabForm extends Model { use HasFactory; public $timestamps = false; public function ..

Read more

I am trying to display an order page for users based on two tables: orders and order_product. The orders table keeps all the useful data for an order, as seen in the model: Order.php protected $fillable = [ ‘user_id’, ‘billing_fname’, ‘billing_lname’, ‘billing_email’, ‘billing_phone’,’billing_address’, ‘billing_county’, ‘billing_locality’, ‘billing_zipcode’, ‘billing_total’, ‘shipped’ ]; public function user(){ return $this->belongsTo(‘AppUser’); } ..

Read more

I have a page which lists out all the teams on a page. I need a query which lists out all the teams but hides specific teams if there id exists within the pivot table under the connection_id field. Team Table $table->bigIncrements(‘id’); $table->integer(‘user_id’)->nullable(); $table->string(‘name’); Teams joint table (links) Schema::create(‘links’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->integer(‘team_id’); ..

Read more

I have a laravel model based on the following table: public function up() { Schema::create(‘things’, function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string(‘label’); $table->foreignId(‘user_id’)->nullable()->constrained(‘users’); }); There is also a pivot table that makes this a many-to-many self-referential model. public function up() { Schema::create(‘thing_thing’, function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string(‘message’)->nullable(); $table->unsignedBigInteger(‘parent_id’)->nullable(); $table->unsignedBigInteger(‘child_id’)->nullable(); $table->unique([‘parent_id’, ‘child_id’]); $table->foreign(‘parent_id’)->references(‘id’)->on(‘things’)->onDelete(‘cascade’); $table->foreign(‘child_id’)->references(‘id’)->on(‘things’)->onDelete(‘cascade’); ..

Read more

I have a table called things that has a pivot table thing_thing. Obviously, this is a self-referential many-to-many relationship. I have the following methods defined in the Thing model: public function parentOf (){ return $this->belongsToMany(Thing::class, ‘thing_thing’, ‘parent_id’, ‘child_id’); } public function childOf(){ return $this->belongsToMany(Thing::class, ‘thing_thing’, ‘child_id’, ‘parent_id’); } My thing_thing migration looks like this: public ..

Read more

I am trying to create a delete method which detaches a individual record from a pivot table but when i delete one it deletes all records associated with the one id. relations public function connections() { return $this->belongsToMany(‘AppTeam’, ‘connections’, ‘team_id’, ‘connection_id’)->withPivot(‘approved’); } public function connections2(){ return $this->belongsToMany(‘AppTeam’, ‘connections’, ‘connection_id’, ‘team_id’)->withPivot(‘approved’); } Method public function deleteConnectionRequest(string ..

Read more

I have many to many relationship between Product and Order. The pivot table is: Schema::create(‘order_products’, function (Blueprint $table) { $table->id(); $table->unsignedBiginteger(‘order_id’); $table->unsignedBiginteger(‘product_id’)->nullable(); $table->integer(‘qty’); $table->integer(‘name’); $table->foreign(‘order_id’)->references(‘id’)->on(‘orders’)->onDelete(‘cascade’); $table->foreign(‘product_id’)->references(‘id’)->on(‘products’)->onDelete(‘set null’); $table->timestamps(); }); I have set on delete "set null" and the line stays in the database. The relationship in the Order model is: public function products() { return ..

Read more

i am a new php programmer and i just started with laravel, i am trying to build a library application and at the moment i am working on the loans part. basically i have a pivot table named copy_user with the following columns: (‘copy_id’, ‘user_id’, ‘date’, ‘return_date’, ‘created_at’, ‘updated_at’) copy_id is one of many copies ..

Read more

I have the below tables users : id | name projects : id | name user_project : id | user_id | project_id My Models contains User.php public function projects() {return $this->belongsToMany(Project::class);} Project.php public function users() {return $this->belongsToMany(User::class);} In my controller file, I want to get a list of users using the User model whose role_id ..

Read more

I would need some help understanding what I am doing wrong. I have really basic ‘required’ fields defined in a PostUpdateRequest rules method. Then, my update method in PostController: public function update(PostUpdateRequest $request, Post $post) { $post->update($request->validated()); $post->tags()->sync(request(‘tags’)); return redirect() … } When I use the method without validation, it works: public function update(Request $request, ..

Read more

i hope you’re having a good day. so i have this ManyToMany relationship where a user can apply to multiple jobs, and a job can have multiple applicants (users). here is the table schema Schema::create(‘applicant_job’, function (Blueprint $table) { $table->id(); $table->unsignedBigInteger(‘job_id’); $table->unsignedBigInteger(‘applicant_id’); $table->unique([‘job_id’, ‘applicant_id’]); $table->foreign(‘job_id’) ->references(‘id’) ->on(‘jobs’) ->onDelete(‘cascade’); $table->foreign(‘applicant_id’) ->references(‘id’) ->on(‘default_users’) ->onDelete(‘cascade’); }); and here ..

Read more

Product model code: public function categories() { return $this->belongsToMany(Category::class); } Category model code: public function products() { return $this->belongsToMany(Product::class); } Product controller code: $products = Product::with(‘categories’)->get(); index.blade.php code: @foreach ($products as $product) <li>Product Name: {{$product->name}}</li> <li>Category: {{ $product->categories->name }}</li> @endforeach When I use the above index.blade.php code the following error shows: Trying to get property ..

Read more