How to pass a parameter to a middleware? - php

I have this route in my routes/web.php:
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkoutIndex')->middleware('auth');
My CheckoutController:
function index()
{
if (Cart::instance('default')->count() == 0) {
return redirect()->route('cartIndex', App::getLocale())->withErrors('Your shopping cart is empty! Please select an item to checkout.');
}
$discount = session()->has('coupon') ? session()->get('coupon')['discount'] : 0;
return view('checkout_index')->with(['lang' => App::getLocale(), 'discount' => $discount]);
}
When I'm not logged in and go to the URL it takes me to a login page as it should. but the login page gives a Missing required parameter for [Route: login] [URI: {lang}/login] [Missing parameter: lang]. error.
The login page works well in its own route anywhere else in the app, I only get the error by clicking on this one specific link:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
I believe having middleware is causing the issue.
I'm using the laravel built-in auth. the route to login is Auth::routes();
Any ideas?

could you try this
in your App\Http\Middleware\Authenticate middleware
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
You need to pass the required parameter to the route:
return route('login', ['lang' => App::getLocale()])

change this line:
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', App::getLocale()) }}">{{__('Proceed to Checkout')}}</a>
to
<a class="btn btn-primary text-light p-3 rounded-0" href="{{ route('checkoutIndex', ['lang' => App::getLocale()) ] }}">{{__('Proceed to Checkout')}}</a>

Related

Passing Parameter into Create in Laravel

