[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]
Laravel Models
In this segment we will focus on creating the model needed for the application. If you remember in the last post on migrations, we created one migration to represent the charities. Now we will utilize Laravel’s powerful Eloquent ORM, which will allow us to represent our database using PHP objects.
Creating Laravel models is actually very simple when done via Artisan. Head to the project root in the command line and enter the following command
php artisan make:model Charity
afterwards open the newly created file app/Charity.php
and add the following contents
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Charity extends Model
{
//
protected $table = 'charities';
protected $fillable = [
'user_id',
'name',
'description',
'monthly_amount'
];
}
The $table
variable let’s Eloquent know which table to associate the model with; It is usually not needed, however I prefer to put it in. Next is a $fillable
array that holds every attribute needed on a POST request to create a new record. If you look, you will see that there are 2 more Laravel models in the app/ directory. Check them out and read them over and get a good understanding of what’s going on.
app/User.php
<?php
namespace App;
use Laravel\Cashier\Billable;
use Laravel\Spark\Teams\CanJoinTeams;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as BaseUser;
use Laravel\Spark\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
use Laravel\Spark\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatableContract;
class User extends BaseUser implements TwoFactorAuthenticatableContract
{
use Billable, TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email',
'name',
'password',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'using_two_factor_auth'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'card_brand',
'card_last_four',
'extra_billing_info',
'password',
'remember_token',
'stripe_id',
'stripe_subscription',
'two_factor_options',
];
}
app/Team.php
<?php
namespace App;
use Laravel\Spark\Teams\Team as SparkTeam;
class Team extends SparkTeam
{
//
}