Laravel Insert Record after field Validation - php

I'm trying a Laravel 5.6 request validation.I face several problems
1.Cannot view validation Messages in view.
2.After adding validation code data not insert to the database.(before adding validation it's work)
3 How can I validation dropdown status ?(Should select Active/Inactive)
designation.blade.php
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation" class="form-control" id="inputDesignation">
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected>Select Status</option>
<option >Active</option>
<option >Inactive</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
DesignationController.php
public function store(Request $request)
{
//Validation the Data
$validatedData = $request->validate([
'designation_type' => ['required','max:255'],
'status' => ['required'],
],
[
'designation_type.required' => 'Designation is required',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
]);
//Data Insert into database
$data =[
'designation_type'=>$request->input('designation'),
'status'=>$request->input('status')
];
DB::table('designation')->insert($data);
return redirect('/designation');
}
Please help me to solve this !!

try following code
public function store(Request $request)
{
//Validation the Data
$validatedData = $request->validate([
'designation_type' => ['required','max:255'],
'status' => ['required'],
],
[
'designation_type.required' => 'Designation is required',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
]);
if($validatedData->fails()) {
return Redirect::back()->withErrors($validatedData);
}
//Data Insert into database
$data =[
'designation_type'=>$request->input('designation'),
'status'=>$request->input('status')
];
DB::table('designation')->insert($data);
return redirect('/designation');
}
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation" class="form-control" id="inputDesignation">
#if($errors->has('designation'))
<div class="error">{{ $errors->first('designation') }}</div>
#endif
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected>Select Status</option>
<option >Active</option>
<option >Inactive</option>
</select>
#if($errors->has('status'))
<div class="error">{{ $errors->first('status') }}</div>
#endif
</div>
</div>
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>

public function store(Request $request)
{
$this->validate($request,[
'name' => 'required'
]);
$category = new Category();
$category->name = $request->name;
$category->slug = str_slug($request->name);
$category->save();
Toastr::success('Category Successfully Saved','Success');
return redirect()->route('admin.category.index');
}
// blade
<form method="POST" action="{{ route('admin.category.store') }}">
#csrf
<div class="form-group form-float">
<div class="form-line">
<input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">
<label class="form-label">{{ __('Name') }}</label>
</div>
</div>
<br>
{{ __('BACK') }}
<button type="submit" class="btn btn-primary m-t-15 waves-effect">{{ __('SUBMIT') }}</button>
</form>

After refer this https://laravel.com/docs/5.6/validation#named-error-bags I did few changes in the code and it's help to solved errors.
In designation.blade.php added
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
#endforeach
</ul>
</div>
#endif
In DesignationController.php
for status dropdown validation.
'status' => 'required|not_in:0',
for data insert database part
DB::table('designation')->insert($validatedData);
Full Code
designation.blade.php
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
#endforeach
</ul>
</div>
#endif
</div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation_type" class="form-control" id="inputDesignation">
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected value="">Select Status</option>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
</div>
</div>
{{-- <button type="submit" class="btn btn-primary">Sign in</button> --}}
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
</div>
<div class="col-md-4"></div>
</div>
DesignationController.php
public function store(Request $request)
{
$validatedData = $request->validate([
'designation_type' => 'required|max:255',
'status' => 'required|not_in:0',
],
[
'designation_type.required' => 'Designation is required !!',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
'status.required' => 'Status is required !!'
]);
DB::table('designation')->insert($validatedData);
return redirect('/designation');
}
Thank You Guys spend your valuable time to help me....!!!

Related

302 found in Laravel project

Route
Route::get('/addproduct', [UserController::class, 'addproduct'])->name('addproduct');
Route::post('/addnewproduct', [UserController::class, 'addnewproduct'])->name('addnewproduct');
Route::get('/showproducts', [UserController::class, 'showproducts'])->name('showproducts');
Controller
public function addproduct()
{
return view('addProduct');
}
public function addnewproduct(Request $request)
{
$user_id = Auth::user()->id;
$request->validate([
'file' => 'required|mimes:jpg,png,gif,svg',
'name' => 'required|string|min:3|max:30',
'description' => 'required|string|min:1|max:255',
'category' => 'required|string|max:255',
]);
$productModel = new Product();
$filename =time().'_' .$request->name;
$filePath = $request->file('file')->move('upload', $filename);
$productModel->name = $request->name;
$productModel->description = $request->description;
$productModel->category = $request->category;
$productModel->image = $filePath;
$productModel->save();
return redirect()->route('showproducts');
}
public function showproducts()
{
$user_id = Auth::user()->id;
$results=DB::select('SELECT * FROM products');
$data = [
'results' =>$results
];
return view('showProduct')->with($data);
}
View
<div class="card-body">
<form method="POST" action="{{ route('addnewproduct') }}" class="needs-validation form" enctype="multipart/form-data" novalidate>
#csrf
<div class="row">
<div class="col-sm-6">
<input type="file" id="input-file-now" class="form-control dropify" name="file" required/>
</div>
<div class="col-sm-6">
<div class="row mb-3">
<label for="validationName" class="form-label">Product name</label>
<div class="input-group has-validation">
<input type="text" class="form-control" name="name" id="validationName" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
You have to enter Product name!
</div>
</div>
</div>
<div class="row mb-3">
<label for="validationCategory" class="form-label">Category</label>
<div class="input-group has-validation">
<select class="form-select" name="category" id="validationCategory" required>
<option selected disabled value="">Select...</option>
<option value="c1">c1</option>
<option value="c2">c2</option>
</select>
</div>
<div class="invalid-feedback">
Please select Category
</div>
</div>
<div class="row mb-3">
<label for="validationDes" class="form-label">Description</label>
<div class="input-group has-validation">
<textarea type="text" class="form-control text-des" name="address" id="validationDes" aria-describedby="inputGroupPrepend" required></textarea>
<div class="invalid-feedback">
Please enter product descriiption in here.
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Check it.
</label>
<div class="invalid-feedback">
You have to checkbox!
</div>
</div>
</div>
<div class="row">
<div class="float-end">
<button class="btn btn-primary sumbtn float-end" type="submit"><i class="bi bi-person-plus-fill"></i> ADD</button>
</div>
</div>
</form>
</div>
after enter all fields and file select and then click checkbox, but when I click button don't go to showproducts page, and don't save enterd data and file.
view screenshot
after click button get 302 and not redirect to "showproducts"
Please help me, I'm very very stress with that problem

Laravel 8 safe (think problem datetime-local input field to mysql)

When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output

dropdown list not shown in laravel

I've got problem with dropdown list.
i want to show a primary key as a dropdown list in view blade , but i can not make the right rout to show it. Have you any ideas how to solve this problem?
this my root
$objects = ['users', 'permissions', 'roles', 'coins','pillars','subtindicators'];
foreach ($objects as $object) {
Route::get("$object", ucfirst(str_singular($object))."Controller#index")->middleware("permission:browse $object")->name("{$object}");
Route::get("$object/datatable", ucfirst(str_singular($object))."Controller#datatable")->middleware("permission:browse $object")->name("{$object}.datatable");
Route::get("$object/add", ucfirst(str_singular($object))."Controller#add")->middleware("permission:add $object")->name("{$object}.add");
Route::post("$object/create", ucfirst(str_singular($object))."Controller#create")->middleware("permission:add $object")->name("{$object}.create");
Route::get("$object/{id}/edit", ucfirst(str_singular($object))."Controller#edit")->middleware("permission:edit $object")->name("{$object}.edit");
Route::post("$object/{id}/update", ucfirst(str_singular($object))."Controller#update")->middleware("permission:edit $object")->name("{$object}.update");
Route::get("$object/{id}/delete", ucfirst(str_singular($object))."Controller#delete")->middleware("permission:delete $object")->name("{$object}.delete");
Route::get("$object/{id}", ucfirst(str_singular($object))."Controller#view")->middleware("permission:view $object")->name("{$object}.view");
Route::get("$object/create", ucfirst(str_singular($object))."Controller#list")->middleware("permission:add $object")->name("{$object}.create");
this my controller
public function add()
{ $strs = DB::table('stargets')->select('*')->get();;
return view('subtindicators.add-edit',compact('strs'));
}
public function update(Request $request, $id)
{
$object = $this->objectModel::find($id);
$object->update([
'skey_name' => $request->skey_name,
'Subtarget_base' => $request->Subtarget_base,
'Subtarget_end' => $request->Subtarget_end,
'subtarget_id' => $request->subtarget_id
]);
if ($request->save == 'browse')
return redirect()->route("{$this->objectName}");
elseif ($request->save == 'edit')
return redirect()->route("{$this->objectName}.edit", ['id' => $object]);
elseif ($request->save == 'add')
return redirect()->route("{$this->objectName}.add");
else
return redirect($request->previous_url);
}
this my blade
#extends('adminlte::page')
#include('bread.title')
#section('main-content')
<div class="container-fluid spark-screen">
<div class="row">
{!! csrf_field() !!}
<form class="form-horizontal" action="{{$actionName=='edit'?route("{$objectName}.update",['id'=>$object->id]):route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<form class="form-horizontal" action="{{ $actionName == 'edit' ? route("{$objectName}.update", ['id' => $object->id]) : route("{$objectName}.create") }}" method="post">
{!! csrf_field() !!}
<input type="hidden" name="previous_url" value="{{ url()->previous() }}">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{ ucfirst($actionName) }} {{ ucfirst($objectName) }} {{ !empty($object) ? "(ID $object->id)" : '' }}</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
</div>
<div class="box-body">
<div class="form-group">
<label for="" class="col-md-2 control-label">col</label>
<div class="col-md-10">
<input type="text" name="skey_name" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_d</label>
<div class="col-md-10">
<input type="text" name="Subtarget_base" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_enname : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="col-md-2 control-label">cl_e</label>
<div class="col-md-10">
<input type="text" name="Subtarget_end" class="form-control" maxlength="255" value="{{ !empty($object) ? $object->coin_arsymbol : '' }}" required>
</div>
</div>
<div class="form-group">
<label for="" class="form-control">c_i</label>
<div class="col-md-10">
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $vis)
<option value="{{$vis->id}}">{{$vis->target_name}}</option>
#endforeach
<!-- <p class="form-control-static">{{ $object->subtarget_id }}</p>-->
</div>
</div>
<div class="form-group has-feedback">
<label for="title">main<span class="text-danger">*</span></label>
<select class="form-control" name="starget_id">
<option selected disabled value = " ">choos</option>
#foreach($strs as $slider2)
<option value="{{$slider2->id}}" {{ $slider2->id== $slider->starget_id ? 'selected' : '' }}>{{$slider2->vname}}</option>
#endforeach
</div>
</div>
</div>
<div class="box-footer">
#include('bread.add-edit-actions')
</div>
</div>
</form> </form>
</div>
</div>
</div>
#endsection
this error what i got
ErrorException (E_ERROR)
Undefined variable: actionName (View: C:\project Last\resources\views\bread\title.blade.php) (View: C:\Ministry Last\resources\views\bread\title.blade.php)
You should forward $actionName variable to your view:
public function add()
{
$strs = DB::table('stargets')->select('*')->get();
$actionName = 'edit';
return view('subtindicators.add-edit',compact('strs', 'actionName'));
}
However this will hardcode the form to being an "edit" form. You must see what you really want with your logic.

