DEV Community

Sharad Raj (He/Him)
Sharad Raj (He/Him)

Posted on

How to start with Laravel & REST APIs in it, I'm new to it?

I've experience with MERN stack but I'm completely new to Laravel, although I've got the basics of php but no oops concepts .... How should I start ? Any resources?

Top comments (8)

Collapse
 
kwnaidoo profile image
Kevin Naidoo • Edited

In Laravel, you should learn these important concepts. It's a huge framework with lots of options so please don't try to learn everything at once:

  1. Routes: routes/web.php : laravel.com/docs/10.x/routing
  2. app/Http/Controllers : laravel.com/docs/10.x/controllers
  3. app/Models : laravel.com/docs/10.x/eloquent
  4. resources/views/ : laravel.com/docs/10.x/views
  5. Blade Templates : laravel.com/docs/10.x/blade
  6. database/migrations/ : laravel.com/docs/10.x/migrations
  7. php artisan : laravel.com/docs/10.x/artisan

MVC essentially.

In Laravel, when a request comes in - the routes file is checked, if a pattern is matched. E.g.:

Route::get("/welcome", 'WelcomeController@index');
Enter fullscreen mode Exit fullscreen mode

You visit: "/welcome", it now matches the above "GET" route (Laravel respects REST verbs GET, POST, etc...). The second argument to this function is which class must handle this request i.e. "WelcomeController" and then which method in this class must handle this request i.e. "index".

In your terminal, if you run this artisan command, it will print out all the routes you have configured:

php artisan route:list

# You can also run this to get all the available artisan commands:
php artisan
Enter fullscreen mode Exit fullscreen mode

The index method then needs to return a view. It can do any processing before returning the view like getting data from the database.

Here is a basic controller:

namespace App\Http\Controllers;
class WelcomeController extends Controller
{
      public function index()
      {
            return response()->json(["message" => "Hello World"]);
      }

      public function exampleWithAView()
      {
            // This will look for a file: resources/views/welcome.php 
            // - or resources/views/welcome.blade.php
            return view("welcome", ["page_title" => "Welcome"]);
      }
}
Enter fullscreen mode Exit fullscreen mode

A model in Laravel looks like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

}
Enter fullscreen mode Exit fullscreen mode

Laravel will automatically look for a table named "categories" in your database.

You can then query for data:

// Get one category
Category::where("id", 1)->get();

// Create a category
$c = new Category();
$c->name = "Test category";
$c->save();
Enter fullscreen mode Exit fullscreen mode

To build an API, Laravel provides "routes/api.php" which is very similar to the "web.php" routes file. The only difference is that your controller method needs to return JSON. Learn more about how to return JSON and other response types here:

laravel.com/docs/10.x/responses

I also advise: laracasts.com/ and reading the Laravel documentation in more detail, at least the getting started part.

Hope that helps.

Collapse
 
servbay profile image
ServBay

First of all, you need a full-stack PHP environment. LOL
Don't you think so?

Collapse
 
sharadcodes profile image
Sharad Raj (He/Him)

Yeah I do ;)

Collapse
 
kwnaidoo profile image
Kevin Naidoo • Edited

If you on Mac: herd.laravel.com/

For Linux and Windows: apachefriends.org/

If you want to deploy to a server, I built an open source tool for that: plexscriptables.com/

On Linux you can also just install PHP locally:

sudo add-apt-repository -y ppa:ondrej/php

sudo apt-get update -y

sudo apt-get install -y php-cli php-mysql \
php-curl php-gd php-mbstring php-xml \
php-zip php-bcmath php-redis \
php-imagick php-intl php-ldap php-soap php-xmlrpc \
php-sqlite3


cd /tmp/ && wget https://getcomposer.org/download/latest-stable/composer.phar
sudo mv /tmp/composer.phar /usr/bin/composer
sudo chmod +x /usr/bin/composer
Enter fullscreen mode Exit fullscreen mode

Then in your Laravel project:
Edit your .env and set your DB as follows:

DB_CONNECTION=sqlite
DB_DATABASE=/path/to/database.sqlite
Enter fullscreen mode Exit fullscreen mode
composer install
php artisan migrate
npm install
npm run build
php artisan serve
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hooneun profile image
hooneun

The documents are the best teachers.

laravel.com/

Collapse
 
juststevemcd profile image
Steve McDougall

It depends what you want to achieve? If you are looking more for integrating with APIs or building APIs yourself.

Each one I can give different advice, so let me know!

Collapse
 
sharadcodes profile image
Sharad Raj (He/Him)

Building APIs

Collapse
 
juststevemcd profile image
Steve McDougall

I have loads of resources on this, but I can also give any advise you may be struggling with?