Skip to content

Commit

Permalink
Fetched product data form the blade
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Jan 18, 2024
1 parent 166e47a commit 819e6e4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/ProductAdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace App\Http\Controllers;

use App\Http\Requests\CreateProductAdRequest;
use App\Models\Category;
use App\Models\ProductAd;
use Illuminate\Http\Request;
Expand All @@ -29,9 +30,10 @@ public function create()
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
public function store(CreateProductAdRequest $request)
{
//
$selectedCategories = $request->all('categories');
dd($selectedCategories);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Requests/CreateProductAdRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateProductAdRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => ['required', 'bail', 'exists:users,id'],
'product_title' => ['required', 'bail'],
'product_description' => ['required', 'bail'],
'product_price' => ['required', 'bail'],
'product_tag' => ['required', 'bail'],
'product_image' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
'categories' => ['required','array'],
];
}
}
9 changes: 9 additions & 0 deletions app/Models/ProductAd.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ class ProductAd extends Model
'product_tag',
'image_path',
];
public function user()
{
return $this->belongsTo(User::class);
}

public function categories()
{
return $this->belongsToMany(Category::class);
}
}
14 changes: 8 additions & 6 deletions resources/views/userend/create-product.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<section>
<div class="form-container">
<span>Create an Ad</span>
<form action="" method="post">
<form action="{{ route('product-ad-post') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label for="product_title" class="form-label">Product Title</label>
<input type="text" class="form-control" name="product_title" id="exampleFormControlInput1" placeholder="Enter Product Title">
Expand All @@ -33,17 +34,18 @@
<input type="text" class="form-control" name="product_tag" id="exampleFormControlInput1" placeholder="Enter Product Tag">
</div>
<div class="mb-3">
<label for="formFile" class="form-label">Upload Product Image</label>
<input class="form-control" type="file" id="formFile">
<label for="formFile" class="form-label">Upload Product Image (Max: 2MB)</label>
<input class="form-control" type="file" id="formFile" name="product_image">
</div>
<div class="mb-3">
<select class="form-select" size="3" aria-label="size 3 select example" id="categories">
<option selected value="">Select Categories</option>
<label for="formFile" class="form-label">Select Categories</label>
<select class="form-select" size="3" aria-label="size 3 select example" id="categories" name="categories[]" multiple>
@foreach ($mainParent as $parentCategory)
@include('userend.commonComponents.create-product-category', ['category' => $parentCategory])
@endforeach
</select>
</div>
<input type="hidden" name="user_id" value="{{ auth()->id() }}">
<input class="btn btn-primary" type="submit" value="Create Ad">
<span class="error-message">
@if(session('message'))
Expand All @@ -52,7 +54,7 @@

@if($errors->any())
@foreach ($errors->all() as $error)
{{ $error}} <br>
{{ $error }} <br>
@endforeach
@endif
</span>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
//guest route protection
Route::middleware(['guest.authenticate'])->group(function () {
Route::get('product-ad', [ProductAdController::class, 'index'])->name('product-ad-form');
Route::post('product-ad',[ProductAdController::class, 'store'])->name('product-ad-post');
});


Expand Down

0 comments on commit 819e6e4

Please sign in to comment.