Bonjour, j'aimerai savoir si quelqu'un peut m'aider sur une requete HTTP POST vers une route laravel. Cette route resemble a:
Route::POST('/update/{'code'}, function($code, Request request){
$objet = Objet::find(1);
$objet->code = $code;
$objet->save();
}
Plutot simple mais je n'arrive pas a faire de test avec postman par exemple
#HTTP POST request problem to update route
41 messages · Page 1 of 1 (latest)
English?
Sorry,
I need help with a HTTP POST request. This route must recieve a Key with value to modify an entry into my db.
I'm alleady able to make a get and recieve what i want so, i'm pretty sure my setting are correct but i can't update my entry with software like postman.
The single quotes in {'code'} looks odd, and Objet:: is spelled incorrectly. Is this the problem? If not, show us the actual code.
Or OK, I guess Objet is your model.
But there are also missing $, I guess you re-typed the code?
Do you get an error?
Route::POST('/update/{'code'}, function($code, Request request){
$code = Code::find(1);
$code->code = $code;
$code->save();
}
Is that the actual code? It has syntax errors
You should get error messages if that is the case
I have 404 error when i try to post request
what should i modify?
Actual code Controller:
<?php
namespace App\Http\Controllers;
use App\Models\Code;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class CodeController extends Controller
{
public function update($newCode, Request $request)
{
$code = Code::findOrFail(1);
$code->code_recu = $code->code_recu + $request->input($newCode);
$code->save();
return response()->json(['message' => 'Code updated successfully']);
}
}
Actual web.php:
<?php
use App\Models\Code;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CodeController;
Route::get('/', function () {
$codes = Code::all();
return response()->json($codes);
});
Route::post('/update/{new_code}', [CodeController::class, 'update'])->name('edit');
Actual Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Code extends Model
{
use HasFactory;
protected $fillable =[
'code',
'code_recu',
'reset'
];
}
HTTP POST request problem to update route
And there is a Code model with id 1?
actually yes
The 404 page doesn't look like it's generated by Laravel, but by Apache. Are other routes working?
GET route is working well
log in apache recieve GET and POST but post return 404 (actually normal because there is nothing to return ) it just have to update DB
but nothing happen in DB
Check the order of parameters, If your controller method is also expecting input from a route parameter, list your route arguments after your other dependencies.
404 means route or model not found, it does not mean "nothing to return"
update(Request $request, $newCode)
I don't see that causing 404, but the docs is clear
Check that you haven't cached routes, php artisan route:clear
actually a made a controller but this is really basic so i can put the function into the web.php
controller don't need any parameter from Request object because the parameter is into the url
i clear the route cache and invert the argument... still 404
how you creating and editing code? basically in case of api we use postman. this is a simple crud try creating a page for get laravel error log's.
You are almost right. but bro @serene nebula pass {new_code} slug in url. so it will update(Request $request, $new_code) in controller. not update(Request $request, $newCode).
I don’t think the names need to match, unless route model binding is used
maybe, it will work i am intermediate not proffesional. i am unable to find any plateform for learning laravel expert. actually it takes time when you do a new project knowledge will update with time. Thanks
ok then, it was a problem into the apache2 configuration that never root to the laravel route
but now a have a419 problem -_-' i want to disable CSRF token (app in domestic network)
Thanks for all, problem was: in the apache configuration, i forget to configure
<VirtualHost *:80>
DocumentRoot /var/www/nom_de_votre_projet/public
ServerName nom_de_votre_projet
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/nom_de_votre_projet/public> #from here
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory> #to here
</VirtualHost>
and disable CSRF token, due to private network this is not really usefull and avoid some problem like code 419
good
Rien à voir mais tu devrais utiliser des noms de variables en anglais pour éviter toute confusion 👍