This repository has been archived by the owner on Aug 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAnswerController.php
78 lines (67 loc) · 2.05 KB
/
AnswerController.php
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
77
78
<?php
namespace LaravelAdminExt\Select2\Test\Controllers;
use App\Http\Controllers\Controller;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use LaravelAdminExt\Select2\Test\Models\Answer;
use LaravelAdminExt\Select2\Test\Models\Question;
use Illuminate\Support\Facades\DB;
/**
* A demo for select, see it in the `form()` method
*/
class AnswerController extends Controller
{
use HasResourceActions;
/**
* Edit interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public function edit($id, Content $content)
{
return $content
->header('Edit')
->description('description')
->body($this->form()->edit($id));
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new Answer);
$form->select('question_id', 'Question')->match(function ($keyword) {
return Question::where('title', 'LIKE', '%' . $keyword . '%')
// because select2 js plugin needs `text` and `id` column,
// so if your model does not contains these two, remember to AS for them
->select([DB::raw('title AS text'), 'id',])
->latest();
})->text(function ($id) {
// return type is `{id1: text1, id2: text2...}
return Question::whereIn('id', [$id])->pluck('title', 'id');
});
$form->textarea('content', 'Content');
return $form;
}
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new Answer);
$grid->column('user_id', __('User'))->sortable()->filterSelect2(function ($keyword) {
return User::where('name', 'LIKE', '%' . $keyword . '%')
->orWhere('email', 'LIKE', '%' . $keyword . '%')
->select([DB::raw('email AS text'), 'id',]);
});
}
}