Day 40: Project Day

Apply the skills learned in Week 39 to build a comprehensive web-based application

Collaborate with peers to build a functional and useful application

Present and showcase the project at the end of the day

Tutorial:

Building a Web-Based Social Networking Platform Using PHP, MySQL, and Laravel

Are you interested in building your own social networking platform? In this tutorial, we’ll walk through how to build a web-based social networking platform using PHP, MySQL, and the Laravel framework.

Prerequisites

Before we get started, make sure you have the following installed:

Step 1: Create a new Laravel project

First, let’s create a new Laravel project by running the following command in your terminal:

composer create-project --prefer-dist laravel/laravel social-network

This will create a new Laravel project named “social-network” in your current directory.

Step 2: Set up database and migrations

Next, we’ll set up our database and migrations. Open up the config/database.php file and update the MySQL credentials to match your local environment.

Then, run the following command in your terminal to create a new migration for our users table:

php artisan make:migration create_users_table

Open up the newly created migration file located at database/migrations/create_users_table.php and add the following code to define our users table schema:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}

Now, run the following command to execute our migration and create our users table:

php artisan migrate

Step 3: Create user model and authentication

With our database set up, let’s create our user model and authentication. Run the following command in your terminal to generate both files:

php artisan make:model User -a

This will generate several files including app/User.php for our user model and app/Http/Controllers/Auth/RegisterController.php for registering new users.

In User.php, add $fillable property with an array of fields that can be mass-assigned when creating or updating a user:

protected $fillable = ['name', 'email', 'password'];

In RegisterController.php, update the $redirectTo property to redirect users after they register:

protected $redirectTo = '/home';

Then, update the validation rules in validator() method to require a unique email address:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

Finally, update the registration form view located at resources/views/auth/register.blade.php. You can customize this view as needed but it should include fields for name, email address, password and password confirmation.

Step 4: Create home page view

Let’s now create a home page view that displays all registered users. In your terminal, run the following command to generate a view file for displaying all users (resources/views/home.blade.php):

touch resources/views/home.blade.php

In home.blade.php, add the following code to display all registered users:

@foreach ($users as $user)
    <h2>{{ $user->name }}</h2>
    <p>{{ $user->email }}</p>
@endforeach

Step 5: Add routes

Finally, let’s add some routes so we can navigate between pages. In routes/web.php, add the following code:

Route::get('/', function () {
    return redirect()->route('login');
});

Auth::routes();

Route::get('/home', '[UserController]index')->name('home');

The first route simply redirects us to /login. The second route sets up RESTful routes for handling authentication related requests such as logging in or out. The third route handles displaying all registered users on homepage.

And that’s it! You now have a simple web-based social networking platform built using Laravel. Of course there are many more features you could add such as profile customization or messaging but this should serve as a good starting point for building out your own custom social networking platform.