Skip to content

Commit d2b3f86

Browse files
committed
add article table and db model
1 parent 7beb91c commit d2b3f86

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ APP_URL=http://localhost
88
DB_CONNECTION=mysql
99
DB_HOST=127.0.0.1
1010
DB_PORT=3306
11-
DB_DATABASE=homestead
12-
DB_USERNAME=homestead
13-
DB_PASSWORD=secret
11+
DB_DATABASE=laravel5restapi
12+
DB_USERNAME=YOUR_MYSQL_USERNAME
13+
DB_PASSWORD=YOUR_MYSQL_PASSWORD
1414

1515
BROADCAST_DRIVER=log
1616
CACHE_DRIVER=file

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Laravel 5 REST API
2+
---
3+
4+
I'll be working alongside [this tutorial](https://www.toptal.com/laravel/restful-laravel-api-tutorial) for building the API
5+
6+
**Step 1:** Create project with [this handy script](https://gist.github.com/connor11528/fcfbdb63bc9633a54f40f0a66e3d3f2e)
7+
8+
I also wrote [this Medium article](https://medium.com/@connorleech/build-an-online-forum-with-laravel-initial-setup-and-seeding-part-1-a53138d1fffc) that goes through installing and configuring a MySQL database for a Laravel 5 application. I find myself refering back to it regularly.
9+
10+
**Step 2:** Configure database and make models
11+
12+
If you don't have MySQL installed on Mac may the force be with you. It comes preinstalled and there's MAMP but getting your machine set up to run SQL to MySQL from the terminal can be tricky.
13+
14+
My setup is to run:
15+
16+
```
17+
$ mysql -uroot -p
18+
> create database MY_APP_NAME;
19+
$ php artisan make:model Article -m
20+
```
21+
22+
The `;` is required in order to end all SQL statements.
23+
24+
Add a **title** and **body** string columns to the articles database and run the migrations:
25+
26+
```
27+
$ php artisan migrate
28+
```
29+
30+
If you get an error saying cannot connect to homestead, even after you updated your .env to not point to homestead anymore clear the config and try again.
31+
32+
To clear config in Laravel:
33+
34+
```
35+
$ php artisan config:clear
36+
```
37+
38+
Make the models fillable (Laravel will protect them by default). Add this line to **app/Article.php**:
39+
40+
```
41+
protected $fillable = ['title', 'body'];
42+
```
43+
44+
Fillable will allow reads and writes to those database columns. You could also set a guarded property to an empty array:
45+
46+
```
47+
$guarded = []
48+
```
49+
50+
Which for our purposes will do the same thing. To learn more about this you can check the Eloquent docs on [Mass Assignment](https://laravel.com/docs/5.5/eloquent#mass-assignment).
51+
52+
53+
54+

app/Article.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Article extends Model
8+
{
9+
protected $fillable = ['title', 'body'];
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateArticlesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('articles', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('title');
19+
$table->text('body');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('articles');
32+
}
33+
}

0 commit comments

Comments
 (0)