PHP code example of sarav / descriptive-api

1. Go to this page and download the library: Download sarav/descriptive-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sarav / descriptive-api example snippets


public function index()
{
    $articles = Article::paginate();
    return response()->ok($articles);
}

public function store(StoreArticleRequest $request)
{
    $article = Article::create($request->validated());
    return response()->created($article);
}

public function destroy(Article $article)
{
    $article->delete();
    return response()->noContent();
}

public function store(Request $request)
{
    if ($request->age < 0) {
      return response()->badRequest();
    }
}

public function store(Request $request)
{
    if (!auth()->check()) {
      return response()->unauthorized();
    }
}

public function store(Request $request)
{
    $user = $request->user();
    if (!$user->isAdmin()) {
        return response()->forbidden();
    }
}

public function update(Request $request, $id)
{
    $article = Article::find($id);
    if (!$article) {
        return response()->notFound();
    }
}

public function update(Request $request, $id)
{
    $validator = Validator::make([
      'comment' => 'ure('Validation failure', $validator->errors());
    }
}

public function update(Request $request, $id)
{
    try {
        // Your Business Logic
    } catch (Exception $e) {
        return response()->internalServerError();
    }
}

public function store(Request $request)
{
    // Overriding badRequest()
    if ($request->age < 0) {
      return response()->badRequest('Invalid age provided');
    }
    
    // Overriding unauthorized()
    if (!auth()->check()) {
      return response()->unauthorized('User not authorized');
    }
  
    // Overriding forbidden()
    $user = $request->user();
    if (!$user->isAdmin()) {
        return response()->forbidden('Access denied. Please contact administrator');
    }
    
    // Overriding notFound()
    $article = Article::find($id);
    if (!$article) {
        return response()->notFound('Article not found');
    }
    
    // Overriding validationFailure()
    $validator = Validator::make([
      'comment' => '