fifth parameter is not displaying in the cart.index page - laravel

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.

Related

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');

Getting values to pass in Payfast

I'm trying to intergrate Payfast with my laravel project. The problem I'm having is that the data isn't being passed to Payfast so Payfast keeps giving me an error saying The supplied variables are not according to specification:
Here is my code in my controller
public function payPayfast(Request $request)
{
$data = [
'merchant_id' => $request->merchant_id,
'merchant_key' => $request->merchant_key,
'return_url' => $request->return_url,
'cancel_url' => $request->cancel_url,
'm_payment_id' => $request->m_payment_id,
'amount' => $request->amount,
'item_name' => 'Test Item From Controller',
'item_description' => 'This is a test product',
'email_confirmation' => '1',
'confirmation_address' => '',
'payment_method' => $request->payment_method,
'signature' => $request->signature,
];
return redirect()->to('https://sandbox.payfast.co.za/eng/process')->with('data', $data);
}
here is my code in my confirmation.blade.php
<div class="content_wrapper">
<h1>Order Confrimation</h1>
<div class="row">
<div class="col-lg-12">
<form action="{{ route('payfast.payPayfast') }}" method="POST">
#csrf
<input type="hidden" name="merchant_id" value="11111111">
<input type="hidden" name="merchant_key" value="2222222222222">
<input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">
<input type="hidden" name="notify_url" value="{{ route('payfast.notify') }}">
<input type="hidden" name="m_payment_id" value="01AB">
<input type="hidden" name="amount" class="completePrice" value="{{ $total }}">
<input type="hidden" name="item_name" value="Test Item">
<input type="hidden" name="item_description" value="A test product">
<input type="hidden" name="email_confirmation" value="1">
<input type="hidden" name="confirmation_address" value="test#test.com">
<input type="hidden" name="payment_method" value="eft">
<?php
$success = url('payfast-success');
$cancel = url('payfast-cancel');
$notify = url('payfast-notify');
$original_str = getAscii('merchant_id=11111111&merchant_key=2222222222222&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=test#test.com&payment_method=eft');
$hash_str = hash('MD5', $original_str);
$hash = strtolower($hash_str);
?>
<input type="hidden" name="signature" value="{{ $hash }}">
<button class="btn btn-dark" type="submit"><img src="{{ asset('img/eft-payfast.jpg') }}"></button>
</form>
</div>
</div>
</div>
The with function adds the data to the session, but PayFast expects the data to be in the url, e.g. https://sandbox.payfast.co.za/eng/process?amount=100&merchant_id=12&merchant_key=23&item_name=cool.
It doesn't seem like the URLGenerator provides an easy way to turn an array into query parameters. You could simply construct the query using string concatenation or have a look at this answer which includes a function to construct such a path.

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

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

How to insert multiple rows in laravel 5?

I want to insert an array with an id :
create.blade :
{{ Form::open(array('route' => 'Charge.store','method'=>'POST')) }}
<select id="disabledSelect" class="form-control" name="Facture_id">
<option value="{{ $Facture->id }}" >{{ $Facture->Num }}</option>
</select>
<br/>
<div class="form-inline">
<div class="form-group">
<input type="text" class="form-control" name="rows[0][Title]" placeholder="libelé"/>
</div>
<div class="form-group">
<input type="text" class="form-control" name="rows[0][Quantity]" placeholder="Quantité"/>
</div>
<div class="form-group">
<input type="text" class="form-control" name="rows[0][Price]" placeholder="Prix unitaire "/>
</div>
<div class="form-group">
<input type="button" class="btn btn-default" value="Ajouter" onclick="createNew()" />
</div>
<div id="mydiv"></div>
</div>
<br/>
<div class="form-group">
<input type="submit" value="Ajouter" class="btn btn-info">
Cancel
</div>
{{ Form::close() }}
<script>
var i = 2;
function createNew() {
$("#mydiv").append('<div class="form-group">'+'<input type="text" name="rows[' + i +'][Title]" class="form-control" placeholder="libelé"/>'+
'</div>'+'<div class="form-group">'+'<input type="text" name="rows[' + i +'][Quantity]" class="form-control" placeholder="Quantité"/>'+'</div>'+'<div class="form-group">'+'<input type="text" name="rows[' + i +'][Price]" class="form-control" placeholder="Prix unitaire "/>'+'</div>'+'<div class="form-group">'+
'<input type="button" name="" class="btn btn-default" value="Ajouter" onclick="createNew()" />'+
'</div><br/>');
i++;
}
</script>
here is my controller , when I tried to submit the form , it inject rows with value of 0.
What should I do ? I tried to use elequent bolk data , but the problem remain the same:
public function store(Request $request)
{
// validated input request
$this->validate($request, [
'Facture_id' => 'required',
]);
// create new task
$rows = $request->input('rows');
foreach ($rows as $row)
{
$Charges[] = new Charge(array(
'course_id'=>$request->input('Facture_id'),
'Title'=>$row['Title'],
'Quantity'=>$row['Quantity'],
'Price'=>$row['Price'],
));
}
Charge::create($Charges);
return redirect()->route('Charge.index')->with('success', 'Your task added successfully!');
}
You can use insert() method:
foreach ($rows as $row)
{
$charges[] = [
'course_id' => $request->input('Facture_id'),
'Title' => $row['Title'],
'Quantity' => $row['Quantity'],
'Price' => $row['Price'],
];
}
Charge::insert($charges);
Don't forget to add all column names you use to a $fillable array:
$fillable = ['course_id', 'Title', 'Quantity', 'Price'];

How to upload picture and store to database in Laravel 5

I need to upload picture and store to database in Laravel 5.
My current code is:
Form:
<form method="POST" enctype="multipart/form-data" action="{{ url('products/new) }}">
{!! csrf_field() !!}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:100|unique:products',
]);
$input = $request->all();
Product::create($input);
return redirect('products');
}
The function $request->hasFile('image')) returns false.
hasFile('image') returns false because there is not an input with name 'image', only the id is 'image'
You should give it a name property, since that's the way the inputs are sent through http.
Instead of this:
<input type="file" id="image">
use this
<input name="image" type="file" id="image">
and in your validator use this:
$this->validate($request, [
'image' => 'required|max:100|unique:products',
]);

Resources