- This application serves to demonstrate the basics of Laravel Restful Api in 10 minutes by creating a todo app
- The artisan command below creates a model of the name Todo alongside a migration for the database schema
php artisan make:model Todo -m
- To solve the Mass Assignment Error
- Add the following field at the top of your Todo model
protected $guarded = [];
- The artisan command below crerats a controller of the name TodosController and binds it to the Todo model
- The --api parameter adds the methods that a generally used in an api to the created controller
php artisan make:controller TodosController --model=Todo --api
- Creates the User Controller
php artisan make:controller UsersController --model=User --api
- Navigate to the created migration and add the following code to the up() method
$table->increments('id');
$table->string('task');
$table->boolean('completed')->default('0');
$table->integer('user_id')->nullable();
$table->timestamps();
- Take note that the users migration has been created for your already
- To create your tables, execute the following artisan command to run the migrations
php artisan migrate
- go to the Todo model and define the relationship that deals with the owner of the task
- we have a one to many relation: one Todo belongs to one User; One user has many Todos
- The code below creates an owner relation in the Todo class
public function owner()
{
return $this->belongsTo(User::class, 'user_id');
}
- The code below links a user to his Todos
public function todos()
{
return $this->hasMany(Todo::class);
}
- paste the following code into the TodoFactory created earlier
$users = \App\User::all()->random(1)->pluck('id')->values();
return [
'task' => $faker->sentence,
'completed' => $faker->boolean(),
'user_id' => $users[0]
];
- Take note the UserFactory already comes predefined for you and ready to use, so no need to create it
- It can be seen under the database/factories directory
- Laravel comes with many different approaches to seed your database,
- ranging from laravel tinker,
- or using the artisan command below
php artisan db:seed
- We will use tinker - is a command line interface tool for manipulating the database
- The below artisan command opens the tinker CLI
php artisan tinker
- Within the tinker environment
- Seed the user table with as many rows of data as you want in this case 20 users
factory(App\User::class, 20)->create()
- Seed the todo table with 40 rows of data
factory(App\Todo::class, 40)->create()
- Add the following code to your API routes
Route::resource('todos', 'TodosController');
Route::resource('users', 'UsersController');
- Add the following code to your index method in the TodosController
return response()->json(Todo::all());
- Add the following code to your show method in the TodosController
return response()->json($todo->with('owner')->get()->where('id', '=', $todo->id));
- Add the following code into your store method
$todo = Todo::create([
'task' => $request->task,
'completed' => $request->completed ? $request->completed : 0,
'user_id' => User::all()->random(1)->pluck('id')[0]
]);
return response()->json(['message' => 'Todo successfully created']);
- Add the following code into your destroy method
$todo->delete();
return response()->json(['message' => "Todo with id {$todo->id} has been successfully deleted"]);
- Add the following code to the update method
if ($request->task)
$todo->task = $request->task;
if ($request->completed)
$todo->completed = $request->completed;
$todo->save();
return response()->json($todo);
- Add the following code to your index method in the UsersController
return response()->json(User::all()->first()->with('todos')->get());
- Add the following code to your show method in the UsersController
return response()->json($user->with('todos')->get()->where('id', '=', $user->id));
- Add the following code into your store method
$todo = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt('secret')
]);
return response()->json(['message' => 'User successfully created']);
- Add the following code to your destroy method
$user->delete();
return response()->json(['message' => "User with id {$user->id} has been successfully deleted"]);
- Add the following code to the update method
if ($request->name)
$user->name = $request->name;
if ($request->email)
$user->email = $request->email;
$user->save();
return response()->json($user);
-
HTTP Methods that correspond to methods in your controllers delete, put, post and get
-
get - http://{-your domain-}/api/todos
-
get - http://{-your domain-}/api/todos/1
-
post - http://{-your domain-}/api/todos - pass it some body data -> json format
task: Upload app to github
- delete - http://{-your domain-}/api/todos/1
- put - http://{-your domain-}/api/todos/1 - pass it some body data -> ensure that you use the x-www-form-urlencoded data
task: Test this api
completed: 1
- get - http://{-your domain-}/api/users
- get - http://{-your domain-}/api/users/1
- post - http://{-your domain-}/api/todos - pass it some body data -> json format
name: Laravel Namibia
email: laranamibia@gmail.com
- delete - http://{-your domain-}/api/users/1
- put - http://{-your domain-}/api/todos/1 - pass it some body data -> ensure that you use the x-www-form-urlencoded data
name: Laravel SA
email: larasa@gmail.com