Product editing

This commit is contained in:
bad 2021-12-08 18:25:48 +01:00
parent 09d737dfab
commit 46e1fa8aee
7 changed files with 62 additions and 13 deletions

View file

@ -1 +1 @@
{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036}}
{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036,"\/tmp\/neoformat\/edit.blade.php":2692964585,"\/tmp\/neoformat\/ProductController.php":4228240463}}

View file

@ -50,5 +50,6 @@ class ImageController extends Controller
public function destroy(Image $image)
{
$image->delete();
return back();
}
}

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Image;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
@ -21,7 +22,8 @@ class ProductController extends Controller
*/
public function create()
{
return view("product/create");
$prod = new Product();
return view("product/edit", [ 'product' => $prod,'new'=>true]);
}
/**
@ -32,17 +34,23 @@ class ProductController extends Controller
*/
public function store(Request $request)
{
Log::debug($request);
$request->validate([
$validated = $request->validate([
'name'=>'required',
'description'=>'required',
'price'=>'required|numeric',
'visualization' => 'image|mimes:jpg,png,jpeg,gif,svg,webp|max:5000',
]);
$product = new Product([
'name' => $request->get("name"),
'description' => $request->get("description")
]);
$product = new Product($validated);
$product->save();
//echo $product->uuid;
if (isset($validated["visualization"])) {
$visualization = new Image();
$visualization->path = $request->file('image')->store("uploads");
$product->images()->save($visualization);
}
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
}
@ -54,7 +62,7 @@ class ProductController extends Controller
*/
public function show(Product $product)
{
return view("product/view", ["product" => $product]);
return view("product.view", ["product" => $product]);
}
/**
@ -65,7 +73,7 @@ class ProductController extends Controller
*/
public function edit(Product $product)
{
//
return view("product.edit", ["product" => $product, 'new'=>false]);
}
/**
@ -79,6 +87,14 @@ class ProductController extends Controller
{
$product->fill($request->all());
$product->save();
if (isset($request["image"])) {
$visualization = new Image();
$visualization->path = $request->file('image')->store("uploads");
$product->images()->save($visualization);
}
//dd($product, $request, $visualization);
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
}

View file

@ -19,5 +19,7 @@ class Product extends Model
protected $fillable = [
'name',
'description',
'images',
'price'
];
}

View file

@ -2,7 +2,7 @@
<a href="{{ route('product.show', ['product' => $product]) }}">
<div class="product-container">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ "" }} @endif">
<div class="name-price-container">
<h2 class="product-name"> {{ $product->name }} </h2>
<h3>{{ ($product->price) }}<span class="price-currency"></span></h3>

View file

@ -0,0 +1,30 @@
@extends('layouts.app')
@section('title', "Sklep")
@section('main')
@if($new)
@php($post_url = route("product.store"))
@else
@php($post_url = route("product.update", $product))
@endif
<form action="{{ $post_url }}" method="post" enctype="multipart/form-data">
<input type="text" name="name" value="{{ $product->name }}">
<textarea name="description">{{ $product->description }}</textarea>
<input type="number" name="price" value="{{ $product->price }}">
<input type="file" name="image">
<button type="submit">Zatwierdź</button>
@if(!$new)
@method("PATCH")
@endif
@csrf
</form>
@foreach($product->images as $vis)
<form action="{{ route("image.destroy", $vis) }}" method="post">
<img src="{{ $vis->URL() }}"/>
<button type="submit">Usuń</button>
@method("DELETE")
@csrf
</form>
@endforeach()
@endsection()

View file

@ -43,4 +43,4 @@ Route::resource("product", ProductController::class)->only(["show"]);
Route::resource("order", OrderController::class)->middleware("auth");
Route::post('/order/create', [OrderController::class, "create"])->name("order.create");
Route::resource("image", ImageController::class)->only(["store", "delete", "create"])->middleware("auth.admin");
Route::resource("image", ImageController::class)->only(["store", "destroy", "create"])->middleware("auth.admin");