Laravel - Refresh only a section instead of refresh the whole page on submit form

I create a contact form on the home page of the website.
This is the code:
<section id="contact" class="light-grey-background m-top-30">
<div class="container">
<header>
<h2 class="title-contact text-center">
<b>CONTATTACI</b>
</h2>
</header>
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">x</button>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form id="formEmail" class="m-top-40" method="post" action="{{ url('/send') }}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-sm-12">
<div class="form-group">
<input id="oggetto" name="oggetto" type="text" placeholder="Oggetto" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="nome" name="nome" type="text" placeholder="Nome" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="cognome" name="cognome" type="text" placeholder="Cognome" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="email" name="email" type="email" placeholder="Email" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea id="messaggio" rows="6" placeholder="Scrivi qui il tuo messaggio..." name="messaggio" class="form-control"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="send" class="btn btn-primary contact m-bottom-70 text-center" value="Send" />
</div>
</div>
</div>
</form>
</div>
</section>
This is the HomeController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
function index()
{
return view('home');
}
function send(Request $request)
{
$this->validate($request, [
'oggetto' => 'required',
'nome' => 'required',
'cognome' => 'required',
'email' => 'required|email',
'messaggio' => 'required'
]);
}
}
And this is the route for the home page:
Route::get('/', 'HomeController#index');
Route::post('/send', 'HomeController#send');
The problem is that when you click on the send button the page is completely updated and to know the status of the form you have to scroll the page. Is there a way to avoid the problem and update only the section that contains this?

