-
Notifications
You must be signed in to change notification settings - Fork 49
Association
JP Barbosa edited this page Jul 23, 2015
·
2 revisions
php artisan make:model Author --migration
nano database/migrations/*create_authors_table.php
...
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
Schema::table('articles', function (Blueprint $table) {
$table->integer('author_id')->unsigned()->index()->default(1);
$table->foreign('author_id')
->references('id')
->on('authors');
});
}
...
php artisan migrate
nano app/Author.php
...
class Author extends Model
{
protected $fillable = ['name', 'email'];
public function articles()
{
return $this->hasMany('App\Article');
}
}
nano app/Article.php
class Article extends Model
{
protected $fillable = ['title', 'content', 'author_id'];
public function author()
{
return $this->belongsTo('App\Author');
}
}
php artisan tinker
$author = App\Author::create(['name' => 'Author Name', 'email' => 'email@domain.local']);
$article = App\Article::first();
$article->update(['author_id' => $author->id]);
App\Article::query()->update(['author_id' => App\Author::first()->id]);
git add .
git commit -m "Add authors associations"
Next step: Association Controller
- Setup
- Basic CRUD
- Validation
- Views
- Association
- Association Controller
- Association Views
- Basic Template
- Bootstrap
- Bootstrap CRUD
- Alerts
- Welcome Page
- Ajax CRUD
- Send Email
- Send Email Views
- Jobs Queue
- Captcha
- Async External Content
- Cached External Content
- Tests Setup
- Functional Tests
- Acceptance Tests
- Continuous Integration
- Deploy with Heroku
- Deploy with Forge
- Update README