Hi I am working on the checkout page in laravel and sent some product data from the cart to checkout and trying to print all the details from an json object but i keep getting the error as Trying to get property 'id' of non-object
The controller function is
public function bill(Request $request){
$input = $request->all();
return view('demo')->with('product' , $request->product)
->with('subtotal' , round($request->subtotal));
}
the cart form is
<form method="post" action="{{ route('pay')}}">
{{ csrf_field() }}
#foreach($cart as $product)
<input type="hidden" name="product[]" value="{{ $product }}">
#endforeach
<input type="hidden" name="subtotal" value="{{$subtotal}}">
<button type="submit" class="gray_btn">Checkout</button>
</form>
the blade page is
#foreach($product as $input)
{{ $input }}
{{ $input->id }}
#endforeach
when i only print the input i am getting the result as
{"id":"10","name":"S007-4ft","price":40,"quantity":"102","attributes":{"image":"glassfilms\/December2019\/MyelERNBbAWhGRbKWiCK.jpg","crm":"PRO209"},"conditions":[]} {"id":"7","name":"Frosted 007-4ft","price":40,"quantity":"103","attributes":{"image":"glassfilms\/December2019\/ZJgWUNaYrPnvsoRfuagv.jpg","crm":"PRO105"},"conditions":[]}
but when i try to print the id only using {{input->id }} i am getting the error.
the route is
Route::post('pay', 'RazorpayController#bill')->name('pay');
You have to pass an array in order to have access by a key, i.e. call json_decode() when returning it:
public function bill(Request $request) {
$input = $request->all();
return view('demo')
->with('product', json_decode($request->product, true)) // <-- here
->with('subtotal', round($request->subtotal));
}
Otherwise the returned value is a just simple string in JSON "shape". Error say it clear ehough: non-object.
You are using json object as php object please decode your json object first than you can use it as php object:
$obj = json_decode($input);
$obj->id // Now you can use it like this
Related
$cartItems contains all the rows of products from database and I am using this inside a blade file.
I want to pass this $cartItems back to a controller from this blade file
Note: $cartItems is from index() function in a controller like below.
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.index', compact('cartItems')
Below is my code.
index.blade.php
Proceed to checkout
web.php
Route::get('/cart/checkout/{cartItems}', 'CartController#checkout')->name('cart.checkout')->middleware('auth');
CartController.php
public function checkout($cartItems)
{
dd($cartItems);
return view('cart.checkout');
}
The error I am getting is,
Missing required parameters for [Route: cart.checkout] [URI: cart/checkout/{cartItems}]. (View: E:\github\LARAVEL\Deal-Ocean\resources\views\cart\index.blade.php)
You can use a form to send data back to server
Update your route from get to post
Route::post('/cart/checkout', 'CartController#checkout')->name('cart.checkout')->middleware('auth');
Use a form to post data to server. You can pass any additional data along with the request as well.
<form method="post" action="/cart/checkout">
#foreach($cartItems as $item)
<input name="cartItems[]" value="{{ $item->id }}"
#endforeach
<button class="site-btn">Proceed to checkout</button>
</form>
And in your controller use Request to access data
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
If you want to proceed with the get request you should be able to do as follow
As $cartItems is a collection of products. So you can send the product ids and query the products using the ids from request.
<a href="{{ route('cart.checkout', ['cartItems' => $cartItems->pluck('id')->toArray()]) }}"
class="site-btn">Proceed to checkout</a>
Update controller
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
Why use the same code logic of the index() method in the checkout method in the
CartController.
the checkout method will look like this:
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.checkout', compact('cartItems');
I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
I want to change the status of a task to complete. I have a status_id column in the database and 1 equals complete. I would like the click of the button to change the status_id to 1
My route
Route::patch('/tasks/completed/{Task}', 'TasksController#completedUpdate')->name('completedUpdate');
My button
<form action="{{ route('completedUpdate', $task->id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<button type="submit" class="button is-inverted" style="margin-top: 10px;">Mark Complete</button>
</form>
My controller
public function completedUpdate(Request $request, $task)
{
$task->status_id = $request->status_id;
$task->save;
return redirect()->back()->with('message', 'task marked complete');
}
the error it gives me is:
Attempt to assign property of non-object
Let me know if any more info is needed
You should change:
public function completedUpdate(Request $request, $task)
{
$task->status_id = $request->status_id;
$task->save;
return redirect()->back()->with('message', 'task marked complete');
}
into:
public function completedUpdate(Request $request, Task $task)
{
$task->status_id = $request->status_id;
$task->save();
return redirect()->back()->with('message', 'task marked complete');
}
so you need to typehint type of $task variable and use save() method instead of save property.
Also probably instead of:
/tasks/completed/{Task}
you should use:
/tasks/completed/{task}
$task->save; should be $task->save();
With ->save, it is looking for a property on the model, hence the error message re 'assigning a property'. Whereas ->save() calls the save method on the object.
In your controller, you're assigning the $task->status_id a value of $request->status_id but you're actually not passing the status_id in your form in your HTML code. You can put a hidden element in your form which is <input name="status_id" value="1" />.
In the meanwhile, do not forget that $task->save; must be $task->save();
Good luck!
I have link like this with token:
Submit New
which produce url:
http://example.com/users/submit/20?_token=fpf0LwHyf0JGBg0fnixjRFo1B5GgUM3RDp6dVgUU
Now in my controller I've added condition which check one column in database and based on this is returning different views.
public function wrongIdSubmit($Id) {
$submits = self::$user->where('submit_id', $Id)->first();
if (!$txid) {
App::abort(404);
}
if($submits->submit_id > 3) {
return View::make('fail',[
'submits' => $submits
]);
}
else {
return View::make('submit',[
'submits' => $submits
]);
}
}
My question is how to pass this token ?_token={{ csrf_token() }} to return View::make along with $submits variable? Because like is now I've got error
production.ERROR: Illuminate\Session\TokenMismatchException
You must add the token to the form itself. You cannot pass it in the URL. Add the following to your form:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
Laravel provide function which returns you direct input hidden field with token.
csrf_field()
It will Generates an HTML hidden input field containing the value of the CSRF token.
so you can try like this:
return View::make('fail',[
'submits' => $submits,
'token' => csrf_field()
]);
and in view just print:
{!! $token !!}
or direct also like:
{!! csrf_field() !!}
out put will be like:
<input type="hidden" name="_token" value="*****" />
Best of luck..
I want to grab the value from checkboxes, and sync those value into my pivot table.
I have 3 tables :
catalog_downloads
export_frequencies
catalog_download_export_frequency (Pivot Table)
Here is what I've tried
View > My check-boxes
{{ Form::label('export_frequency' , 'Export Frequency', array('class'=> 'required cool-blue'))}} <br>
#foreach (ExportFrequency::all() as $export_frequency)
<input type="checkbox" name="{{$export_frequency->name}}" id="{{$export_frequency->id}}" value="{{$export_frequency->name}}">
{{$export_frequency->name}} <br>
#endforeach
In my Controller (CatalogDownloadController.php)
public function store()
{
$catalog_download = new CatalogDownload;
$catalog_download->title = Input::get('title');
$catalog_download->save();
foreach(ExportFrequency::all() as $export_frequency ){
$export_frequency_id = Input::get($export_frequency->name);
if(is_array($export_frequency_id))
{
$catalog_download->export_frequencies()->sync([$export_frequency_id, $catalog_download_id]);
$catalog_download_id = $catalog_download->id;
}
}
return Redirect::to('catalog_downloads/')
->with('success','The catalog_download was created succesfully!');
}
Goal
Again, I just want to sync : $export_frequency_id, $catalog_download_id
to my catalog_download_export_frequency table.
Question
Can someone tell me what I missed ? The result won't sync.
Feel free to give me suggestions/advice on this.
Thanks for your time.
This should do it mate:
// Your form view
{{ Form::label('export_frequencies' , 'Export Frequencies', array('class'=> 'required cool-blue'))}} <br />
#foreach ($exportFrequencies as $exportFrequency)
<input type="checkbox" name="export_frequencies[]" id="{{ $exportFrequency->id }}" value="{{ $exportFrequency->id }}">
{{ $exportFrequency->name }}<br />
#endforeach
// Your store method
public function store()
{
$input = Input::except('export_frequencies');
$exportFrequencies = Input::get('export_frequencies'); // Use get, not only
$catalogDownload = $this->catalogDownload->create($input); // Using dependency injection here, so don't forget to assign your CatalogDownload model to $this->catalogDownload in your contructor
if (isset($exportFrequencies)
{
$catalogDownload->exportFrequencies()->attach($exportFrequencies);
}
return Redirect::to('catalog-downloads')->with('success', 'The Catalog Download was created successfully!');
}