Last active
March 1, 2019 18:53
-
-
Save bunlongheng/ec7d7ed8c53472cf6cfe2a99386d06fc to your computer and use it in GitHub Desktop.
Sample Laravel + Angular CRUD 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\Skill; | |
use Input, Validator, Auth, Redirect, Request, Session, Mail, View, Image; | |
class SkillController extends Controller { | |
//------------------------------------------------------------------------------------------------- [ all ] | |
public function all(){ | |
return Skill::orderBy('created_at', 'desc')->get(); | |
} | |
//------------------------------------------------------------------------------------------------- [ get ] | |
public function get($id){ | |
$skill = Skill::findOrFail($id); | |
return $skill; | |
} | |
public function types(){ | |
return Skill::all()->groupBy('type'); | |
} | |
//------------------------------------------------------------------------------------------------- [ index ] | |
public function index(){ | |
$skills = Skill::orderBy('created_at', 'desc')->get(); | |
return View::make('layouts.be.skills.index', get_defined_vars()); | |
} | |
//------------------------------------------------------------------------------------------------- [ Store ] | |
public function store(){ | |
$inputs = Input::all(); | |
$name = Input::get('name'); | |
$type = Input::get('type'); | |
$value = Input::get('value'); | |
$skill = new Skill; | |
$skill->name = $name; | |
$skill->type = $type ; | |
$skill->value = $value ; | |
$skill->save(); | |
} | |
//------------------------------------------------------------------------------------------------- [ Update ] | |
public function update($id){ | |
$inputs = Input::all(); | |
$name = Input::get('name'); | |
$type = Input::get('type'); | |
$value = Input::get('value'); | |
$skill = Skill::find($id); | |
$skill->name = $name; | |
$skill->type = $type ; | |
$skill->value = $value ; | |
$skill->save(); | |
} | |
public function updateType($index){ | |
$inputs = Input::all(); | |
$type = Input::get('type'); | |
$new_type = Input::get('new_type'); | |
$skills = Skill::where('type','=',$type)->get(); | |
foreach ($skills as $skill) { | |
$skill = Skill::find($skill->id); | |
$skill->type = $new_type ; | |
$skill->save(); | |
} | |
} | |
//------------------------------------------------------------------------------------------------- [ Destroy ] | |
public function destroy($id){ | |
$skill = Skill::find($id); | |
$skill->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment