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

Laravel relationships in migrations?

When creating a migration you can specify foreign keys on your tables, i.e. public function up() { Schema::table(‘roles’, function(Blueprint $table) { $table->increments(‘id’); $table->integer(‘user_id’)->unsigned(); //rest of fields then… $table->foreign(‘user_id’)->references(‘id’)->on(‘users’); }); } This will create a foreign key on the user_id column on the roles table. The benefits of foreign keys is that when an update or … Read more

How to find all the relations between all mysql tables?

The better way, programmatically speaking, is gathering data from INFORMATION_SCHEMA.KEY_COLUMN_USAGE table as follows: SELECT `TABLE_SCHEMA`, — Foreign key schema `TABLE_NAME`, — Foreign key table `COLUMN_NAME`, — Foreign key column `REFERENCED_TABLE_SCHEMA`, — Origin key schema `REFERENCED_TABLE_NAME`, — Origin key table `REFERENCED_COLUMN_NAME` — Origin key column FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` — Will fail if user don’t have privilege WHERE … Read more

How do I access the properties of a many-to-many “through” table from a django template?

The easiest way is just to pass the band to the template. Templates are capable of navigating the relationships between models and there is both members and membership_set queryset managers on Group. So here is how I would do it: view: def group_details(request, group_id): group = get_object_or_404(Group, pk=group_id) return render_to_response(‘group_details.html’, {‘group’: group}) template: <h2>{{ group.name … Read more

What rel=profile is for?

In HTML 4.01, there is the profile attribute for the head element: This attribute specifies the location of one or more meta data profiles, separated by white space. For future extensions, user agents should consider the value to be a list even though this specification only considers the first URI to be significant. It is … Read more

Ruby on rails – Reference the same model twice?

Here’s a complete answer to this issue, in case people visiting this question are new to Ruby on Rails and having a hard time putting everything together (as I was when I first looked into this). Some parts of the solution take place in your Migrations and some in your Models: Migrations class CreatePrivateMessages < … Read more