Laravel whereDoesntHave() – multiple OR conditions

You can use doesntHave and specify the boolean operator: $products = Product::doesntHave(‘categories’)->doesntHave(‘countries’, ‘or’)->get(); Actually you only need whereDoesntHave if you want to pass in a closure to filter the related models before checking if any of them exist. In case you want to do that you can pass the closure as third argument: $products = … Read more

MySQL – How to insert into table that has many-to-many relationship

Here is what i ended up doing. I hope it helps someone. INSERT INTO persons (firstname,lastname) VALUES (‘John’,’Doe’); SET @person_id = LAST_INSERT_ID(); INSERT IGNORE INTO properties (property) VALUES (‘property_A’); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, @property_id); INSERT IGNORE INTO properties (property) VALUES (‘property_B’); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, … Read more

How to define two relationships to the same table in SQLAlchemy

As of version 0.8, SQLAlchemy can resolve the ambiguous join using only the foreign_keys keyword parameter to relationship. publish_user = relationship(User, foreign_keys=[publishing_user_id], backref=backref(‘pages’, order_by=id)) edit_user = relationship(User, foreign_keys=[last_edit_user_id]) Documentation at http://docs.sqlalchemy.org/en/rel_0_9/orm/join_conditions.html#handling-multiple-join-paths

How to view database diagram in a Code First using localdb

Install Entity Framework Power Tools Beta 4, restart Visual Studio, right-click on the context in your solution view and you’ll see a new ‘Entity Framework’ option in the context menu. Select ‘View Entity Data Model’ to see a beautiful visual database diagram in Visual Studio. Voilà! Entity Framework 6 Power Tools: Link

Elasticsearch relationship mappings (one to one and one to many)

There are 4 approaches that you can use within Elasticsearch for managing relationships. They are very well outlined in the Elasticsearch blog post – Managing Relations Inside Elasticsearch I would recommend reading the entire article to get more details on each approach and then select that approach that best meets your business needs while remaining … Read more

Laravel check if relation is empty

There are a variety of ways to do this. #1 In the query itself, you can filter models that do not have any related items: Model::has(‘posts’)->get() #2 Once you have a model, if you already have loaded the collection (which below #4 checks), you can call the count() method of the collection: $model->posts->count(); #3 If … Read more

tech