In api.php I've described some routes. GET method works. Can't tell the same about POST method.
<?php
use Illuminate\Http\Request;
use App\UserUnfo;
Route::middleware('auth:api')->get('/user', function (Request $request)
{
return $request->user();
});
Route::get('/person', function() {
$person = [
'ip' => '127.0.0.1',
'name' => 'me'
];
return $person;
});
Route::post('/person', function(Request $request) {
$userInfo = UserInfo::create([
'name' => $request->input('name'),
'ip' => $request->input('ip')
]);
return $userInfo;
});
In web.php
Route::get('/home', 'HomeController#index')->name('home');
The error I've got
Class 'UserInfo' not found
You're using the wrong model it's spell mistake.
use App\UserUnfo;
To
use App\UserInfo;
Related
Route::get('/', function () {
return view('posts', [
'posts' => User::latest()->get()
]);
});
I can't use latest, because it says method not found.
Try this
use App\Models\User;
Route::get('/', function () {
return view('posts', [
'posts' => User::latest()->get();
]);
});
Make sure you use the User model correctly, like this:
use App\Models\User;
Route::get('/', function () {
return view('posts', [
'posts' => User::latest()->get();
]);
});
I have route api.php like so
Route::group(['middleware' => ['jwt.verify']], function() {
Route::get('logout', [ApiController::class, 'logout']);
Route::get('pengguna', [ApiController::class, 'getAuthenticatedUser']);
Route::get('get_kabupaten/{provid}', [KabupatenController::class, 'get_kabupaten']);
Route::get('get_kecamatan/{kabid}', [KecamatanController::class, 'get_kecamatan']);
Route::get('get_kelurahan/{kecid}', [DesaController::class, 'get_kelurahan']);
Route::get('get_bencana', [JenisbencanaController::class, 'get_bencana']);
Route::get('saport', [ReportController::class, 'get_laporan']);
Route::post('saport/create', [ReportController::class, 'store']);
Route::get('saport/{id}', [ReportController::class, 'show']);
// Route::get('laporan', [LaporanController::class, 'get_laporan']);
// Route::post('laporan/create', [LaporanController::class, 'store']);
// Route::get('laporan/{id}', [LaporanController::class, 'show']);
// Route::put('update/{product}', [LaporanController::class, 'update']);
// Route::delete('delete/{product}', [LaporanController::class, 'destroy']);
});
all everything else work except
Route::get('pengguna', [ApiController::class, 'getAuthenticatedUser']);
Route::get('saport', [ReportController::class, 'get_laporan']);
Route::post('saport/create', [ReportController::class, 'store']);
Route::get('saport/{id}', [ReportController::class, 'show']);
and this my reportcontroller
<?php
namespace App\Http\Controllers;
use App\Models\Laporan;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Validator;
use Auth;
class ReportController extends Controller
{
public function get_laporan()
{
$laporan=Laporan::where('user_id', Auth::id())->get();;
return response()->json(compact('laporan'));
}
public function store(Request $request)
{
$data = $request->only( 'desa_id','bencana_id','koordinat','korban_kk','korban_orang','sebab_bencana');
$validator = Validator::make($data, [
'desa_id' => 'required',
'bencana_id' => 'required',
'koordinat' => 'required',
'korban_kk' => 'required',
'korban_orang' => 'required',
'sebab_bencana' => 'required',
]);
//Send failed response if request is not valid
if ($validator->fails()) {
return response()->json(['error' => $validator->messages()], 200);
}
$laporan = new Laporan;
$laporan->bencana_id = $request->bencana_id;
$laporan->desa_id= $request->desa_id;
$laporan->koordinat= $request->koordinat;
$laporan->user_id= Auth::id();
$laporan->korban_kk= $request->korban_kk;
$laporan->korban_orang= $request->korban_orang;
$laporan->sebab_bencana= $request->sebab_bencana;
$laporan->bantuan_diperlukan= $request->bantuan_diperlukan;
$laporan->respon_instansi= $request->respon_instansi;
$laporan->lokasi_pengungsian= $request->lokasi_pengungsian;
$laporan->pengungsi_kk= $request->pengungsi_kk;
$laporan->pengungsi_orang= $request->pengungsi_orang;
$laporan->permintaan_bantuan= $request->permintaan_bantuan;
$laporan->save();
return response()->json([
'success' => true,
'message' => 'laporan telah di input',
'data' => $laporan
], Response::HTTP_OK);
}
public function show($id)
{
$laporan=Laporan::where('user_id', Auth::id())->find($id);;
if (!$laporan) {
return response()->json([
'success' => false,
'message' => 'Sorry, Laporan not found.'
], 400);
}
return response()->json(compact('laporan'));
}
}
i have rename int from laporan to report to saport, create new controller, and make it HTTPS for saport always return 400.
as far as i know the structure of controler the same with other controller, couse i use meke:controller, and why it return 400 400 Bad Request.
and i make new basic route, and it return 400
Route::get('saport', function () {
return 'Hello World';
});
cahche:clear
route:clear
dumptautoload
trying remove saport from outside jwt,verify
all return 400
i have try all.
btw in local it run like charm, it case only in sharehosting.
i think it postman issue, not laravel. but if any solve for it issue please tell me
When I use middleware like below, no problem:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
But if I try to use it with apiResources, like below:
Route::apiResources([
'user' => 'API\UserController',
'posts' => 'API\PostController'
])->middleware('auth:api');
Then I get an error message:
php artisan route:list
BadMethodCallException
Method Illuminate\Routing\RouteRegistrar::apiResources does not
exist.
What is the correct way to call ApiResource with middleware in routes/api.php ?
I don't believe you can add any middleware's to the apiResources, what you could do instead is nest them inside a route group that applies them
Route::group(['middleware' => 'auth:api'], function () {
Route::apiResources([
'user' => 'API\UserController',
'posts' => 'API\PostController'
]);
});
This would also allow you to shorten the controller definitions with the namespace option:
Route::group(['middleware' => 'auth:api', 'namespace' => 'API'], function () {
Route::apiResources([
'user' => 'UserController',
'posts' => 'PostController'
]);
});
I can't understand what is happening here. on my local server its working fine but when I put everything on my live server I'm having this error
My web Route
Auth::routes(['verify' => true]);
Route::get('/', function(){
return view('auth.login');
})->name('auth.login')->middleware('auth');
Route::resource('/register', 'Auth\registerController#index')->name('register');
Route::group( ['middleware' => 'auth' ], function()
{
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['namespace' => 'dashboard', 'prefix' => 'dashboard'], function() {
Route::get('/', 'DashboardController#index');
});
SO ON...
}
My RegisterController
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
public function index(Request $request)
{
$referral = '';
$keyword = $request->get('search');
$referral = Referral::where([
['code', $keyword],
['status', 0]
])->first();
if (is_null($keyword))
return view ( 'Auth.register');
elseif ($referral)
return view ( 'Auth.register', compact('referral', $referral))
->withDetails ( $referral )
->withQuery ( $keyword );
else
return view ( 'Auth.register')->withMessage ( 'The code you provided is not existing or already been taken.' );
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'country_code' => $data['country_code'],
'phone_number' => $data['phone_number'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
registerController Path
what do you this causing this?
Thank you in advance!
this line of code in your web.php file is causing the issue
Route::resource('/register', 'Auth\registerController#index')->name('register');
your class name starts with R but you have written using r. moreover it seems the route is a simple get route but you have written it as resource. change this too. so the route should be like
Route::get('register', 'Auth\RegisterController#index')->name('register');
How did you namespace your registerController?
Because laravel later versions use a rather different namespace than previous versions. Hence change your namespace (and imported files) to this;
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
If you are using laravel 8:
use App\Http\Controllers\Auth\RegisterController;
Auth::routes(['register' => false]);
Route::get('/user_register', [RegisterController::class, 'showRegistrationForm'])->name('register');
However it throws this error when I clean cache:
Unable to prepare route [user_register] for serialization. Another route has already been assigned name [register].
The new route works, but the old route yet it works
i have problem in submitting this form it produces NotFoundHttpException
{{Form::open(array('parsley-validate'=>'','url'=>url('dashboard/send_message')))}}
this is the function the form should execute in dashHome.php in dashboard folder
public function postSendMessage()
{
$validator = Validator::make(Input::all(), array(
'studentId' => 'required|exists:student,id',
'content' => 'required',
));
if ($validator->fails())
return Redirect::to('dashboard')->withErrors($validator);
$user = Auth::user();
$user->decrement('sms_credit');
return Redirect::to('dashboard')->with('messageSent',1);
}
this is the route in route.php
Route::group(array('prefix' => 'dashboard','before'=>'auth'), function() {
Route::get('/', 'dashHome#index');
});
You need to define a route for the url dashboard/send_message in your routes.php file, like this:
Route::group(array('prefix' => 'dashboard','before'=>'auth'), function()
{
Route::get('/', 'dashHome#index');
Route::post('send_message', 'dashHome#postSendMessage');
});