Category : many-to-many

I have a Many to Many relationship between Wallet & User Models: User.php: public function wallets() { return $this->belongsToMany(Wallet::class,’user_wallet’,’user_id’,’wallet_id’)->withPivot(‘balance’); } Wallet.php: public function users() { return $this->belongsToMany(User::class,’user_wallet’,’wallet_id’,’user_id’)->withPivot(‘balance’); } And the pivot table user_wallet goes like this: Now I need to get the name of wallets and the balance of that wallet in a table at ..

Read more

I have a Many To Many relationship between User Model & Wallet Model: Wallet.php: public function users() { return $this->belongsToMany(User::class); } And User.php: public function wallets() { return $this->belongsToMany(Wallet::class); } And I want to get wallet list of a single user like this: @forelse($user->wallets as $wallet) <tr> <td>{{ $wallet->id }}</td> </tr> @empty <td colspan="5" class="text-center">No ..

Read more

I have a Many To Many relationship between User Model & Wallet Model: Wallet.php: public function users() { return $this->belongsToMany(User::class); } And User.php: public function wallets() { return $this->belongsToMany(Wallet::class); } And I have these three tables related to Wallets: Table wallets: public function up() { Schema::create(‘wallets’, function (Blueprint $table) { $table->bigIncrements(‘id’); $table->string(‘title’); $table->string(‘name’)->unique(); $table->tinyInteger(‘is_active’); $table->tinyInteger(‘is_cachable’); ..

Read more

Lets say I have a User, Roles and User_Role table. I have a custom pivot model for my User_Role table. In a User model I have a relation like this public function roles() { return $this->belongsToMany(Role::class, ‘User_Role’) ->using(UserRole::class); } I have a global scope inside UserRole model which is not included in this relation. I ..

Read more

I have a page that displays all the records from the ‘Receita’ table like this: public function mostrarTodos() { $receitas = Receita::all()->toArray(); $ingredientes = Ingrediente::all()->toArray(); return view(‘home’, compact(‘receitas’, ‘ingredientes’)); } on the top of the page there is a form with many checkboxes that will filter the results displayed on the front page: <div class="col-span-2 ..

Read more

I have the following table structure as shown in the diagram: Briefly, it is composed of several many-to-many polymorphic relationships as described: many resources can have many sources and the pivot table sourceables contains catalog_number and lot_number information to make each row in the pivot table unique. Many resources could also come from the same ..

Read more

I’m overriding voyager admin panel & trying to store multiple related items in add new product form. My view is <div class="form-group col-md-8"> <label class="control-label">Related Products</label> <select class="form-control chosen-select"> @foreach($products as $product) <option class="form-control" value="{{$product->id}}" name="related_id[]"> {{$product->name}} </option> @endforeach </select> </div> Product Model public function related() { return $this->belongsToMany(‘AppProduct’, ‘related_products’, ‘product_id’, ‘related_id’); } RelatedProduct Model ..

Read more

To make this problem clear, please look first at this image: To make it more understandable. We have three "normal" models (User, Course and Lesson) and two Pivot models. My goal is to attach "course_user" to a lesson. The following code leads to an error: // This gives me the very first enrolled student of ..

Read more

I have a product database. I created tables with similar products. A relationship should have a multi-product to multi-product ratio. I created the product__similar table: product_id (FK) similar_id (FK). How can I create a relationship so that links are formed in both directions? I created the relationship: public function productSimilar() { return $this->belongsToMany(Product::class, ‘product__product_similar’, ‘product_id’, ..

Read more

Restaurant Table id —> PK Name Address City Phone Latitude Longitude Categories Table id–>PK section_id —> FK parent_id —> for categories and sub-categories category_name–> slug categories_restaurants_table id-> PK category_id –> FK restaurant_id –> FK Now I want to establish this many-to-many relations in the Product Model… Is this possible? How can I insert update delete ..

Read more

Basically I created an Model Tags and Project. Project Model: protected $fillable = [ ‘title’, ]; public function tags() { return $this->belongsToMany(Tags::class); } Tags Model: use HasFactory; protected $fillable = [ ‘coding’, ‘name’, ]; public function projects() { return $this->belongsToMany(Project::class); } I also did a migration : Schema::create(‘tag_project’, function (Blueprint $table) { $table->foreignId(‘tag_id’)->constrained(); $table->foreignId(‘project_id’)->constrained(); }); ..

Read more

Basically I created an Model Tags and Project. Project Model: protected $fillable = [ ‘title’, ]; public function tags() { return $this->belongsToMany(Tags::class); } Tags Model: use HasFactory; protected $fillable = [ ‘coding’, ‘name’, ]; public function projects() { return $this->belongsToMany(Project::class); } I also did a migration : Schema::create(‘tag_project’, function (Blueprint $table) { $table->foreignId(‘tag_id’)->constrained(); $table->foreignId(‘project_id’)->constrained(); }); ..

Read more

I have 3 models: Movie, Celebrity, Role. Each movie has many celebrities and each celebrity has many roles in many movies (for example the movie "Once Upon a Time" has celebrity "Quentin Tarantino" as roles ["Director","Writer"], and obviously "Quentin Tarantino" can have other movies performing different roles. What is the best way to implement a ..

Read more

I have a case with several models. Order Model, Product Model, Port Model, and Logistic Model. With an Order belongsTo Many Products, Order Belongs To Port with ‘to_port_id’ ,Order Belongs To Logistic with ‘logistic_id’, Product belongs To Port with ‘from_port_id’, from_port_id , to_port_id ,logistic_id and unit_price combined with a pivot table . And now I ..

Read more