Last active
June 22, 2022 07:02
-
-
Save abdulbasit-dev/6807f8dce0c8159a0d77195eab15a067 to your computer and use it in GitHub Desktop.
Laravel Api Model Controller Stub comman controller action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace {{ namespace }}; | |
use {{ namespacedModel }}; | |
use {{ rootNamespace }}Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Response; | |
use Illuminate\Support\Facades\Validator; | |
class {{ class }} extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
//check permission | |
//$this->authorize("_access"); | |
${{ modelVariable }}s = {{ model }}::all(); | |
return $this->josnResponse(true, "All {{ model }}s.", Response::HTTP_OK, ${{ modelVariable }}s); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
//check permission | |
//$this->authorize("_access"); | |
//validation | |
$validator = Validator::make($request->all(),[ | |
""=>['required'], | |
]); | |
if ($validator->fails()) { | |
return $this->josnResponse(false, "The given data was invalid.", Response::HTTP_UNPROCESSABLE_ENTITY, null, $validator->errors()->all()); | |
} | |
//{{ model }}::create([ | |
// ""=>$request-> | |
//]); | |
return $this->josnResponse(true, "{{ model }} cretaed successfully.", Response::HTTP_CREATED); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param \{{ namespacedModel }} ${{ modelVariable }} | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show({{ model }} ${{ modelVariable }}) | |
{ | |
//check permission | |
//$this->authorize("_access"); | |
return $this->josnResponse(true, "Show {{ model }} info.", Response::HTTP_OK, ${{ modelVariable }}); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \{{ namespacedModel }} ${{ modelVariable }} | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, {{ model }} ${{ modelVariable }}) | |
{ | |
//check permission | |
//$this->authorize("_access"); | |
//validation | |
$validator = Validator::make($request->all(),[ | |
""=>[] | |
]); | |
if ($validator->fails()) { | |
return $this->josnResponse(false, "The given data was invalid.", Response::HTTP_UNPROCESSABLE_ENTITY, null, $validator->errors()->all()); | |
} | |
//${{ modelVariable }}->update([ | |
// ""=>$request->'' | |
//]); | |
return $this->josnResponse(true, "{{ model }} updated successfully.", Response::HTTP_OK); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param \{{ namespacedModel }} ${{ modelVariable }} | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy({{ model }} ${{ modelVariable }}) | |
{ | |
//check permission | |
//$this->authorize("_access"); | |
${{ modelVariable }}->delete(); | |
return $this->josnResponse(true, "{{ model }} deleted successfully.", Response::HTTP_OK); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment