Created
January 20, 2020 22:35
-
-
Save Sean-Spallen/4ad4b63a20b03e99c297d9c2ca9cdb0e to your computer and use it in GitHub Desktop.
Laravel Controller Template
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
use App\Item; | |
class ItemController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() | |
{ | |
$items = Item::all(); | |
return view('items.index', compact('items')); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() | |
{ | |
return view('items.create'); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$request->validate([ | |
// REQUIRED FIELDS | |
'name'=>'required', | |
]); | |
// PULLS VALUES FROM FIELDS | |
$item = new Item([ | |
'name' => $request->get('name'), | |
]); | |
$item->save(); | |
return redirect('/items')->with('success', 'Item saved!'); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) | |
{ | |
$item = Item::find($id); | |
return view('items.show', compact('item')); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) | |
{ | |
$item = Item::find($id); | |
return view('items.edit', compact('item')); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) | |
{ | |
// REQUIRED FIELDS | |
$request->validate([ | |
'name'=>'required', | |
]); | |
$item = Item::find($id); | |
// PULLS VALUES FROM FIELDS | |
$item->name = $request->get('name'); | |
$item->save(); | |
return redirect('/item')->with('success', 'Item updated!'); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) | |
{ | |
$item = Item::find($id); | |
$item->delete(); | |
return redirect('/items')->with('success', 'Item deleted!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment