Laravel share package

When building websites you most likely have social share buttons on your pages, adding them can be a repetitive task. This Laravel share package solves this issue.

After following the installation instructions you can add social share buttons to your page like this.

Share::page('http://blog.jorenvanhocht.be')->facebook();

You can generate one share link, but you can also generate all available services at once, just by chaining the service names.

Share::page('http://jorenvanhocht.be', 'Share title')
    ->facebook()
    ->twitter()
    ->googlePlus()
    ->linkedin('Extra linkedin summary can be passed here');

At the time of this writing the following services are available:

  • Facebook
  • Twitter
  • Google Plus
  • Linkedin

The source code can be found on Github and the package is installable through composer.

Reseted password in Laravel not working

I had a pretty tough bug  to debug today, my reseted password using the Laravel resets password trait was not working. After some research I was able to define what was causing this bug and how I could resolve it.

If you are like me you would probably have set a mutator on your user model that Hashes the password before saving it in the database.

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = Hash::make($password);
}

Now when you look in the ResetsPassword trait you will see that Laravel also hashes the new password. So the password was hashed twice, that’s why the new password was not working.

protected function resetPassword($user, $password)
{
    $user->forceFill([
        'password' => bcrypt($password),
        'remember_token' => Str::random(60),
    ])->save();

    Auth::guard($this->getGuard())->login($user);
}

Since I was not using the dafault Laravel Trait anyway (due to some custom stuff) I could just remove the bcrypt function in my controller. Now I was able to login again. If you are using the default Trait you will have to find another work around or remove your mutator.

If you are using the default Trait you will have to find another work around or remove your mutator.

Adding Mandrill tags to your Laravel email

The problem

When using Laravel mail with Mandrill as the mail driver you can send mails the Laravel way, just by adding the API key to your env file.

It works great but what if you use one Mandrill account for all your clients and you have to keep track of the number of emails send for each client?

API keys

The first thing coming to your mind will probably be to create an API key for each client. Which is great to see the difference in your outbound dashboard in Mandrill.

But using these api keys does not allow you to see how many emails you have sent with it, therefore we will need tags.

Mandrill tags

Using tags within Mandrill allows you to see how many emails you have sent with that tag. Besides creating an api key for each client, you also create a tag for them.

Attaching Mandrill tags using Mail::send(…);

Mandrill has a powerful api to send mail templates where you can also specify those tags. But we don’t want to use that api, we want to use the build in Laravel mail functionality.

According to the following Mandrill docs  you can also send tags using http headers. That’s great since Laravel allows you to add additional headers to emails.

Eventually, you will end up with a mail function similar to the following one:

Mail::send('your-mail-viewl', [], function($message) {
    $message->to('info@example.com');
    $message->subject('Subjectline');
    $message->getSwiftMessage()
        ->getHeaders()
        ->addTextHeader('X-MC-Tags', 'my-laravel-tag', 'my-second-laravel-tag');
});

You can even add multiple tags just by separating them with a comma.

Return data from laravel events to controllers

Laravel events are lovely to work with, it makes your controllers much cleaner and it groups multiple tasks. For example, when creating a new user. I used it a lot but I never had to return data back to my controller.

At first, I was a bit worried, I had never seen an example where there was actually data returned to the controller.

But Laravel shouldn’t be Laravel if it was not possible, after a bit of research it seemed that you could just return whatever you want in every listener (in the handle method) of your event. In your controller, you would get an array with a key for each listener you have for the fired event.

# response of the fired event. This event had one listner 

0 => "{"slug":"my-slug","id":1634,"name":"my name","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"XXX","id":123,"name":"Jorenvh","description":"Joren Van Hocht","public":false,"type":"NORMAL","links":{"self":[{"href":"http://github.com/jorenvh"}]}},"public":false,"links":{"clone":[{"href":"http://github.com/jorenvh/blogify.git","name":"http"},{"href":"ssh://git@github.com/jorenvh/blogify.git","name":"ssh"}],"self":[{"href":"http://github.com/jorenvh/blogify"}]}}"]

