29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Laravel Cashier 175<br />

Migration<br />

Before using Cashier, we’ll need to add several columns to your database. Don’t worry, you can<br />

use the cashier:table Artisan command to create a migration to add the necessary column. For<br />

example, to add the column to the users table run the command: php artisan cashier:table users.<br />

Once the migration has been created, simply run the migrate command.<br />

Model Setup<br />

Next, add the Billable trait and appropriate date mutators to your model definition:<br />

.<br />

1 use Laravel\Cashier\Billable;<br />

2 use Laravel\Cashier\Contracts\Billable as BillableContract;<br />

3<br />

4 class User extends Model implements BillableContract<br />

5 {<br />

6 use Billable;<br />

7<br />

8 protected $dates = ['trial_ends_at', 'subscription_ends_at'];<br />

9 }<br />

Adding the columns to your model’s $dates property will instruct Eloquent to return the columns<br />

as Carbon / DateTime instances instead of raw strings.<br />

Stripe Key<br />

Finally, set your Stripe key in your services.php configuration file:<br />

.<br />

1 'stripe' => [<br />

2 'model' => 'User',<br />

3 'secret' => env('STRIPE_API_SECRET'),<br />

4 ],<br />

Subscriptions<br />

Creating Subscriptions<br />

To create a subscription, first retrieve an instance of your billable model, which typically will be an<br />

instance of App\User. Once you have retrieved the model instance, you may use the subscription

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!