Laravel modal binding gives route not found error - php

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>

Related

Is there a way of getting authenticated user ID and use it on navigation.blade?

I'm building a new Laravel app, using Breeze.
What i'm trying to do is get the authenticated user id to redirect it to the profile route, which is:
Route::group([
'prefix' => 'profile',
'as' => 'profile.',
'middleware' => ['auth']
], function () {
Route::get('/{id}', [ProfileController::class, 'show'])->name('profile');
}
);
But, on the layout of navigation, as shown below, i can't manage to get it on the :href.
I already tried some approaches, like:
:href="{{ route('profile', [Auth::user()->id]) }}"
:href="{{ route('profile',/([Auth::user()->id])) }}"
But none of them seems to work.
The navigation.blade.php dropdown part:
<x-slot name="content">
<x-dropdown-link :href="route('admin.aprovar')">
{{ __('Aprovações') }}
</x-dropdown-link>
<x-dropdown-link>
{{ __('Perfil') }}
</x-dropdown-link>
<!-- Authentication -->
<form method="POST" action="{{ route('logout') }}">
#csrf
<x-dropdown-link :href="route('logout')"
onclick="event.preventDefault();
this.closest('form').submit();">
{{ __('Log Out') }}
</x-dropdown-link>
</form>
</x-slot>
Any help or hint would be appreciated.
Thanks for your time!
Credits to #aynber to answering, what i needed to do was:
{{ route('profile', ['id' => Auth::user()->id]) }}

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>

Laravel 5.2 - Delete from db

I am using Laravel Framework version 5.2.45.
I have created a simple view that outputs my todos:
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
Within my routes I have created the following route to delete a todo:
Route::get('/todo/delete/{id}', [
'uses' => 'TodosController#delete',
'as' => 'todo.delete'
]);
Within my TodosController I created the following delete method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Todo;
class TodosController extends Controller
{
public function delete($id) {
$todo = Todo::find($id);
$todo->delete();
return redirect()->back();
}
// ...
When I press the button in the frontend nothing happens. I do not get any error...
Any suggestions what is wrong with my implementation?
Appreciate your replies!
You are using button not tag
turn your code from
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
to
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
Try Below code, You have used a button instead of a tag
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
You should do like this :
Delete Button :
<a class="btn btn-primary" href="{{ route('todo.delete',$todo->id) }}">Delete</a>
And delete function look like below :
public function delete($id) {
try {
$delete_flag = Todo::where(['id' => $id])->first();
$delete_flag->delete();
return redirect()->back()->with('success', 'Todo deleted successfully');
} catch (Exception $ex) {
return redirect()->back()->with('error', 'Something went wrong');
}
}
#foreach($todos as $todo)
{{ $todo->todo }} x
#endforeach
delete code--
$toDo = Todo::findOrFail($id)->delete();
if($toDo){
return response()->josn(['message'=>'deleted']);
}

Trying to delete record by CRUD route::delete method(Laravel 5.3)

I have a problem with Larave's framework program with deleting by CRUDS method DELETE.
My route method is:
Route::delete('cats/{cat}/delete', function(Furbook\Cat $cat){
$cat->delete(); return redirect('cats.index')
->withSuccess('Cat
has been deleted.'); });
My view with delete url:
#extends('layouts.master')
#section('header')
Back to the overview
<h2>
{{ $cat->name }}
</h2>
<a href="{{ url('cats/'.$cat->id.'/edit') }}">
<span class = "glyphicon glyphicon-edit"></span>
Edit
</a>
<a href ="{{ url('cats/'.$cat->id.'/delete') }}">
<span class ="glyphicon glyphicon-trash"></span>
Delete
</a>
<p>Last edited: {{ $cat->updated_at }}</p>
#endsection
#section('content')
<p>Date of Birth: {{ $cat->date_of_birth }} </p>
<p>
#if ($cat->breed)
Breed:
{{ url('cats/breeds/'.$cat->breed->name) }}
#endif
</p>
#endsection
My Cat model:
<?php
namespace Furbook;
use Illuminate\Database\Eloquent\Model;
class Cat extends Model {
// We specified the fields that are fillable in the Cat model beforehand
protected $fillable = ['name','date_of_birth','breed_id'];
// informacja o tym, żeby nie uaktualniać update_at w tabeli kotów
public $timestamps = false;
public function breed(){
return $this->belongsTo('Furbook\Breed');
}
}
?>
When I'm clicking on delete link, there is an error like this:
MethodNotAllowedHttpException in RouteCollection.php line 233:
I don't know what's wrong. Could you somoene help me with solving problem?
Could someone help me with this problem?
I would be very grateful, greetings.
This is to do with the Request you are making. You must either create form with the delete method like so
<form action="{{ url('cats/'.$cat->id.'/delete') }}" method="DELETE">
<button class ="glyphicon glyphicon-trash">Delete</button>
</form>
OR change your route to a get
Route::get('cats/{cat}/delete', function(Furbook\Cat $cat){
$cat->delete();
return redirect('cats.index')->withSuccess('Cat has been deleted.');
});
If you go the form route don't for get to add the {{ csrf_field() }}
https://laravel.com/docs/5.4/csrf
Using Route::delete(), you cannot place it in an anchor. Make a form with DELETE method.
{!! Form::model($cat, ['method' => 'DELETE', 'url' => 'cats/'.$cat->id.'/delete']) !!}
<button type="submit">Delete</a>
{!! Form::close() !!}

How to define a route for a link to go to destroy() function in Laravel 4?

I defined a resource in my routes like this:
Route::resource('shops', 'shopsController');
I need to create a link to delete an item
<a class="btn btn-xs btn-danger" href="{{ URL::to('shops/'. $row->id . '/destroy') }}" > </a>
but this URL requires me to define a new route like this:
Route::get('shops/{id}/destroy', 'shopsController#destroy');
So, how can I enforce the URL to go through the default route, through its resource, to get the destroy function??
I tried
href="{{ route('shops.destroy', $row->id ) }}" data-method="delete"
but I redirects me to show() instead!!!!
You can't delete the user via anchor tag href attr you need to delete it either with form or use Ajax.
{{ Form::open(array('url' => 'shops/'. $row->id)) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this User', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
Here's the
similar question
Update
Hope this would help you.
You can pass $row->id to the anchor Id attr like so
<a class="btn btn-xs btn-danger" id="{{$row->Id}}" onclick="delete_user(this.id) return false;" href="#" rel="nofollow" >Delete this entry</a>
and then use the below script to work with Destory method.
function delete_user(Id) {
var result = confirm("Want to delete?");
if (result==true) {
$.ajax({
url:'http://localhost:81/laravel/myapps/public/shops/'+Id,
type:"Post",
data: {'_method':'delete'},
success:function($msg){
alert($msg);
}
});
}
}
</script>
Try this
<a class="btn btn-xs btn-danger" href="{{ route('shops.destroy',array($row->id)) }}" data-method="delete" rel="nofollow" data-confirm="Are you sure you want to delete this?">Delete this entry</a>
laravel-delete

Categories