Each of these keys contain the JSON encoded data that you returned in the handle method of your listener.

Happy birthday Laravel

Five years ago today Taylor announced the first version of Laravel, Happy birthday Laravel.

I’m using Laravel for almost 2 years now and started my journey at version 4.2. Two versions later today the framework has changed a lot and I became a big enthusiast of it.

Many thanks to Taylor Otwell for making my life as a developer more enjoyable.

Export php variables to javascript

Spatie, known for their Laravel packages, released a brand new package. Laravel blade javascript is a Blade directive to export PHP variables to Javascript.

But instead of exporting variables in the controller our package does it in the view.

Read the full article on Freek Van der Herten his blog. You can also find all the other Spatie packages on their website.

New migration and blade features coming to Laravel 5.3

Taylor Otwell announced two new features coming to Laravel 5.3 on twitter today.  You will be able to roll back one migration at a time and you will have access to the $loop variable in blade foreach loops.

Rolling back one migration at a time

This new feature will come in handy when one of your migrations fail for some reason. You will be able to use it this way:

php artisan migrate:rollback --step=1


Blade $loop variable

In blade foreach loops you will have access to a $loop variable, it will allow you to do things like this, but knowing Taylor there will be a lot more available.

@foreach($users as $user)
    @if($loop->first)
        <!-- First iterartion -->
    @endif
    
    @if($loop->last)
        <!-- Last iteration -->
    @endif
@endforeach


Code credits go to Taylor Otwell & Laravel php

Why you shouldn’t set global variables in Base Controller

Laravel Daily posted a new tip today, they explain why you shouldn’t set global variables in Base Controller.

Setting global variables in the base controller works, until you need to add a error page or 404 page. When rendering those pages no controller will ever be hit. So your global variables are not available at that moment.

For the workaround I suggest you take a look at the original article on the Laravel Daily site.

Laravel 5.3 – Multiple Migration Paths

A awesome new feature coming to Laravel 5.3 is the ability to have multiple migration paths.

This feature comes in handy for all package developers and of course for all package users. Currently when a package wants to use migrations you have to use php artisan migrate to run your own migrations and php artisan migrate –provider”Package\ServiceProvider” to migrate package migration files.

Once Laravel 5.3 has been released you will be able to register multiple migrations paths like this:

$this->loadMigrationsFrom('path/to/migrations/folder');

from in a service provider for example. The end user can now migrate all the migrations while only using php artisan migrate. There is no need to look all the providers up to migrate package migrations.

Blogify the Laravel blog package

Blogify is a blog package made for Laravel,

I created Blogify last year as my final project for school. The reason why I choose to create a package, is that the Laravel community had given so much resources to me and I wanted to give something back.

But why a blog package and not something else?

During my internship I saw that there where a lot of projects that where build a long time ago and suddenly needed a blog and because one of those projects was custom and was based on Laravel we had to write a whole new feature, no big deal but when i asked around it seemed like this happend more often then I expected.

I inspired my whole backend styling on WordPress, it’s the most used blog system in the world so there should be a good reason to do it on a different way.

The WordPress way works and most bloggers know where to search for general things.

But why should u choose Blogify over WordPress? Blogify is designed for developers who love custom build tools and want to integrate a simple blog without using WordPress. And it’s still as easy as managing a WordPress blog.

Eventually I graduated and got nice grades (15/20) for the Blogify package, a very excited Jury and a mention in the Laravel-news newsletter. But then I started working and Blogify did not get the love it needed, unsolved issues, the installation process did not work anymore and 2 new versions of Laravel where released but Blogify was still where it was a year ago.

Last week I started thinking …. the project where I put so much time in was just sitting there somewhere on the web, such a sad story.

So I decided to give Blogify the love it deserved and I migrated the code base over to Laravel 5.2 and solved the issues so users could install it smoothly again.

Make sure to check out this awesome package and spread the word so that I can actually give something back to the community.