Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to Create an API using Laravel?

 


Controllers.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\BusinessCategory\CreateCategoryRequest;
use App\Http\Requests\BusinessCategory\UpdateCategoryRequest;
use App\Http\Resources\BusinessCategory\BusinessCategoryResource;
use App\Model\BusinessCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BusinessCategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$status=request()->query('status');
$businesses=BusinessCategory::when($status
         !== null,function($query) use($status){
$query->where('status',$status);
})->orderBy('id','desc')->get();

return responseSuccess(BusinessCategoryResource::
            collection($businesses));

}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(CreateCategoryRequest $request)
{
$data=[
'title'=>$request['title'],
];
$business=DB::transaction(function() use($data,$request){
$business=BusinessCategory::create($data);
if($request->image){
$business->addMedia($request['image'])
            ->toMediaCollection('categoryimage');
}
return $business;
});
if($business!=null){
return responseSuccess($business,201,'
            Business category is created successfully!');
}
else {
return responseError('Internal Server Error',500);
}


}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$business=BusinessCategory::find($id);
return responseSuccess(new BusinessCategoryResource($business));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateCategoryRequest $request, $id)
{
$business=BusinessCategory::find($id);
if(is_null($business)){
return responseError('Internal Server Error',500);
}
else {
$business=DB::transaction(function() use($business,$request){
$business->title=$request['title'];
$business->save();
return $business;
});

}
if(is_null($business)){
return responseError('Internal Server Error',500);
}
else {
return responseSuccess($business,200,
            'You have updated the data successfully!');
}
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$data=BusinessCategory::find($id);
if(is_null($data)){
return responseError('The category is not found',500);

}
else {
DB::transaction(function() use($data){
$data->delete();
return $data;
});
if(!is_null($data)){
return responseSuccess($data,201,'You have deleted the data successfully');
}
else {
return responseError('Error Occurred!');
}

}
}
}

Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.