Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
What is REST API in Laravel?
Client–server architecture: Your REST API interface should follow a client-server architecture,
Stateless: Your REST API interface should be stateless,
Cacheable: Your REST API interface should be cacheable,
Uniform interface: Your REST API should have a uniform interface,Layered system ,Code on demand.
File name : index.php
REST API Method
A REST API is an interaface that allows you to interface your web application with other systems like mobile devices and web browsers via a set of methods that correspond to CRUD (create, read, update, delete) operations. In REST rules, you need to map a specific HTTP method to a specific CRUD operation.
File name : index.php
GET: This method can be mapped to an action to get/retrieve the resource data,
POST: This method can be mapped to an action to create a new resources,
PUT: This method can be mapped to an action to update existing resources,
DELETE: This method can be mapped to an action delete the resource,
PATCH: This method can be mapped to an action to make partial update on a resource.
Rest API Controller
File name :
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Restapi;
class RestApiController extends Controller
{
public function index()
{
$data = Restapi::all();
//return $data;
return response()->json($data);
}
public function show(Request $request,$id)
{
$data = Restapi::find($id);
return response()->json($data);
}
public function store(Request $request)
{
$data = new Restapi;
//$data->name = Input::get('name');
$data->name = $request->Input('name');
$data->address = $request->Input('address');
$data->mobile = $request->Input('mobile');
$data->save();
//return $data;
return response()->json($data);
}
public function update(Request $request,$id)
{
$data = Restapi::find($id);
$data->name = $request->input('name');
$data->address = $request->input('address');
$data->mobile = $request->input('mobile');
$data->save();
//return $data;
return response()->json($data);
}
public function deleteinfo(Request $request,$id)
{
$data = Restapi::find($id);
$data->delete();
return response()->json(null, 204);
}
}
Route
File name : index.php
Route::get('restapi', 'admin\RestApiController@index');
Route::get('restapi-show/{id}', 'admin\RestApiController@show');
Route::post('restapi-save', 'admin\RestApiController@store');
Route::put('restapi-update/{id}', 'admin\RestApiController@update');
Route::delete('restapi-delete/{id}', 'admin\RestApiController@deleteinfo');
Model
File name : Restapi.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Restapi extends Model
{
public $timestamp = true;
protected $fillable = ['name','address','mobile'];
}
Example
File name : ApiController.php
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Student;
class ApiController extends Controller
{
/* public function index()
{
return view('admin.student-api');
}
*/
public function index()
{
return csrf_token();
}
public function getAllStudents() {
$students = Student::get()->toJson(JSON_PRETTY_PRINT);
return response($students, 200);
//$stud = Student::get();
//return response()->json($stud,200);
//$stud = Student::find('2');
//return response()->json($stud,200);
}
public function createStudent(Request $request) {
$student = new Student;
$student->name = $request->name;
$student->course = $request->course;
$student->save();
return response()->json([
"message" => "student record created"
], 201);
}
public function getStudent($id) {
if (Student::where('id', $id)->exists()) {
$student = Student::where('id', $id)->get()->toJson(JSON_PRETTY_PRINT);
return response($student, 200);
} else {
return response()->json([
"message" => "Student not found"
], 404);
}
}
public function updateStudent(Request $request, $id) {
if (Student::where('id', $id)->exists()) {
$student = Student::find($id);
$student->name = is_null($request->name) ? $student->name : $request->name;
$student->course = is_null($request->course) ? $student->course : $request->course;
$student->save();
return response()->json([
"message" => "records updated successfully"
], 200);
} else {
return response()->json([
"message" => "Student not found"
], 404);
}
}
public function deleteStudent($id){
if(Student::where('id', $id)->exists())
{
$student = Student::find($id);
$student->delete();
return response()->json([
"message" => "records deleted"
], 202);
} else {
return response()->json([
"message" => "Student not found"
], 404);
}
}
}