1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larrock\Core\Traits; |
4
|
|
|
|
5
|
|
|
use Larrock\Core\Component; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Larrock\Core\Helpers\MessageLarrock; |
8
|
|
|
use Larrock\Core\Helpers\FormBuilder\FormCategory; |
9
|
|
|
|
10
|
|
|
trait AdminMethodsCreate |
11
|
|
|
{ |
12
|
|
|
/** @var Component */ |
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creating a new resource. |
17
|
|
|
* @param Request $request |
18
|
|
|
* @return \Illuminate\Http\Response |
19
|
|
|
* @throws \Exception |
20
|
|
|
*/ |
21
|
|
|
public function create(Request $request) |
22
|
|
|
{ |
23
|
|
|
$post_rows = [ |
24
|
|
|
'title' => $request->get('title', 'Новый материал'), |
25
|
|
|
'url' => str_slug($request->get('title', 'Новый материал')), |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
if ($request->has('category')) { |
29
|
|
|
$post_rows['category'] = $request->get('category'); |
30
|
|
|
} else { |
31
|
|
|
foreach ($this->config->rows as $row) { |
32
|
|
|
if ($row->fillable && $row instanceof FormCategory) { |
33
|
|
|
if ($findCategory = \LarrockCategory::getModel()->whereComponent($this->config->name)->first()) { |
34
|
|
|
$post_rows[$row->name] = $findCategory->id; |
35
|
|
|
} else { |
36
|
|
|
MessageLarrock::danger('Создать материал пока нельзя. Сначала создайте для него раздел'); |
37
|
|
|
|
38
|
|
|
return back()->withInput(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$test = Request::create('/admin/'.$this->config->name, 'POST', $post_rows); |
45
|
|
|
|
46
|
|
|
if (! method_exists($this, 'store')) { |
47
|
|
|
$trait = new class { use AdminMethodsStore; }; |
48
|
|
|
return $trait->store($test); |
49
|
|
|
} |
50
|
|
|
return $this->store($test); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.