| // A little lesson on naming conventions in Laravel Eloquent: // Table 1: product_variants - variations of a certain product // Table 2: variant_usage - pivot table between product_variants and usage // Table 3: usage - the various product variant uses // Usage - returns all usages for the ProductVariant model public function usage() | |
| { | |
| return $this->belongsToMany('App\Usage', 'variant_usage', 'variant_id', 'usage_id'); | |
| // Note: the 2nd/3rd/4th parameters wouldn't be necessary... | |
| // ...if the 'variant_usage' table were named 'product_variant_usage' | |
| // ...or if 'product_variant' were simply named 'variant' | |
| // ...(i.e. - product_variants --> product_variant_usage --> usage) | |
| // Also Note: the ID parameters are the connecting points in variant_usage | |
| // ...(i.e. - product_variants --> {variant_id --> usage_id} --> usage) | |
| } |
The PHP "empty($var)" function (or language construct) is a handy function that I use a lot. It replaces all of the following checks: isset($var) $var !== 0 $var !== 0.0 $var !== "" $var !== "0" $var !== null $var !== false count($var) > 0 // if array This is particularly useful for integers, but can also be useful for checking strings and floats. It replaces a bunch of conditions you'd otherwise have to type. I've had some disagreements over my frequent usage of empty(). There are those who believe explicit value checks are important to code readability. My response is to ask why a longer line of code is any more readable. There are also those who consider '0' to be non-empty, in which case the use of empty() would not be appropriate. However, the majority of the time when you use empty(), it will be to check a MySQL ID value, which will (should) always be greater than 0. Consider the following example: if...
Comments
Post a Comment