Thank you everyone for taking your time reading this thread.
Currently building a personal project in Laravel 8 and when using validate() two things happen:
1-. The data simply doesn't get send to the database (It doesn't show any error)
2-. After hitting "save" and redirecting to the previous page it shows a blank page. Now if I hit CTRL + F5 and resend the form, it keeps showing me a blank page, but if a just click the URL and hit enter then it shows me the page correctly with all the CSS and html.
web.php
Route::get('/', [\App\Http\Controllers\ClientController::class, 'index']);
Auth::routes();
Route::get('/backend/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::resource('backend/products', \App\Http\Controllers\ProductsController::class);
Route::resource('backend/posts', \App\Http\Controllers\PostsController::class);
ProductsController.php
public function index()
{
$products = Products::all();
return view('backend.products.index', ['products' => $products]);
}
public function create()
{
$categories = ProductsCategories::all();
return view('backend.products.create', compact('brands', 'categories'));
}
public function store(Request $request)
{
//dd($request->all());
$request->validate([
'name' => ['required'],
'description' => ['nullable'],
'normal_price' => ['required', 'numeric', 'min:0.01'],
'bulk_price' => ['required', 'numeric', 'min:0.01'],
'special_price' => ['required', 'numeric', 'min:0.01'],
'stock' => ['required', 'numeric', 'min:1'],
'image' => ['nullable']
]);
}
public function show(Products $product)
{
return view('backend.products.show', ['product' => $product]);
}
public function edit(Products $product)
{
return view('backend.products.edit', ['product' => $product]);
}
public function update(Request $request, Products $product)
{
//dd($request->all());
$request->validate([
'name' => ['required'],
'description' => ['nullable'],
'normal_price' => ['required', 'numeric', 'min:0.01'],
'bulk_price' => ['required', 'numeric', 'min:0.01'],
'special_price' => ['required', 'numeric', 'min:0.01'],
'stock' => ['required', 'numeric', 'min:1'],
'image' => ['nullable']
]);
return view(route('products.index'));
}
public function destroy(Products $products)
{
dd($products);
}
Products migration
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->float('normal_price')->unsigned();
$table->float('bulk_price')->unsigned();
$table->float('special_price')->unsigned()->nullable();
$table->integer('stock')->unsigned();
$table->string('image')->nullable();
$table->timestamps();
});
}
dd($request->all) In ProductsController
array:8 [
"_token" => "YsFZb3vqa5LpOoyHHwb0HRDpMtXRscnx5INteEbs"
"name" => "Aimee Stafford"
"description" => "Eveniet corporis co"
"normal_price" => "654"
"bulk_price" => "614"
"special_price" => "28"
"stock" => "13"
"image" => null
]
create.blade.php
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header"></div>
<div class="card-body">
<form action="{{route('products.store')}}" method="POST">
@csrf
<div class="mb-3 input-group">
<label for="name" class="input-group-text">Name</label>
<input type="text" class="form-control" id="name" name="name" required value="{{old('name')}}">
@error('name')
<span>{{$errors->first('name')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<textarea class="form-control" id="description" name="description" rows="3" placeholder="Description">{{old('description')}}</textarea>
@error('description')
<span>{{$errors->first('description')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" id="normal_price" name="normal_price" required step="0.01" placeholder="Normal price" value="{{old('normal_price')}}">
@error('normal_price')
<span>{{$errors->first('normal_price')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" id="bulk_price" name="bulk_price" required step="0.01" placeholder="Bulk price" value="{{old('bulk_price')}}">
@error('bulk_price')
<span>{{$errors->first('bulk_price')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<span class="input-group-text">$</span>
<input type="number" class="form-control" id="special_price" name="special_price" required step="0.01" placeholder="Special price" value="{{old('special_price')}}">
@error('special_price')
<span>{{$errors->first('special_price')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<label for="stock" class="input-group-text">Stock</label>
<input type="number" class="form-control" id="stock" name="stock" min="1" step="1" required value="{{old('stock')}}">
@error('stock')
<span>{{$errors->first('stock')}}</span>
@enderror
</div>
<div class="mb-3 input-group">
<label for="image" class="input-group-text">Image</label>
<input type="file" class="form-control" id="image" name="image"/>
@error('image')
<span>{{$errors->first('image')}}</span>
@enderror
</div>
<div class="mb-3">
<input type="submit" class="btn btn-primary" value="Save">
<a href="{{route('home')}}" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
If I check the browser console on that blank page i mentioned (after hitting the store method) it doesn't show any errors.
A have the same problem in Laravel 6.
I have been stuck in this problem for a couple of days by now, and I don't understand the issue.
Thank you anyone who responds.