404 Not Found While Passing id to another Controller - Laravel - php

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>

Related

How to pass a parameter to a middleware?

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>

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

Undefined variable: errors in laravel foreach

I'm trying to retrieve data from database into my Laravel CRUD, but it says undefined variable in my view.
Here's the view frame.blade.php
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
Here's the controller
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$mhs = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame')->with('frame', $mhs);
}
And here's the route
Route::group(['prefix' => 'mahasiswa'], function () {
Route::get('/dashboard/{id}', [
'uses' => 'DashboardController#show',
'as' => 'mahasiswa.show',
]);
Route::post('/dashboard/', [
'uses' => 'DashboardController#store',
'as' => 'mahasiswa.store',
]);
Route::put('/dashboard/{id}', [
'uses' => 'DashboardController#update',
'as' => 'mahasiswa.update',
]);
Route::delete('/dashboard/{id}', [
'uses' => 'DashboardController#destroy',
'as' => 'mahasiswa.destroy',
]);
});
I keep getting "Undefined variable: mahasiswa" on my view
Does anyone know the reason for this?
You are passing a data called frame and trying to iterate over $mahasiswa, so either change your blade to this:
#foreach($frame as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
or in your controller:
public function index(Request $request)
{
$mhs = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame')->with('mahasiswa', $mhs);
}
Try this way:
First in the controller assign your DB query results to $mahasiswa:
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$mahasiswa = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame', compact('mahasiswa'));
}
Then in the view:
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
It will get rid of that error.
I used return view('frame', compact('mahasiswa')); i.e compact instead of with. I think it's a lot cleaner and less confusing for starters.
it can still work this way:
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$data['mahasiswa'] = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame',$data);
}
view
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach

Laravel modal binding gives route not found error

I'm developing simple crude application using laravel 4.2. this is my controller method for edit/update.
class ProductsController extends BaseController{
public function getEdit($id){
$product=Products::find($id);
$this->layout->content=View::make('products.edit',compact('product'));
}
}
this is the part of edit.blade.php file
{{ Form::model($product, ['route' => ['products/update', $product->id], 'method' => 'patch']) }}
I define route for the ProductsController as follows in route.php file
Route::controller ( 'products', 'ProductsController');
when i try to edit product(http://localhost:8000/products/5/edit)
it says Route [products/update] not defined.
this is my edit link
<a class="btn btn-small btn-info" href="{{ URL::to('products/' . $product->id . '/edit') }}">Edit </a>
what is the reason for this error? i have define patchUpdate() function on product contraller.
You are using a route controller, not a resourceful controller - so there are no 'named' routes.
You could do this
{{ Form::model($product, ['action' => 'ProductsController#putEdit', $product->id], 'method' => 'patch']) }}
Add following line your routes.php file
Route::model('products', 'Product');
Route::resource('products', 'ProductsController');
and also change what #The Shift Exchange has suggested
products.update not products/update
change also
<a class="btn btn-small btn-info" href="{{ URL::to('products/getEdit/'. $product->id) }}">Edit </a>

Categories