Passed
Push — master ( ccdc18...c3c113 )
by Gianluca
05:54
created

AdminCreateDetailController::setNewDetail()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 29
rs 9.2888
c 1
b 0
f 0
cc 5
nc 2
nop 1
1
<?php
2
3
4
namespace Mongi\Mongicommerce\Http\Controllers\admin;
5
6
use Illuminate\Http\Request;
7
use Mongi\Mongicommerce\Http\Controllers\Controller;
8
use Mongi\Mongicommerce\Models\Detail;
9
use Mongi\Mongicommerce\Models\DetailValue;
10
11
class AdminCreateDetailController extends Controller
12
{
13
    public function page(){
14
        $types_details = config('mongicommerce.details');
15
        return view('mongicommerce::admin.pages.details.create_details',['types' =>$types_details ]);
16
    }
17
18
    public function setNewDetail(Request $r){
19
20
        $r->validate([
21
            'name' => 'required',
22
            'type' => 'required',
23
            'category' => 'required'
24
        ]);
25
26
        $name = $r->get('name');
27
        $type = $r->get('type');
28
        $values = $r->get('values');
29
        $category_id = $r->get('category');
30
31
        $detail = new Detail();
32
        $detail->category_id = $category_id;
33
        $detail->type = $type;
34
        $detail->name = $name;
35
        $detail->save();
36
37
        if($type === 'select' || $type === 'checkbox' || $type === 'radio'){
38
            foreach ($values as $value){
39
                $datails_value = new DetailValue();
40
                $datails_value->detail_id = $detail->id;
41
                $datails_value->value = $value;
42
                $datails_value->save();
43
            }
44
        }
45
46
        return response()->json(true);
47
    }
48
}
49