Laravel 4 route - laravel

I've got a problem with using URL::route. There is a public function in my controller called AuthController called delete_character, here's how it looks:
public function delete_character()
{
$player->delete();
return View::make('index')->with('danger', 'You have successfully deleted your character!');
}
Also, I've created a named route:
Route::post('delete_character', array(
'as' => 'delete_character',
'uses' => 'AuthController#delete_character'
));
All I want to do is to execute the $player->delete. I don't want it to be a site, just when I click a button it's gonna delete the player.
I've also done the button:
<td><a class="btn btn-mini btn-danger" href="{{ URL::route('delete_character') }}"><i class="icon-trash icon-white"></i> Delete</a></td>
But I constantly get MethodNotAllowedHttpException. Any hints?

In my example, I am using GET request method (POST is used when form is being submited, for instance) to capture this action.
I pass ID of client I wish to delete in the reqeust URL, which results into URL in this form: http://localhost:8888/k/public/admin/client/delete/1 (You should post it from form, according to your example/request).
Not posting whole solution for you to force you to learn! My answer is not 100% identical to your situation, but will help, for sure.
// routes.php
Route::group(['prefix' => 'admin'], function(){
Route::get('client/delete/{id}', 'Admin\\ClientController#delete');
});
// ClientController.php
<?php
namespace Admin;
use Client;
class ClientController extends BaseController
{
...
public function delete($clientId)
{
$client = Client::findOrFail($clientId);
// $client->delete();
// return Redirect::back();
}
...
}
// view file, here you generate link to 'delete' action
delete

Related

Dynamic url routing in Laravel

I am new in Laravel using version 5.8
I do not want to set route manually for every controller.
What i want is that if i give any url for example -
www.example.com/product/product/add/1/2/3
www.example.com/customer/customer/edit/1/2
www.example.com/category/category/view/1
for the above example url i want that url should be treated like
www.example.com/directoryname/controllername/methodname/can have any number of parameter
I have lots of controller in my project so i want this pattern should be automatically identified by route and i do not need to specify manually again and again Directory Name, Controller , method and number of arguments(parameter) in route.
try this:
Route::get('/product/edit/{id}',[
'uses' => 'productController#edit',
'as'=>'product.edit'
]);
Route::get('/products',[
'uses' => 'productController#index',
'as'=>'products'
]);
in the controller:
public function edit($id)
{
$product=Product::find($id);
return view('edit')->with('product',$product);
}
public function index()
{
$products=Product::all();
return view('index')->with('products',$products);
}
in the index view
#foreach($products as $product)
Edit
#endforeach
in the edit view
<p>$product->name</p>
<p>$product->price</p>

Laravel redirect after delete item

In my Laravel app, I want to delete an item and redirect immediately after that to the home page. Ideally, I want to do this purely in the routes file:
Route::get('event/{event}/delete', ['as' => 'events_delete', 'uses' => 'EventsController#destroy', function () {
return Redirect::route('events_list');
}] );
In my destroy function I have the following:
public function destroy(Event $event)
{
$event->delete();
}
The delete itself works (after refresh), but the redirect part does not seem to work. How can I redirect to the homepage immediately after the delete happened?
You either use the controller or the closure but not both, I don't think you are allowed to use them both.
But in your controller you can set the redirect like this:
public function destroy(RequestEvent $event)
{
$event->delete();
return redirect()->route('your_home_route');
}

How call route resource in Laravel

I have problem. My route definition contains:
route::resource('admin/settings/basic','admin\settings\BasicController');
but I don't know how can I call the edit action from basiccontroller in my a href link.
href='{{ link_to_route('admin/settings/basic/edit') }}'
Please give me some advice.
Given a route like the following:
app/Http/routes.php
Route::resource('profile', 'ProfileController');
Your controller could look something like this:
app/Http/Controllers/ProfileController
public function edit($id)
{
$profile = Profile::all(); // Grab some data
return view('profile.edit', [$profile]); // Pass some data to the Edit view
}
In the view, you might have a form for editing like so:
resources/views/profile/edit.blade.php
<?= Form::model($profile, ['route' => ['profile.update', $profile->id], 'method' => 'PUT', 'class' => 'form-horizontal']) ?>
That form routes to ProfileController#update
For other routes, such as an index, it is handled all for you. You just have to make sure you return the correct view in your ProfileController#index, and hitting the route for /profile will be passed through that method
You can always refer to the documentation as well - RESTful Resource Controllers

