Laravel Eloquent – Attach vs Sync

attach(): Insert related models when working with many-to-many relations No array parameter is expected Example: $user = User::find(1); $user->roles()->attach(1); sync(): Similar to the attach() method, the sync() method is used to attach related models. However, the main differences are: sync() accepts an array of IDs to place on the pivot table Secondly, most important, the … Read more

Laravel save / update many to many relationship

tldr; Use sync with 2nd param false Many-to-many relationship is belongsToMany on both models: // Task model public function users() { return $this->belongsToMany(‘User’, ‘user_tasks’); // assuming user_id and task_id as fk } // User model public function tasks() { return $this->belongsToMany(‘Task’, ‘user_tasks’); } In order to add new relation use attach or sync. Difference between … Read more

Implementation difference between Aggregation and Composition in Java

Composition final class Car { private final Engine engine; Car(EngineSpecs specs) { engine = new Engine(specs); } void move() { engine.work(); } } Aggregation final class Car { private Engine engine; void setEngine(Engine engine) { this.engine = engine; } void move() { if (engine != null) engine.work(); } } In the case of composition, the … Read more

Laravel where on relationship object

The correct syntax to do this on your relations is: Event::whereHas(‘participants’, function ($query) { return $query->where(‘IDUser’, ‘=’, 1); })->get(); This will return Events where Participants have a user ID of 1. If the Participant doesn’t have a user ID of 1, the Event will NOT be returned. Read more at https://laravel.com/docs/5.8/eloquent-relationships#eager-loading

Laravel orderBy on a relationship

It is possible to extend the relation with query functions: <?php public function comments() { return $this->hasMany(‘Comment’)->orderBy(‘column’); } [edit after comment] <?php class User { public function comments() { return $this->hasMany(‘Comment’); } } class Controller { public function index() { $column = Input::get(‘orderBy’, ‘defaultColumn’); $comments = User::find(1)->comments()->orderBy($column)->get(); // use $comments in the template } } … Read more

Laravel – Eloquent “Has”, “With”, “WhereHas” – What do they mean?

With with() is for eager loading. That basically means, along the main model, Laravel will preload the relationship(s) you specify. This is especially helpful if you have a collection of models and you want to load a relation for all of them. Because with eager loading you run only one additional DB query instead of … Read more

How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?

One-to-one: Use a foreign key to the referenced table: student: student_id, first_name, last_name, address_id address: address_id, address, city, zipcode, student_id # you can have a # “link back” if you need You must also put a unique constraint on the foreign key column (addess.student_id) to prevent multiple rows in the child table (address) from relating … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)