product->destination is not being added in the show page of Cart in Crinsane Laravel Shopping cart - laravel

destination is not being added in Cart. Name , qty, price and id is passed and can be easily be accessed in the cart.index page , but the destination is not shown. Also tell me how can i pass the id="str" and id="rTotal" to cart.store page and show them in card.index page (These two variable are of javascript variables that contains some value). Can anyone help. Here is the code the i'm passing values of the product to the cart
<form action="{{route('cart.store')}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{$product->id}}">
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="destination" value="{{$product->destination}}">
<input type="hidden" id="str" name="str" value="">
<input type="hidden" id="rTotal" name="rTotal" value="">
<button type="submit">click me</button>
</p>
</form>
This is the code of my cartController
public function store(Request $request)
{
Cart::add(array('id' => $request->id, 'name' => $request->name, 'qty' => 1,
'price' => $request->price, 'destination' => $request->destination));
return redirect()->route('cart.index')->with('success_message','Item was added to your cart');
}
And here is the code of cart.index page that is showing all the other values except destination
<tbody>
#foreach(Cart::content() as $row)
<tr>
<td><img src="img/detailsquare.jpg" alt="White Blouse Armani" class="img-fluid"></td>
<td>{{$row->id}}</td>
<td>{{$row->name}}</td>
<td>{{$row->qty}}</td>
<td>{{$row->price}}</td>
<td>{{$row->destination}}</td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
#endforeach
</tbody>

Card::add method 5th argument is an option and you can pass as an array so you can give extra argument to it:
Cart::add($request->id,
$request->name,
$request->get('qty',1),
$request->price,
[
'destination' => $request->destination,
'rTotal' => $request->get('rTotal'),
'str' => $request->get('str')
]
);
If you want to try my Laravel E commerce always welcome to try out :
AvoRed an Laravel Shopping Cart

Related

no record inserted in the database in laravel

I have tried many times to insert a product which belongs to a category without change in the database
this is controller
class ProduitController extends Controller
{
public function store(Request $request)
{
$pdt=new Produit();
$pdt->nom=$request->input('nomPdt');
$pdt->libelle=$request->input('libelle');
$pdt->qte_stock=$request->input('qte');
$pdt->pu=$request->input('pu');
$pdt->categorie_id=$request->input('categorie');
$pdt->etat=1;
$pdt->save();
}}
View
<form action="{{url('produit/'.$pdt->id)}}" method="post">
<label for="nom">nom:</label><br>
<input type="text" id="" name="nom" value=""><br>
<label for="libelle">libellé:</label><br>
<input type="text" id="" name="libelle" value=""><br>
<label for="qte">Qte en stock:</label><br>
<input type="number" id="" name="qte" value="" min="0"><br>
<label for="pu">PU:</label><br>
<input type="number" id="" name="pu" value="" min="0"><br>
<label for="etat">Etat:</label>
<input type="checkbox" id="" name="etat" ><br>
<label for="selectCategorie">Categorie:</label>
<select id="" name="selectCategorie">
<option value="selectionnez">selectionnez</option>
#foreach ($categories as $cat)
<option value="{{ $cat->id }}"> {{ $cat->nom }} </option>
#endforeach
</select>
for the product table associated with the category table
We aren’t seeing the full picture of the route and uri here. It would be worth verify that. The easiest what to do that would be a temporary dd(‘here’); at the top of your controller's store() method to make sure you are reaching the controller method you expect to. Once that’s verified, you can remove the dd() and move on with confidence.
A few of the input names you reference within the controller do not match those in your form. You need to make sure everything matches the names from the forms.
$pdt=new Produit();
$pdt->nom=$request->input('nom');
$pdt->libelle=$request->input('libelle');
$pdt->qte_stock=$request->input('qte');
$pdt->pu=$request->input('pu');
$pdt->categorie_id=$request->input('selectCategorie');
$pdt->etat=1;//you have this on your form, but have hard coded here?
$pdt->save();
You can also clean this up slightly by loading all the input and using the create() method on the mode.
$inputData = $request()->all();
$pdt = Produit::create([
'nom' => Arr::get($input, 'nom'),
'libelle' => Arr::get($input, 'libelle'),
'qte_stock' => Arr::get($input, 'qte'),
'pu' => Arr::get($input, 'pu'),
'categorie_id' => Arr::get($input, 'selectCategorie'),
'etat’ => 1
]);
Hope this gets things working!