Passing dates from form into blade view

I have a shopping cart that has a pick-up and return date option that needs to be selected. I cannot get either of them to be echoed into the cart view when they are selected. I am using the Laravel Shopping Cart library to build the cart, which has an array for the extra options. I have passed the values into there to pass into the view, but it doesn't seem to work.
Here is the form mark:
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bakersfield">Bakersfield</option>
<option value="chico">Chico</option>
<option value="fresno">Fresno</option>
<option value="hayward">Hayward</option>
<option value="manteca">Manteca</option>
<option value="oakley">Oakley</option>
<option value="redwood_city">Redwood City</option>
<option value="sacramento">Sacramento</option>
<option value="salinas">Salinas</option>
<option value="san_jose">San Jose</option>
<option value="san_jose_fusion">San Jose Fusion</option>
<option value="santa_rosa">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
<input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
<input type="hidden" name="returnDate" value="returnDate">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
Here is the cart view:
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
<p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
And here is the create item part of the controller:
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
From your form blade
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">
Pick Up Date
</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
then in the bottom somewhere of your master/app blade you put
<script>
$(function() {
$("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
<script type="text/javascript">
$(function() {
$("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
be sure to include these necessary files jquery.min.js, and if you use bootstrap then you need bootstrap.min.js, bootstrap-datepicker.min.js
Please let me know if it works

Categories