-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathProjectController.php
More file actions
76 lines (57 loc) · 2.28 KB
/
ProjectController.php
File metadata and controls
76 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace App\Http\Controllers\Api\V1;
use App\Models\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProjectController extends Controller
{
/* function to fetch all Projects */
public function index(Request $request)
{
//echo "<pre>";print_r($request->sortDirection);die;
$q = $request->q; // search keyword, will search for name
$pageIndex = $request->pageIndex ? $request->pageIndex : 0; // the index of the page to shown, default 0
$pageSize = $request->pageSize ? $request->pageSize : 3; // how many items to return, default 3
$sortBy = $request->sortBy ? $request->sortBy : 'name' ; // attribute to sort, default name
$sortDirection = $request->sortDirection ? $request->sortDirection : 'ASC'; // direction of the sort, default ASC
$query =Project::query();
// Search by name
if (isset($q)) {
$query->where('name', 'LIKE', '%'.$q.'%');
}
// search by sort by and direction
$query->orderBy($sortBy, $sortDirection);
return $query->paginate($pageSize);
}
/* function to fetch Project for which id given in route */
public function showProject($id)
{
return Project::find($id);
}
/* function to create new Project */
public function createProject()
{
$data = [
['name'=>'Project 4']
];
Project::insert($data);
echo "record inserted";
}
/* function to update Project with specific Id */
public function updateProject(Request $request)
{
$id = $request->id;
$data = ['name'=>'Project 5'];
Project::where(['id'=>$id])
->update($data);
return Project::find($id);
}
/* function to delete Project with given Id */
public function deleteProject(Request $request)
{
$id = $request->id;
Project::where(['id'=>$id])
->delete();
echo "Project deleted successfully";
}
}