How to get session mobile in laravel

I want to after registered a user name, age, mobile and ... get the session of mobile because in next page, I want to get mobile in input hidden.
Attention: I did not do session at all.
I think it's like this:
RegisterController.php
public function register(Request $request, User $user)
{
$code = rand(10000,99999);
session->get('mobile')
$user = \App\User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'gender' => $request->gender,
'mobile' => $request->mobile,
'code' => $code,
.
.
.
return redirect()->route('code')->with('mobile', $request->mobile);
}
It redirect to this page.
Code
code.blade.php
<form action="{{ route('send') }}" method="post">
{{ csrf_field() }}
<input type="hidden" class="form-control" value="{{ session->mobile }}" name="mobile" id="mobile">
<div class="form-group">
<label for="code">کد</label>
<input type="text" class="form-control" name="code" id="code">
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger" id="btn-ok">OK</button>
</div>
</form>
use Illuminate\Support\Facades\Session;
For saving in session use
Session::put('mobile', $request->mobile);
then retrieve it using
Session::get('mobile');

how to solve this error in laravel: ErrorException (E_WARNING) Invalid argument supplied for foreach()

I am creating a medicine shop invoice system . So at that process, I am creating a invoice form where customer information and invoicing information store in a two different model Customer model and Invoice model.
My invoice form view code below;
<form action="{{url('/invoice')}}" method="post">
{{csrf_field()}}
<div class="form-row">
<div class="col-md-4 col-md-offset-2">
<label for="customerName">Customer Name:</label>
<input type="text" id="customerName" name="customerName" class="form-control"><br>
<label for="address"> Address:</label>
<input type="text" id="address" name="address" class="form-control"><br>
<label for="mobile"> Mobile No.:</label>
<input type="text" id="mobile" name="mobileNumber" class="form-control"><br>
</div>
<div class="col-md-4">
<label for="invoiceNo"> Invoice No.:</label>
<input type="text" id="invoiceNo" name="invoiceNum" class="form-control"><br>
<label for="date"> Date:</label>
<input type="date" id="date" name="date" class="form-control"><br>
</div>
</div>
<hr>
<table class="table table-bordered" cellpadding="10px" cellspacing="5px">
<thead>
<th>Meidicine Name:.</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
<th style="text-align: center"><i class="fas fa-plus-square plus "></i></th>
</thead>
<tbody>
<tr>
<td>
<select name="medicineName" id="" class="form-control-sm medicineName" >
#foreach($data as $item)
<option value="{{$item->id}}">{{$item->medicineName}}</option>
#endforeach
</select>
</td>
<td><input type="number" class="form-control-sm quantity" name="quantity"></td>
<td><input type="number" class="form-control-sm price" name="price"></td>
<td><input type="number" class="form-control-sm totalAmount" name="totalAmount"></td>
<td style="text-align: center"><i class="fas fa-times"></i></td>
</tr>
<tr><td><input type="submit" class="btn btn-info btn-sm" value="ADD"></td></tr>
</tbody>
</table>
</form>
In this invoice form medicine name came from another model Medicine using laravel eloquent relationship
my InvoiceController store method code below
public function store(Request $request)
{
$customer = new Customer();
$customer->fill($request->all());
if($customer->save()){
$id = $customer->id;
foreach($request->medicineName as $value => $key) --->error occurs
this line
{
$data = array(
'customer_id' => $id,
'medicine_id' => $value,
'invoiceNum' => $request->invoiceNum[$key],
'date' =>$request->date[$key],
'quantity' =>$request->quantity[$key],
'price' =>$request->price[$key],
'totalAmount'=> $request->totalAmount[$key]
);
Invoice::insert($data);
}
}
return back();
}
when I click ADD button to submit data two different model Customer & Invoice browser can show error look like
"ErrorException (E_WARNING)
Invalid argument supplied for foreach()"
How to solve this type of error, pls anyone can help me..
it's because $request->medicineName is just one element and not an array or collection, try it like this :
public function store(Request $request)
{
$customer = new Customer();
$customer->fill($request->all());
if($customer->save()){
$id = $customer->id;
$data = array(
'customer_id' => $id,
'medicine_id' => $request->medicineName,
'invoiceNum' => $request->invoiceNum,
'date' =>$request->date,
'quantity' =>$request->quantity,
'price' =>$request->price,
'totalAmount'=> $request->totalAmount
);
Invoice::insert($data);
}
return back();
}

