Error 404 on edit rows - Backpack crud- Laravel - php

I'm actually inserting data without problems on my DB...but when I try to edit one row and save the changes... it throws me this error...
"No query results for model [model's name]"
I already try to fix that with => protected $primaryKey = 'id'; On my model.
But still getting the same error ....any help ?
The specific error route looks like
"admin/titulares/2"
and before I press the save button like "admin/titulares/2/edit"
My routes
Route::group([
'middleware' => ['prefix' => config('backpack.base.route_prefix', 'admin'),
'web', config('backpack.base.middleware_key', 'admin')],
'namespace' => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
Route::get('dashboard', 'dashboardController#dashboard');
#CRUD::resource('equipos', 'EquiposCrudController');
#CRUD::resource('regiones', 'RegionesCrudController');
CRUD::resource('parametros', 'ParametrosCrudController');
CRUD::resource('estaciones', 'EstacionesCrudController');
#CRUD::resource('redes', 'RedesCrudController');
#CRUD::resource('huso', 'HusoCrudController');
CRUD::resource('titulares', 'TitularesCrudController');
CRUD::resource('operadores', 'OperadoresCrudController');
});
UPDATE:
This is my edit form action (it uses https://laravelcollective.com/docs/5.0/html)
{!! Form::open(array('url' => $crud->route.'/'.$entry->getKey(), 'method' => 'put', 'files'=>$crud->hasUploadFields('update', $entry->getKey()))) !!}

Are you sure using right method for update, You should use put for update data, for example,
Route::put('example/{id}', 'ExampleController#update')
I hope this will help

Related

How to resolve conflict between routes in Laravel

I am trying to have two separate fuctions on my application. One to complete a task and the other to delete but I am getting an error and the form doesn't load: Route [task.delete] not defined. How can I resolve the conflict between the routes? The form only loads when I take the second route out. UPDATE: The delete function now acts the same as the complete function.
Route::patch('/task/{task}',['uses' => 'TaskController#complete', 'as'=> 'task.complete']);
Route::delete('/task/{task}',['uses' => 'TaskController#delete', 'as'=> 'task.delete']);
Controller:
public function delete(Task $task) { $task->delete(); session()->flash('status', 'Task Deleted!'); return redirect('/profile/' . auth()->user()->id); }
In laravel world you should use the delete request type if you try to remove
something from database
so it will be
Route::delete('/task/{task}',['uses' => 'TaskController#delete', 'as'=> 'task.delete']);
Route::patch('/task/{task}',['uses' => 'TaskController#complete', 'as'=> 'task.complete']);
you can read more about it in the Basic Routing section
Use a different verb. You can have multiple routes matching the same pattern as long as they use different methods.
Route::delete('/task/{task}',['uses' => 'TaskController#delete', 'as'=> 'task.delete']);
Route::post('/task/{task}',['uses' => 'TaskController#complete', 'as'=> 'task.complete']);
// or `Route::patch()`, both are valid.
Try to change
Route::patch('/task/{task}',['uses' => 'TaskController#delete', 'as'=> 'task.delete']);
to
Route::delete('/task/{task}',['uses' => 'TaskController#delete', 'as'=> 'task.delete']);

Laravel - Route pattern cannot reference variable name more than once

I am working on a legacy code, a project built with Laravel 5.2, and I am getting an error:
Route pattern "/api/v0/taxonomy/{term}/{{term}}" cannot reference variable name "term" more than once.
For this route:
/post/106
This is are my routes:
Route::group(['prefix' => 'api'], function() {
Route::group(['prefix' => 'v0'], function () {
Route::get('route/{a?}/{b?}/{c?}/{d?}', 'DynamicRouteController#resolve');
Route::get('id/{id}', 'DynamicRouteController#resolveId');
Route::get('search', 'SearchController#search');
Route::resource('taxonomy/{term}','TaxonomyController');
});
});
Not sure, why am I getting this error?
When you define a route as a resource then Laravel seems to create all the routes necessary for your resource: GET, POST, PATCH, DELETE.
So you would just need to define Route::resource('taxonomy','TaxonomyController'); or Route::resource('taxonomy.post','TaxonomyPostController');
Check docs
You just need rename your route parameter like this:
Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
=> ['{term}' => 'your_name']]);
be careful!!!!. you need insert bracket. this will cause wrong result:
Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
=> ['term' => 'your_name']] );
attention:
'your_name' should not be same as your parameter so this method make wrong
result.
Route::resource('taxonomy/{term}','TaxonomyController', ['parameters'
=> ['{term}' => 'term']});

Laravel ID obfuscating (Meaningless Numbers to Meaningful Title)

