Laravel - Create and Attach - Many to Many relationship
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Creating and Attaching Many-to-Many Relationships with Laravel
In this comprehensive tutorial, we'll work through the process of creating and attaching relationships between multiple models in Laravel. This approach is useful when two tables have a many-to-many relationship. Let's use the example of buying products and their codes.
1. Set up your database relationships with migrations:
a. Create a "buy" table for users' transactions (buys).
b. Create a "codec" table to store all available codecs.
c. Create an intermediate table called "buy_codec" to keep track of which buys are associated with which codecs. This table will have fields for both the "buy" and "codec" tables, as well as a timestamp field to record the time of addition/removal.
2. Define the models:
- Create a Codec model that extends App\Model. Inside your Codec class, include the public function buy() using the belongsToMany() relationship. This will return all buys where this codec is attached.
class Codec extends Model {
protected $guarded = ['id'];
public function buy() {
return $this->belongsToMany('App\Buy');
}
}
- Create a Buy model that extends App\Model. Inside your Buy class, include the public function codec() using the belongsToMany() relationship. This will return all codecs attached to this buy.
class Buy extends Model {
protected $guarded = ['id'];
public function codec() {
return $this->belongsToMany('App\Codec');
}
}
3. Modify the User model for easier data access:
- Create a new User model that extends Authenticatable. Add a public function buy() in your User class using the hasMany() relationship. This will return all buys created by this user.
class User extends Authenticated {
public function buy() {
return $this->hasMany('App\Buy');
}
}
4. Create the forms to attach codecs:
- In your controller, create a form for adding buys with text inputs for name, user associated by their user_id, and dropdowns or checkboxes containing all available codecs (if multiple can be attached). Use Laravel's Form facade for this. In the store method, you'll need to access the data from this form and create a new buy record, specifying the user_id along with any other relevant information.
- Once the buy is created, attach all codecs by iterating over each selected codec in the request array. For each codec, perform an attach() on the pivot table "buy_codec" linking it to the appropriate buy and codec using their respective IDs.
class UserBuyController extends Controller {
public function create($userId) {
$codecs = Codec::lists('name', 'id');
$usr = User::findOrFail($userId);
return view('buy.create', compact('usr', 'codecs'));
}
public function store($userId, Request $request) {
$usr = User::findOrFail($userId)->buy()->create($request->all());
foreach ($request->input('codecs') as $codecId) {
Buy::withTrashed()->find($buyId)->attach('App\Codec', ['codec_id' = $codecId]);
}
return view('buy.index');
}
}
Remember, Laravel offers many built-in features to simplify data management. Make sure you use these tools effectively and efficiently for your application needs.