Archives

Laravel – Model and Controller

C:\Users\EP\book>php artisan make:controller UserAccountController –resource –model=UserAccount

A App\UserAccount model does not exist. Do you want to generate it? (yes/no) [yes]:
> yes

Model created successfully.
Controller created successfully.

Laravel – Route

C:\Users\EP\blog>php artisan route:list
+——–+———–+———————–+—————+—————————————————-+————–+
| Domain | Method | URI | Name | Action | Middleware |
+——–+———–+———————–+—————+—————————————————-+————–+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | api/users | users.index | App\Http\Controllers\UserAccountController@index | api |
| | POST | api/users | users.store | App\Http\Controllers\UserAccountController@store | api |
| | GET|HEAD | api/users/create | users.create | App\Http\Controllers\UserAccountController@create | api |
| | GET|HEAD | api/users/{user} | users.show | App\Http\Controllers\UserAccountController@show | api |
| | PUT|PATCH | api/users/{user} | users.update | App\Http\Controllers\UserAccountController@update | api |
| | DELETE | api/users/{user} | users.destroy | App\Http\Controllers\UserAccountController@destroy | api |
| | GET|HEAD | api/users/{user}/edit | users.edit | App\Http\Controllers\UserAccountController@edit | api |
+——–+———–+———————–+—————+—————————————————-+————–+

Laravel Project (MVC) – Beginning

create database lms_laravel_db;

CREATE TABLE user_account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(25) NOT NULL,
email_id VARCHAR(200) NOT NULL,
PASSWORD VARCHAR(50) NOT NULL
);

download composer and install
https://getcomposer.org/download/

download laravel
C:\Users\EP>composer global require “laravel/installer”

create project
C:\Users\EP>composer create-project –prefer-dist laravel/laravel blog
Note:–(double hyphen) before prefer
C:\Users\EP\blog\.env, edit the following
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=lms_laravel_db
DB_USERNAME=root
DB_PASSWORD=password

C:\Users\EP\blog\config\database.php, edit mysql
Note:/*comment all other database */

C:\Users\EP>cd blog

C:\Users\EP\blog>php artisan serve
Laravel development server started:

In chrome,
http://127.0.0.1:8000/

C:\Users\EP\blog>php artisan make:model UserAccount
Model created successfully.
In C:\Users\EP\blog\app\UserAccount, edit the model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserAccount extends Model
{
    protected $table = 'user_account';
    protected $primaryKey = 'id';
}

C:\Users\EP\blog>php artisan make:controller UserAccountController –resource
Controller created successfully.
Note:–(double hyphen) before resource

C:\Users\EP\blog\app\Http\Controllers\UserAccountController, edit the controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class UserAccountController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = DB::table('user_account')
		->select ('id', 'name', 'email_id', 'password')
		->get();
    	return response()->json($list);
    }
}

C:\Users\EP\blog\routes\api.php, edit the api.php

Route::resource(‘users’, ‘UserAccountController’);

http://localhost:8000/api/users