Created
July 9, 2021 11:24
-
-
Save AlphaRomeoMike/4af48c3a684089e5fe0e53b0b295b14e to your computer and use it in GitHub Desktop.
Sanctum product controller
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 App\Http\Controllers; | |
use App\Models\Product; | |
use Illuminate\Http\Request; | |
class ProductController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
return Product::all(); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$request->validate([ | |
'name' => 'required', | |
'slug' => 'required', | |
'price' => 'required' | |
]); | |
return Product::create($request->all()); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
return Product::find($id); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
$product = Product::find($id); | |
$product->update($request->all()); | |
return $product; | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
Product::destroy($id); | |
return true; | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param string $name | |
* @return \Illuminate\Http\Response | |
*/ | |
public function search($name) | |
{ | |
return Product::where('name', 'like', '%' . $name . '%')->get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment