How to Bind to Eloquent Model Event in Laravel 5

Whenever a Laravel model is modified there are a number of events that fire that allow you to trigger your own action(s). For example, in my PoliticsEQ application I needed to calculate some statistics about keywords and sentiment scores and store them in a separate table to improve performance for some of the front in graphs. The challenge was making sure the keyword statistics table was updated whenever a keyword was added or updated.

Laravel gives you the following events right out of the box: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored. You can get the particulars here. Most of them are pretty intuitive.

In my case it made sense to use the “saved” event. To make this happen was remarkably. All I had to do was create a new “Service Provider” and use the Event::saved pattern in the provider’s boot method.

So first I created a KeywordStats provider.


namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Keyword, App\KeywordStat;
use Log;

class KeywordStats extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Keyword::saved(function($keyword) {
          $stat = KeywordStat::where('keyword_name', $keyword->name)->first();
          if (!$stat) {
            $stat = new KeywordStat(['keyword_name'=>$keyword->name]);
          }
          $stat->sentiment_avg = Keyword::where('name', $keyword->name)->avg('sentiment_score');
          $stat->total_usages = Keyword::where('name',$keyword->name)->get()->count();
          $stat->save();
          Log::info("Updated {$keyword->name} / avg: {$stat->sentiment_avg} / count: {$stat->total_usages}");
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        
    }
}

Then I simply registered the provider in the config/app.php providers array:


    App\Providers\KeywordStats::class,

How to Be a Good WordPress Developer?

Pippin has some thoughts. I concur with most of them, especially the one about challenging yourself. The best self-taught developers are those who become obsessed with solving problems. They can’t stop themselves. They might get discouraged and walk away from the computer in disgust. But a few minutes later they’ll think “but I haven’t try this” and be right back at it.

Good developers surround themselves with people who know more than they do. They have no real desire to show how smart they are but would rather learn something smart from others. If you can let your pride go and be ok being the dumbest person in a room full of the smartest people ( as opposed to being the smartest in the room ) you’ll be better than you’d ever imagined otherwise. Plus people will like working with you which helps .

If your looking for a more “skill” based assessment of what it takes to be a developer, this is a pretty good article. Specifically, I like the notion of “problem decomposition,” which is a fancy way to say troubleshooting.

A key aspect of solving a problem ( and thus being a good developer ) is not getting handcuffed by what you don’t know. Obviously, if you knew you wouldn’t have a problem. But just because you don’t know what is causing a specific issue doesn’t mean you know nothing. I often start troubleshooting by clarifying and confirming what I do know.

For instance,  if WordPress gives me an “Error establishing a database connection” screen: What do I know? Do I know my username and password are correct? Can I confirm that by connecting to mysql via the command line or PhpMyAdmin? Do I know that mysql is running on the server?  Again does phpmyadmin work? Does my hosting dashboard have a mysql “status” icon”? Or does <code>service mysql status</code> return something? So on and so forth.

Confirming what you know sometimes exposes that what you think you know may not actually be true. And if it is true, it helps you focus your theories of the problem on areas that really are unknown. Like anything else, the more you decompose problems the better you’ll be at it.

Add tag-like functionality to your pod inputs

Yet another update: The code below expects the name of your pick field to correspond to the name of the related pod type ( i.e. a pickfield name “genre” should relate to a pod called “genre” , as opposed to “file_type”).

Warning: My code highlighter is gimping up the code a little. If you plan to copy/paste, please download the text file instead of copying straight from the webpag.

Try the DEMO
(login using guest/guest).

Update: The demo was down yesterday because I was playing with some code. Sorry about that.

Update 2:
Also note that if this input helper isn’t working on your site make sure that you’ve enqueued the “suggest” script which ships automatically with WordPress. Do this by adding the following line of code to your functions.php file:

if (!wp_script_is('suggest', 'queue') && !wp_script_is('suggest', 'to_do') && !wp_script_is('suggest', 'done'))  {
        wp_enqueue_script('suggest');
}

So this isn’t really a helper so I’m not going to add it to the PodsCMS helper gallery. However, I think it’s a useful bit of code for pods developers.

The problem: You prefer using pods for developing content that needs lots of relationships managed seamlessly. Post types and tags just won’t cut it. But man, you miss that slick tag UI that WordPress has where you just start typing, hit enter and POW the tag is added. Pods doesn’t have anything like this natively.

The solution:

First create a new input helper and paste in the following code:


Add
x  

This helper creates an input field and calls up some jQuery magic to make it auto-complete. But in order for this magic to work you need to add a function to your functions.php (or a plugin file if that’s the case) and hook it to the wp_ajax_{$action} hook. For more about this hook check Gary Cao’s article.

add_action('wp_ajax_pods_suggest','ajax_pod_search');
function ajax_pod_search() {
if(isset($_GET['q'])) {
$str = $_GET['q'];
if(strlen($str) < 2) { die; }
$pod = $_GET['datatype'];
global $wpdb;
$query = $wpdb->prepare("SELECT name FROM {$wpdb->prefix}pod_tbl_{$pod} WHERE name LIKE '%s'", '%'.like_escape($str).'%'); 
$results = $wpdb->get_col($query);
echo join( $results, "n" );
} 
die;
}