fifth parameter is not displaying in the cart.index page

I'm sending five parameter to add to cart function,Here is the code
<form action="{{route('cart.store')}}" method="post"> {{ csrf_field() }}
<input type="hidden" name="id" value="{{$product->id}}">
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="destination" value="{{$product->destination}}">
value="">
<button type="submit">click me</button>
</form>
The code of cartController is
public function store(Request $request)
{
Cart::add($request->id, $request->name, 1 , $request->price , ['destination' => $request->destination]);
return redirect()->route('cart.index')->with('success_message','Item was added to your cart');
} // i have also tried
Cart::add(array('id' => $request->id, 'name' => $request->name, 'qty' => 1,
'price' => $request->price, 'destination' => $request->destination));
And the code for cart.index is
<tbody>
#foreach(Cart::content() as $row)
<tr>
<td><img src="img/detailsquare.jpg" alt="White Blouse Armani" class="img-fluid"></td>
<td>{{$row->id}}</td>
<td>{{$row->name}}</td>
<td>{{$row->qty}}</td>
<td>{{$row->price}}</td>
<td>{{$row->destination}}</td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
#endforeach
</tbody>
This is not giving me any error and display the product id,name,quantity,price but did not displays the destination. Anyone have a solution
Remove extra line after input type :
<form action="{{route('cart.store')}}" method="post"> {{ csrf_field() }}
<input type="hidden" name="id" value="{{$product->id}}">
<input type="hidden" name="name" value="{{$product->name}}">
<input type="hidden" name="price" value="{{$product->price}}">
<input type="hidden" name="destination[]" value="{{$product->destination}}">
/*value=""> */ // **remove this line**
<button type="submit">click me</button>
</form>
If not working then from cartController try var_dump($request) and see result.
Try this :
public function store(Request $request)
{
Cart::add($request->id, $request->name, 1 , $request->price , $request->destination);
return redirect()->route('cart.index')->with('success_message','Item was added to your cart');
}
Also check your model and db.

redirect to section of one-page site in laravel not working

I am working on a site in laravel. The email sending form is at the footer of the website. When I send an image, for now it just saves the email in the database. After saving the email, I want to redirect to the footer section of the one-page website but it doesn't. Funny thing is, it works right when validation is removed.
here is my form:
<form class="form" method="POST" action="{{ action('IndexController#email') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="name" class="form-control">
<input type="email" name="email" class="form-control">
<input type="text" name="subject" class="form-control">
<textarea class="form-control" name="message" style="height: 100px;"></textarea>
<button type="submit" class="btn btn-primary btn-block">Send</button>
</form>
Here is the post route:
Route::post('new/sendemail', ['as' => 'sendemail', 'uses' => 'IndexController#email']);
and here is the controller method:
public function email(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'subject'=>'required',
'message' => 'required'
]);
$input = $request->all();
Email::create($input);
return redirect('new/#contact'); //this is where i want to redirect to that section but it doesn't work
}

Resources