laravel question: The latest Method not found - laravel

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();
]);
});

Related

Can Laravel route model binding work with nesting for middlewares?

I'm running this code to use my project policy for middlewares. It works as long as {project} is "last in the chain". Is there any way to make it work for deeper levels too?
Route::middleware(['web', 'auth:sanctum', 'verified'])->group(function () {
//...
Route::prefix('project/{project}')->middleware('can:view,project')->group(function () { // This works, but not if I go one more level after this...
Route::get('/', function (Project $project) {
return view('projects::show', [
'project' => $project,
]);
})->name('project');
Route::prefix('settings')->middleware('can:update,project')->group(function () {
// I get 403 here and I don't think I even get through the first middleware...
});
});
});
Either this
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function (Project $project) { // this is the one line I am not sure of
// some routes
})->can('update', Project::class);
})->can('view', Project::class);
or
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function () use ($project) { // this is the one line I am not sure of
Route::get('/', function (Setting $setting) use ($project) {
return view('project.settings.show', [
'project' => $project,
'settings' => $settings
]);
});
})->can('update', Project::class);
})->can('view', Project::class);
Way I think it should be done
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::middleware(['can:update, project'])->group(function () {
Route::get('settings', function() {
// do something
})
});
})->can('view', Project::class);

How can I group routes

Route::group(['controller' => [AdminController::class], 'middleware' => ['role:administrator']], function () {
Route::get('/admin', 'index');
});
It doesn't recognize the AdminController
Route::controller(AdminController::class)
->middleware('role:administrator'))
->group(function () {
Route::get('/admin', 'index');
});
More information about route controller groups in https://laravel.com/docs/9.x/routing#route-group-controllers

How to call ApiResource with middleware in my routes?

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'
]);
});

Class App\Http\Controllers\Auth\registerController does not exist in laravel 5.8

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

How to fix Class not found error while making POST request?

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;

Resources