laravel soft delete using a form

Hi I am trying to soft delete and restore a user using a form, I am using a couple of packages for user auth and roles which are Zizaco Confide and Zizaco Entrust. I've added the following to the user.php model
use SoftDeletingTrait;
use ConfideUser;
use HasRole;
protected $softDelete = true;
and I've run a test as so to test this works:
Route::get('/deleteme', function(){
User::find(2)->delete();
return 'done';
});
and this updated the timestamp field, however I want to put this into my controller to neaten things up and give it a form. So I've done this in the table of users:
#if(empty($user->deleted_at))
{{Form::open(['method'=>'PATCH','action'=>
['UsersController#softDeleteUser',$user->id]])}}
<button type="submit">Suspend</button>
{{Form::close()}}
#else
{{Form::open(['method'=>'delete','action'=>
['UsersController#restoreUser',$user->id]])}}
<button type="submit">Re-activate</button>
{{Form::close()}}
#endif
and in my Controller:
public function softDeleteUser($id){
$user = User::find($id);
$user->delete();
// redirect
return Redirect::to('/admin');
}
public function restoreUser($id) {
User::find($id)->restore();
$user->save();
Redirect::to("/admin");
}
In my routes:
Route::post('/admin/user/{resource}/delete',
array('as' => 'admin.user.delete', 'uses'
=>'UsersController#softDeleteUser'));
Route::post('/admin/user/{resource}/restore',
array('as' => 'admin.user.restore',
'uses' =>'UsersController#restoreUser'));
However I get this error:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Any ideas what I'm doing wrong??
Well you've set your two forms to use the PATCH and DELETE method but your routes are set to POST (Route::post).
You can either change the routes:
Route::patch('/admin/user/{resource}/delete',
array('as' => 'admin.user.delete', 'uses'
=>'UsersController#softDeleteUser'));
Route::delete('/admin/user/{resource}/restore',
array('as' => 'admin.user.restore',
'uses' =>'UsersController#restoreUser'));
Or remove the method in your forms (it will default to POST)
{{Form::open(['action'=> ['UsersController#softDeleteUser',$user->id]])}}
<button type="submit">Suspend</button>
{{Form::close()}}
And
{{Form::open(['action'=> ['UsersController#restoreUser',$user->id]])}}
<button type="submit">Re-activate</button>
{{Form::close()}}

Laravel 4.2 form action not working

I'm new to Laravel and am building a simple web application. I'll show what code I'm using next and then I'll explain my problem.
Here's how my form starts in the view login.blade.php:
<?php
//build the form
echo Form::open(array('action' => 'AuthenticationController#authenticateUser'));
And here's what the route for the home page:
Route::get('/', function() {
return View::make('login', array('page_title' => 'Log in to MANGER'));
});
Finally, here's the authentication controller (for now it's a simple redirect to simulate login):
class AuthenticationController extends BaseController {
public function authenticateUser()
{
//retrive from database
return View::make('hr', array('page_title' => 'MANGER Login'));
}
}
My problem is, I'm getting an error on login.blade.php. saying: Route [AuthenticationController#authenticateUser] not defined. (View: /opt/lampp/htdocs/manger/app/views/login.blade.php)
How is the error about a route when I've defined a controller instead? And also, how can this be fixed? Please excuse any noob errors and thanks a lot in advance! :-)
You still need to define your route like this:
Route::post('authenticate', ['uses' => 'AuthenticationController#authenticateUser']);
Otherwise it won't know what method to use, or the url to create.

Resources