php namespace App Http Controllers use Illuminate Http Request use App

 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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \App\Models\Product;
class ProductsController extends Controller
{
public function index()
{
// Add categories
$query = Product::all();
return response()->json($query);
}
public function show($id)
{
// Add categories
$query = Product::find($id);
return response()->json($query);
}
public function destroy($id)
{
$p = Product::find($id)->delete();
return response()->json($p);
}
<<<<<<< HEAD
public function update($id, Request $request)
{
$name = $request->input('name');
$color = $request->input('color');
$description = $request->input('description');
$amount = $request->input('amount');
$prod = Product::find($id);
$prod->name = $name;
$prod->color = $color;
$prod->description = $description;
$prod->amount = $amount;
$prod->save();
return response()->json($prod);
}
public function add_category($id, $c_id)
{
$product = Product::find($id);
$p = $product::find($id)->categories()->attach($c_id);
return response()->json($p);
}
public function delete_category($id, $c_id)
{
$product = Product::find($id);
$query = $product->categories()->detach($c_id);
return response()->json($query);
}
=======
>>>>>>> parent of 46170f0... Добавил обновление для продукта по id
public function store(Request $request)
{
$name = $request->input('name');
$color = $request->input('color');
$description = $request->input('description');
$amount = $request->input('amount');
$prod = new Product;
$prod->name = $name;
$prod->color = $color;
$prod->description = $description;
$prod->amount = $amount;
$prod->save();
return response()->json($prod);
}
}