I am using a Laravel jenssengers MongoDB and it is my first time seeing the word obfuscating. I see many open source options on github but nothing that does what i want.
How do i obfuscate my url from looking like this
http://blog.dev:8000/article/57780ee99a892008f64d5341
To looking like
http://blog.dev:8000/article/how-to-do-this-article
My routes expect an article id like so
Route::get('/article/{article_id}', [
'uses' => 'MainController#singleArticle',
'as' => 'article'
]);
...And Controller
public function singleArticle($article_id){
$article = Article::find($article_id);
return view('article',['article' => $article]);
}
And i am assuming it is quicker working with ids rather than text.(Correct me if i am wrong) Therefore changing the route to ...
Route::get('/article/{article_title}', [
'uses' => 'MainController#singleArticle',
'as' => 'article'
]);
and controller to
public function singleArticle($article_title){
$article = Article::where('title',$article_title)->first();
return view('article',['article' => $article]);
}
Would be a bad choice that i might regret later when two articles have the same title?
So how do i obfuscate my routes, to look like the title of the article which is being fetched from the database?

Route before getting executed after the route

In my routes.php I have the following group:
Route::group(array('before' => 'checkSomething', 'prefix' => '{lang}'), function() {
echo "1";
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#home'));
});
And the filter attached to it:
Route::filter('checkSomething', function(){
echo "2";
if(!somethingNotRight($something)){
return Redirect::route('index', array()));
}
});
My question is, why is the route inside the route group getting called first, and after that the filter? If I execute this code, I get the following result:
21
But what I want is:
12
Pretty simple isn't it? You are aplying your filter BEFORE the app hits the controller of the route, if you want to apply filter after just change:
Route::group(array('after' => 'checkSomething', 'prefix' => '{lang}')
The code inside the Route::group closure isn't called at the moment the route is executed. It's called very early to register all the routes. The filter is working correctly, but to test that you'd need to put the echo inside the controller

Laravel 4: Multi-Page Form on the same route?

Hello folks I am stuck.
I want to register a User in Laravel 4. Now the thing is, that I want to first grab the email and password and save them in the database. And in step 2 of the registration process, I want to grab all the other details like first and last name and so on.
The difficulty is, that everything should be under one route called signup, for example everything under http://example.org/signup
Another difficulty is, that I have to access the same route with the same methods (GET & POST) twice, because I once get and post the form for Email and Password, and then I get and post the First, Last and Company Name into the Database.
I came up with the following solution, to store everything into the session, because through the session I can access the variables. So whenever I access my UserController I check, if there is data in the session and if yes, redirect to form 2.
Here are all my files:
http://help.laravel.io/d4104cae42f9a2efe1466ce53d086826bc9f6d7f
My Get-Method from the UserController:
public function create()
{
if(Session::has('email')) {
return View::make('frontend.signup.step2');
}
else {
return View::make('frontend.signup.step1');
}
}
My Post-Method from the UserController:
public function store()
{
// If User has a email and password in the session from the first create-View
// his data should be stored and then he gets redirected to a new create-View
Session::flush();
Session::put('email', Input::get('email'));
Session::put('password', Input::get('password'));
if (Session::has('email')) {
try
{
// Let's register a user.
$user = Sentry::register(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
));
// Let's get the activation code
$activationCode = $user->getActivationCode();
// Send activation code to the user so he can activate the account
// Save Email in Emaillist
Email::create(array(
'email' => Session::get('email')
));
// Redirect
return Redirect::action('UserController#create');
}
return Redirect::route('signup');
}
else {
return 'No Session here';
}
}
Here are my routes:
Route::get('signup', array('as' => 'signup', 'uses' => 'UserController#create'));
Route::post('signup', array('as' => 'signup', 'uses' => 'UserController#store'));
For some reason I believe that it gets unneccessary complicated and I believe that there must be another more simple and intuitiv way to solve this, instead with if statements and redirects to the same controller-method.
Nonetheless I came up with some other solutions, for example just using the "signup" as prefix, but I don't like it that way.
Route::group(array('prefix' => 'signup'), function()
{
Route::get('/', function(){
return 'Yeab bababy yea';
});
Route::get('step1', array('as' => 'signup.step1', 'uses' => 'UserController#getStep1'));
Route::post('step1', array('as' => 'signup.step1', 'uses' => 'UserController#postStep1'));
Route::get('step2', array('as' => 'signup.step2', 'uses' => 'UserController#postStep2'));
Route::post('step2', array('as' => 'signup.step2', 'uses' => 'UserController#postStep2'));
});
Is there any way of accomplishing the task while only using one route and without using clientside Javascript to store the variables in the database? (I am a unexperienced with ajax)
The Goal should be to catch the email and still stay on the same route, like those smart guys here for example:
https://www.crazyegg.com/signup
I hope there is a way. Thank you for your help Internet.
Kind regards,
George
P.S.
It's 1 am here in Germany, so don't be mad if I don't respond the next couple of hours to comments, because I am going to sleep now. Thank you very much.

Categories