I'm trying to update a column in my database with a total of votes on a project. I'm having a 404 problem but I don't see the problem in my route. (PS: Laravel 3)
This is my Vote button on every project :
{{ Form::open('project/addvote', 'VOTE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Vote') }}
{{ Form::close() }}
So when you click on the vote button it use this route :
Route::put('project/addvote', array('uses'=>'projects#addvote'));
And this is my action in the projects controller (no updating yet, just trying to redirect) :
public function put_addvote(){
return Redirect::to_route('project', $id)
->with('message', 'Vote successful');
}
Redirecting to this route :
Route::get('project/(:num)', array('as'=>'project', 'uses'=>'projects#project'));
And this is getting me the 404 error
Thanks to every response and the great help here!
Actually, Redirect::to_route expects a name of a route to redirect to it and a named route has to be declare with a name, like,
Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects#addvote'));
So, you can use it;s name to redirect to it like
return Redirect::to_route('project');
Here, project has been used as it's name using 'as' => 'project'. In, your example, you didn't gave any name to the route, here
Route::put('project/addvote', array('uses'=>'projects#addvote'));
The, as => 'route_name' is missing.
For second question, you can do it as
$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();
Inserting & Updating Models.
Update :
It was a bit confusing but after a conversation through the commenting system the answer for routing is give below :
You (OP) mentioned that you have a route declared as
Route::get('projets', array('as'=>'projets', 'uses'=>'projets#index'));
To this route you are trying to redirect using this
return Redirect::to_route('project', $id);
So, you are passing a parameter and it's not in your route declaration and this is the problem, so to overcome this change your route declaration to this
Route::get('projets/(:num)', array('as'=>'projets', 'uses'=>'projets#index'));
Or
Route::get('projets/(:any)', array('as'=>'projets', 'uses'=>'projets#index'));
Or, you can make the param optional using a ?, for example :
Route::get('projets/(:any?)', array('as'=>'projets', 'uses'=>'projets#index'));
Check wildcard Routes.
Update :
You should have postd the original code with the question, anyways, Change this
return Redirect::to_route('project', $id);
to
return Redirect::to_route('project', array($id));
Related
I am making a website for a dog shelter, and I want to make a button which would mark the dog as adopted. In other words, I am trying to update 1 column from 1 row. It will always be set from false to true.
I have tried making my own method in my existing controller, adding a route and creating a form, but I get errors that some other fields are also required, which are the same required fields when I would create a new dog (name, sex, DoB etc).
Code of the method in my controller:
public function markAsAdopted(Request $request, $id){
$pet = Pet::table('pets')->where('id', $id)->update(['is_adopted' => true]);
return redirect("/pets/$pet->id")->with('success', 'Successfully marked as adopted');
}
Code of the route:
Route::patch('/pets/{id}', 'PetsController#markAsAdopted');
Code of the form:
{!! Form::open(['action' => ['PetsController#markAsAdopted', $pet->id], 'method' => 'POST', 'class' => 'float-right ml-1 mr-1']) !!}
{{Form::hidden('_method', 'PATCH')}}
{{Form::submit('Mark as adopted', ['class' => 'btn btn-success'])}}
{!! Form::close() !!}
The form is on the page /pets/{id}
I have created all the other routes in this controller with the --resource flag when creating the controller, so the skeleton of CRUD was there.
Did I miss something? OR is there a better way of doing this?
Laravel is routing the markAsAdopted call to your update() (or possibly create() depending on how you have your Routes set) instead of the markAsAdopted() method. This is, in turn, calling your PetsRequest class (or whatever you've called your verification class).
It is most likely coming in as a POST and possibly ignoring the PATCH method, thus routed to update(). You can do a couple of things - move the patch method higher in Routes file and test by changing to POST. If this works, you know this is the problem. This is just a test - you will break the other pets POST coming in below it in the Routes file, so remember to change it back :)
I typically skip the PATCH thing altogether though, as I've run into similar issues. I normally just make a POST route with a special name (e.g. Route::post('/petsMarkAsAdopted/{id}', 'PetsController#markAsAdopted');) and open the form with the url to that special route.
If you really want to keep the PATCH, do the higher placement test above, and you can use blade to set the method after you open your form, like so:
#method('PATCH')
Edit: As noted in the comments, the above fixes the routing, but there is a different problem in your controller method now that we can access it. Take out the table(), and just call the model to get the pet your need. I've separated out the call for clarity:
$pet = Pet::where('id', $id)->first();
$pet->update(['is_adopted' => true]);
i want to set a variable to twig when redirect link like when use the render methods :
return $this->render('socialBundle::index.html.twig',array(
'id' => $id
));
it will set the Id variable to twig : {{ id }}
return $this->redirect($this->generateUrl("tuto_animaux_voir", array(
'id' => $id
)));
it will set the Id variable to the link : xxxxx.com/Id , i want that it render this variable to twig when redirect ...
sorry for this bad concept and language because i'm not england , and this is my first question in stackoverflow ,
wait for a reponse , thanks
I don't quite understand what you want to do, if you want to put a link in a twig template or just redirect from a controller according a condition. if you can give more details of what you want to do I think I can help you.
UPDATE (solution)
You could use "Forwarding to Another Controller" instead of using "Redirect" and from there to send the variables to the twig template
Based on your last comment, I came to the following conclusion.
Assume that you have index action, with route tuto_animaux_voir. And route has one parameter id. It takes id as parameter and fetches alert from the database(we have alerts on the database). Also we have entity Alert with fields id(default), title.
...
public function indexAction($id){
$alert = $this->getDoctrine()->getEntityManager("AppBundle:Alert")->find($id);
return $this->render("AppBundle:Post:index.html.twig", array(
'alert' => $alert
));
}
In your template index.html.twig you can show your alert.
...
{{ alert.title }}
...
If you want to redirect to the index action in the twig.
<a href="{{ path("tuto_animaux_voir", { 'id': alert_id })}}">Alert Title<a>
Also if you want render this action as partial.
{{ render(controller('AppBundle:Alert:index',{'id': alert_id})) }}
I think my answer would be helpful for you.
In you case 'tuto_animaux_voir' is a route associated to controller action. Given id will be available as action parameter, so you can pass it like this:
public function fooAction($id)
{
return $this->render(
'socialBundle::foo.html.twig',
['id' => $id]
)
}
You can also fetch data from database and pass them to the view.
If that's not what to you want to achieve, tell us why you want to use redirect, and what these two actions should do.
Edit:
If I understood your comment correctly, you want to implement flash messages, which are already available in symfony.
Example in docs is simple and self-explanatory.
I have a page where I can add moderators to my forum category (subreddit) and I can see a list of already existing moderators that I have assigned. Both on the same view create.blade.php
Next to each moderator, I have a delete link to allow the subreddit's owner to remove moderators as well.
However when clicking on it, give me the following error
NotFoundHttpException in RouteCollection.php line 161
Note: in order to access the route to add moderators, I use this subreddit/{id}/moderators/create
I understand my routes may be the problem here, these are the routes I'm using
Route::resource('subreddit', 'SubredditController');
Route::resource('subreddit.moderators', 'ModeratorsController');
This is the destroy() method in ModeratorsController
public function destroy(Moderator $moderator, Subreddit $subreddit)
{
$mod = Moderator::where('subreddit_id', '=', $subreddit->id)->where('user_id', '=', $moderator->id)->get();
$mod->delete();
return 'done';
}
And this is the delete link in the view
<span class="glyphicon glyphicon-remove"></span> Del
I don't think the above action() will work here because it replaces the subreddit_id with the moderator.id like this subreddit/2/moderator where 2 is the id of the moderator that has replaced 17 which was the id of the subreddit that I was adding moderators to.
I have also tried testing different routes to see if it might work, like subreddit/{id}/moderators/{modid}/delete but always got the same error.
When you are using resource, you need to need to send DELETE HTTP request.
And you are using nested resource, you need to send both $moderator->id and $subreddit->id
In your case, You need to change a tag to form with delete method
{!! Form::open(['action' => ['ModeratorsController#destroy', $moderator->id, $subreddit->id], 'method' => 'delete']) !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
And you might need to make some change to destroy function
public function destroy($subreddit_id, $moderator_id)
{
$mod = Moderator::where('subreddit_id', $subreddit_id)
->where('user_id', $moderator_id)->first();
$mod->delete();
return 'done';
}
for more information http://laravel.com/docs/5.1/controllers#restful-resource-controllers
Using 4.2 and trying to add a custom method to my controller.
My routes are:
Route::get('ticket/close_ticket/{id}', 'TicketController#close_ticket');
Route::resource('ticket', 'TicketController');
Everything CRUD wise works as it should, but at the bottom of my TicketController I have this basic function:
public function close_ticket($id) {
return "saved - closed";
}
When I am showing a link to route on my page:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
I constantly get a route not defined error, but it surely is defined...?
Any ideas where this is going wrong?
link_to_route expects a route name, not a url. This is why you are getting 'route not defined' errors, because you have not defined a route with the name you supplied to link_to_route. If you give your route a name, you can use link_to_route.
Given the following route definition, the name of the route is now 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController#close_ticket'));
The value for the 'as' key is the route name. This is the value to use in link_to_route:
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
The laravel helper method link_to_route generates an HTML link. Which mean when clicked, the user will be performing a GET request.
In your routes file, you are defining this as a POST route.
Route::post(...)
Also, take a look at the docs for link_to_route here:
http://laravel.com/docs/4.2/helpers
You'll see that the first argument should be just the route name, without the ID appended.
I'm learning laravel 3 at the moment, and I have a technical question. I have a list of authors and I'm trying to make some links to set a filter to order those authors.
I tried setting a default parameters to order by Name, it works, but I can't pass any other filters.
This is my route :
Route::get('authors', array('as'=>'authors', 'uses'=>'authors#index'));
And this is my controller function :
public function get_index($filter="name"){
return View::make('authors.index')
->with('title', 'Authors list')
->with('authors', Author::order_by($filter)->get());
}
And this is the links in my view trying to send the filter I want
{{ HTML::link_to_route('authors', 'Id', array('id')) }}
{{ HTML::link_to_route('authors', 'Name', array('name')) }}
The parameters I try to send (id and name) from the view never reach the controller so it always use default parameter.
Thank you !
You're not setting any route parameters, you must do something like:
Route::get('authors/(:any?)', array('as'=>'authors', 'uses'=>'authors#index'));
Take a look at the docs: http://three.laravel.com/docs/routing#wildcards