Skip to content

Instantly share code, notes, and snippets.

@AlphaRomeoMike
Created July 9, 2021 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlphaRomeoMike/4af48c3a684089e5fe0e53b0b295b14e to your computer and use it in GitHub Desktop.
Save AlphaRomeoMike/4af48c3a684089e5fe0e53b0b295b14e to your computer and use it in GitHub Desktop.
Sanctum product controller
<?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