I tried to passsing Parameter to 'App\Http\Controllers\StoreController#create', but Laravel return error 404
i tried this on view:
<div class="text-end">
<?php $id = $i->id; ?>
<a href="{{ url('/store/create/' . $id) }}"
class="btn btn-primary text-end">Checkout</a>
</div>
web.php:
Route::get('/store/create/{$id}','App\Http\Controllers\StoreController#create');
What did i do wrong?
Just Removing the dollar sign from the route. Route::get('/store/create/{id}'. Here we cann't use the dolla sign in routes w

routes/web route me to another route

My web doesn't seem to be directing to the correct page.
Here is my blade
<div class="container">
<div class="col-lg-12 d-flex justify-content-between align-items-center">
<h2>Informations</h2>
<a href="{{ route('add-new-information') }}" class="btn text-success">
<i class="fas fa-plus-circle fa-2x"></i>
</a>
</div>
<hr>
<div class="row d-flex justify-content-center align-items-center mt-5" style="max-height: 500px !important; overflow-y: scroll">
#foreach ($informations as $info)
<div class="card col-sm-11 p-0 mb-4 clickable-item" onclick='window.location = "{{ route('admin-informations', ['id' => $info->id]) }}"'>
...
</div>
#endforeach
</div>
</div>
Here is my routes/web
Auth::routes();
Route::group(['middleware' => ['auth'], 'prefix' => '/',], function () {
Route::get('/', function () {
return redirect('/home');
});
Route::group(['prefix' => 'admin'], function() {
Route::get('/informations', [App\Http\Controllers\InformationController::class, 'index'])->name('informations');
Route::get('/informations/{id}', [App\Http\Controllers\InformationController::class, 'indexAdminInfo'])->name('admin-informations');
Route::get('/informations/add-new-information', [App\Http\Controllers\InformationController::class, 'add'])->name('add-new-information');
});
});
and here is my controller
public function indexAdminInfo($id){
$information = Information::find($id);
// $comments = Comment::where('information_id', $id)->orderByDesc('created_at')->get();
$ack_count = Acknowledge::where('information_id', $id)->count();
$user_ack = Acknowledge::where([
['information_id', '=', $id],
['user_id', '=', Auth::user()->id],
])->first();
$ack = 'FALSE';
if($user_ack != null){
$ack = 'TRUE';
}
return view('adminviews/infoadmin', compact('information', 'ack_count', 'ack', 'user_ack'));
}
public function add(){
return view('adminviews/addinfo');
}
For some reason, when I click the a tag with the href {{ route('add-new-information') }} to go to the add page 'adminviews/addinfo',
instead the page will go to the 'adminviews/infoadmin' page, which will cause an error, because no parameters are being sent.
I tried checking the code, but it looks correct to me. Can anybody find an error on this?
the problem is with your routes:
these two routes are ambiguous:
Route::get('/informations/{id}');
Route::get('/informations/add-new-information');
just think of below scenario:
router wants to route, this url : /information/add-new-information
router will hit the first defined route, because it is compatible with the definition ('/informations/{id}')
Note :{id} is a variable and can be any string
so it will go with this.
Solution
write the more restricted route first,
and more general route later:
Route::get('/informations/add-new-information');
Route::get('/informations/{id}');

Post not deleting, server error 500 in laravel 7

Please what am I doing wrong, Paymentdetails refuse to delete instead, its loading server error.There's something wrong with my code.
This is my controller
public function destroy(Paymentdetail $paymentdetail)
{
// dd($id);
if(Auth::user()->role_id == '1'){
$this->paymentdetails->delete($paymentdetail->id);
event(new Deleted($paymentdetail));
return redirect()->route('paymentdetails.index')
->withSuccess(__('Payment details deleted successfully.'));
}
else{
return redirect()->back()
->withErrors(__('Sorry! You Are Not Authorized To Delete Payment Details.'));
}
}
index.blade
<a href="{{ route('paymentdetails.edit', $paymentdetail) }}"
class="btn btn-icon edit"
title="#lang('Edit Paymentdetail')"
data-toggle="tooltip" data-placement="top">
<i class="fas fa-edit"></i>
</a>
<a href="{{ route('paymentdetails.destroy', $paymentdetail) }}"
class="btn btn-icon"
title="#lang('Delete Paymentdetail')"
data-toggle="tooltip"
data-placement="top"
data-method="DELETE"
data-confirm-title="#lang('Please Confirm')"
data-confirm-text="#lang('Are you sure that you want to delete this payment details?')"
data-confirm-delete="#lang('Yes, delete details!')">
<i class="fas fa-trash"></i>
</a></a>
Moved my answer from comments"
$this->paymentdetails->delete($paymentdetail->id);
seems over-engineered and confusing
$paymentdetail is instance of Paymentdetail and it extends Laravel Model. As it is instance of model - it knows how to find itself in DB therefore you can call
$paymentdetail->delete();
and instance will delete itself
First check your request method,it should be Get method.
iF method is Get then is it redirecting to given function?
You should on debug mod of laravel which gives you proper error.
Another thing check is user authenticated?

Missing required parameters for [Route: havence.automail.edit] [URI: havence/automail/{automail}/edit]

I create edit page to edit the form. When I debug it. It's showing error which is Missing required parameters. I already try many ways but i can't solve it. Anyone can help on this?
<td class="text-right">
<a href='{{ route("email.edit",["id"=>$mailTemplate->id]) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
route file
Route::get('api/email/create', ['as' => 'email.create', 'uses' => 'Havence\AutoMailController#create']);
Route::get('automail/mail', 'Havence\AutoMailController#mail');
Route::get('automail/index',['as'=>'email.index','uses' => 'Havence\AutoMailController#index']);
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Route::get('automail/delete',['as'=>'email.delete','uses' => 'Havence\AutoMailController#destroy']);
Controller
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit')->with('mailTemplates', $mailTemplates);
}
You could do the following:
<td class="text-right">
<a href='{{ route("email.edit", $mailTemplate) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
The route:
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Then in the controller you can get it:
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit', compact('mailTemplates'));
}

404 Not Found While Passing id to another Controller - Laravel

I don't know why I'm getting 404 not found error when I'm trying to pass an id to another controller.
here's my index.blade.php
<a class="btn btn-sm btn-default" href="{{ route('receiving_details',
['id'=>$r_main->id])}}" title="Show Received Data"><i class="fa fa-arrow
right"</i></a>
web.php
Route::get('receiving_details/{$id}',[
"uses" => 'ReceivingDetailsController#index',
"as" => 'receiving_details'
]);
ReceivingDetailsController.blade.php(This is where I want to pass id from view.blade.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ReceivingDetailsController extends Controller
{
public function index($id){
echo $id;
}
}
Just remove $ from your get request.
Route::get('receiving_details/{id}',[
"uses" => 'ReceivingDetailsController#index',
"as" => 'receiving_details'
]);
In your href please have this little adjustment
<a class="btn btn-sm btn-default" href="{{ route('receiving_details', $r_main->id)}}" title="Show Received Data"><i class="fa fa-arrow right"</i></a>

Categories