Now simply deploy the input_helper on your chosen multi-pick field pod.

But wait! It’s not saving your pod items. That’s because PodsCMS expects to receive a list of comma-separated pod ids to register the relationship. But our jQuery is outputting the name instead.

So we now need to add a pre-save helper to manage migrating tag names to ids.

save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
} else {
$pid = (is_array($pid)) ? $pid[0] : $pid;
}
$vals[] = $pid; 
endforeach; 
} else {
$pid = pod_check($field, $name, 'name'); 
if(!$pid) 
{ 
  $new_tag = new PodAPI();
  $pid = $new_tag->save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
} else {
$pid = (is_array($pid)) ? $pid[0] : $pid;
}
$vals = $pid; 
} 
$val_str = implode(',',$vals);
$columns[$field]['value'] = $val_str;
endif;
?>

Note that we have to tell the helper the field to which we want it to apply. We do this by setting the $field variable. If you are going to use the input helper on multiple fields, you’ll want to create a pre-save helper for each field. This helper will iterate over the submitted pod names, check to see if that pod exists already and getting its id. If the pod name does not already exist, the helper will create it.

Note that this helper makes use of a function called “pod_check”. You will need to add this function to your functions.php file as well.

function pod_check($datatype, $value='', $field='id') {
    global $wpdb;
    $datatype = pods_sanitize($datatype);
    $field = pods_sanitize($field);
    $return = $wpdb->get_col($wpdb->prepare("SELECT `id` FROM `{$wpdb->prefix}pod_tbl_{$datatype}` WHERE `$field` = ".('id'==$field?"%d":"%s"),array($value)));
    if (true == $return && 0 < $return) {
    return $return;
    } else {
    return false;
    }
}

This function actually does the work of looking up the pod id.

And that's it. You can make any pod into a "tag" using this approach. But keep in mind that if you are using this field to relate a pods with required fields, you will need to modify the pre-save helper to add those required fields.

Meta Tag Titles and Descriptions for Your WordPress site.

There are many good plugins out there the help you easily add meta tags to your site. All-in-one-SEO is probably the most popular of all the options. But there several reason why you may want to manually add this code to your template.

For one, you may be a template designer and want your templates to come pre-installed with this SEO friend feature.

Second, you may be having issues with Facebook link sharing not working. WordPress plugins use the wp_head() hook to access your template. But you have no control over the order. If you are using more than a dozen plugins your meta tags can get buried under tons of other code. This can prevent some bots from finding them. Indeed on several of my site Facebook’s bot was not finding them.

Third, it is just always good to do things without plugins if possible. Call it Occum’s Razor of web site building, the less code you use, the better.

Luck for us, it is extremely simple to add code for meta tags and descriptions without knowing much about wordpress or PHP.

Open the header.php file in your theme and paste in the following code:

<?php if(is_singular()) { ?>
<?php
global $post;
?>
<?php $recent = new WP_query('p='.$post->ID);
while($recent->have_posts()) : $recent->the_post(); ?>
<meta name="title" content="<?php the_title(); ?>">
<meta name="description" content="<?php the_content_rss('', TRUE, '', 50); ?>">
<?php endwhile; ?>
<?php } else { ?>
<meta name="title" content="<?php bloginfo('title'); ?>">
<meta name="description" content="<?php bloginfo('description'); ?>">
<?php } ?>

So what’s going on here? First, we’re checking to see if the post is a single post using the WordPress Conditional tag <?php if(is_singular()); ?> because we’ll to pull the title and the description of the individual post for our meta tags. But to do this, we need to get some information about the post, which is why we use the call global $post. This will give us information about the current post. Particularly it allows us access to $post->ID to query information about the post using WP_query.

The query we us looks like this:

$recent = new WP_query('p='.$post->ID); 

Once we have the query we put it into the standard wordpress loop:

while($recent->have_posts()) : $recent->the_post();

Then we use the standard WordPress tag <?php the_title(); ?> to pull in the META TITLE and <? the_content_rss() ?> to get the description. Notice, we are not using <?php the_excerpt(); ?>. This is because this WordPress tag prints the excerpt with a “read more” link in it. This will seriously screw up your theme. Using <?php the_content_rss(); ?> allows us to specify how many words of the content to pull in. But the second parameter has to be set to TRUE to avoid pulling in a “read more” link.

SEO Link Building: Quality First

There’s always been a bit of tension between quantity and quality in link building campaigns. Do I build a widget that can get me thousand of links with minimal effort? Or do I focus on getting big links for a few dozen sources? Here’s a video from SEOMOZ.org on the growing importance of link quality.

SEOmoz Whiteboard Friday – Link Quality vs. Quantity from Scott Willoughby on Vimeo.