Category : pivot-table

i have 3 models 1-book: class Book extends Model { protected $guarded=[]; public function users(){return $this->belongsToMany(User::class);} public function bookUser(){return $this->hasMany(BookUser::class);} } 2-user class User extends Authenticatable { protected $guarded = []; public function books(){return $this->belongsToMany(Book::class);} 3-bookuser class BookUser extends Model { protected $guarded = []; protected $table = ‘book_user’; public function book(){return $this->belongsTo(Book::class);} public function ..

Read more

I am currently writing a search query but im having issues joining a where with a whereHas if i wanna search for categories only.. The below query works if i just search for categories solo $goals = $myGoals ->whereHas(‘categories’, function ($q) use ($search) { $q->where(‘name’, ‘like’, "%$search%"); })->paginate(10); if i wanna search for title only.. ..

Read more

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

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 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

Is there a Laravel (i.e. easy) way to perform an upsert on a pivot table? I’m using the following code: if ($mpi->sites()->where(‘site_id’, $site_id)->exists()) { $mpi->sites()->updateExistingPivot($site_id, [‘pivot_data’=>$pivot_data]); } else { $mpi->sites()->attach($site_id, [‘pivot_data’=>$pivot_data]); } What I want is something like this: $mpi->sites()->updateOrAttach($site_id, [‘pivot_data’=>$pivot_data]); My Mpi model looks like this: class Mpi extends Model { public function sites() ..

Read more

I’ve got Item catalog management system. CatalogItem(model): Schema::create(‘catalog_items’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘name’); $table->float(‘price’); $table->float(‘deposit’)->nullable(); $table->timestamps(); }); Pricing(model): Schema::create(‘pricings’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘name’,127); $table->boolean(‘enable’)->default(true); $table->timestamps(); }); CatalogItemPricingPivot(pivot model): Schema::create(‘catalog_item_pricing’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->bigInteger(‘catalog_item_id’); $table->bigInteger(‘pricing_id’); $table->float(‘discount’)->nullable()->default(0); $table->timestamps(); }); Now when I want to update pricing for CatalogItem I do this: $CatalogItem->pricings()->sync([ ..

Read more