Tell me what I'm doing wrong. Here is the code that, if I insert directly into routes.php

routes.php Route::get('dashboard', 'DashboardController@index'); Route::get('wallets', 'WalletsController@index'); Route::post('addwallet', function(){ //принимаем данные от пользлвателя $wallet = Request::input('wallet', '+11111111111'); $query = DB::table('wallets')->insert( ['wallet' => $wallet]); }); 

then it works, and if I insert it into the class method, it does not work. I call the route like this: Route::get('addwallet', 'WalletsController@add');

 namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Response; class WalletsController extends Controller { public function index(){ $wallets = DB::select('select * from wallets LIMIT 20'); return view('wallets.index', ['wallets' => $wallets]); } public function add(){ //принимаем данные от пользлвателя $wallet = Request::input('wallet', '+11111111111'); $query = DB::table('wallets')->insert( ['wallet' => $wallet]); } 

Here is the error that the console displays:

 500 Internal Server Error 159ms jquery-....min.js (строка 5) "NetworkError: 500 Internal Server Error - http://exchanger.local/orders/laravel/public/addwallet" 
  • And what does he write in the logs? You are showing us a mistake from the browser console, it is useless. What is in the server logs and Laravel? - YuS
  • Thanks already solved the problem with this method public function add(Request $request){ //принимаем данные от пользователя $wallet = $request->input('wallet', '+11111111111'); $query = DB::table('wallets')->insert( ['wallet' => $wallet ]); public function add(Request $request){ //принимаем данные от пользователя $wallet = $request->input('wallet', '+11111111111'); $query = DB::table('wallets')->insert( ['wallet' => $wallet ]); - rodgers

2 answers 2

The previous version could be solved like this.

Option 1

Replace

 $wallet = Request::input(... 

On

 $wallet = \Request::input(... 

or

Option 2

Replace

 use Illuminate\Http\Request; 

on

 use \Request; 

PS In Laravel Illuminate\Http\Request used when passing the $request parameter to the controller method ( function methodName(Request $request){} ) and the record of the type Request::get('fielld') will not work

    Actually already answered, but I will duplicate:

     public function add(Request $request, Response $response){ //принимаем данные от пользлвателя $validator = Validator::make(request->only(['wallet']), [ 'wallet' => 'string', ]); $validator->validate(); $wallet = $request->input('wallet', '+11111111111') $query = DB::table('wallets')->insert( ['wallet' => $wallet]); }