Laravel - Request safe method does not exist - laravel

I generated my StorePostRequest using artisan make command.
I defined rules on the rules method doing this:
public function rules()
{
return [
'title' => 'required|min:3|max:255',
'slug' => ['required', Rule::unique('posts', 'slug')],
'thumbnail' =>'required|image',
'excerpt' => 'required|min:3',
'body' => 'required|min:3',
'category_id' => 'required|exists:categories,id'
];
}
However, in my PostController, I'm not able to get validated inputs except thumbnail using the safe()->except('thumbnail') like explained here
I'm getting the error
BadMethodCallException
Method App\Http\Requests\StorePostRequest::safe does not exist.

Check your laravel/framework version by running
php artisan --version
The safe method found on the FormRequest class was only added in version 8.55.0.
Just good to keep in mind that just because you're on a version 8 of laravel framework, that doesn't mean you'll have all methods and properties found in the laravel 8.x docs. That is unless you're on the current latest version 8 of course.

Using the except() method directly on $request worked. Thanks to #JEJ for his help.
$request->except('thumbnail');

Related

Output is empty when perform a seed in nWidart Laravel

I'm using nWidart package to manager Laravel app using modules.
I perform a migrate on Settings module.
php artisan module:make-migration create_locations_table Settings
php artisan module:migrate Settings
It's ok!
When I make a seeder:
php artisan module:make-seed Locations Settings => It worked!
But: php artisan module:seed Settings
<?php
namespace Modules\Settings\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class LocationsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
$data = [
'id' => '1',
'name' => 'Hà Nội',
'parent_id' => NULL,
'type' => '1',
'district_id' => NULL,
'province_id' => NULL,
'country_id' => '79162',
'created_at' => '2020-03-26 12:01:08',
'updated_at' => '2020-03-26 12:01:08'
];
DB::table('locations')->insert($data);
}
}
The output is empty!
I think it doesn't go to the LocationsTableSeeder file because when I try dd(1) on it, the output is empty too
Can you help me?
Thanks so much!
Since you're using laravel/framework 5.7.*, you might want to install nwidart/laravel-modules ^4.0 as it is the proper version for you laravel version. See compatibilty.
If that still doesn't work, do this for the meantime:
php artisan db:seed --class=Modules\Settings\Database\Seeders\LocationsTableSeeder
Way forward
Since your project is pretty far behind. I doubt that they will provide any support for this anymore. Upgrading your laravel to ^7.x and laravel-modules to ^7.0 may fix this since there had been an issue about this and has been fixed in the latest versions.

"Class 'App\Game\User' not found" even with changed providers

When i try to register an account it comes up with this error " class App\Game\user" not found
I have changed the providers auth.php to App\Game\User::class,
and i have changed the name space on the user.php to namespace App\Game;
Game.php does exist (however nothing is coded in it and im wondering if this is the problem?)
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Game\User::class,
],
the browser highlights the return line in this part of the RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Edit: I didn't realize it meant folders in the naming convention and put them into the appropriate folders. Thank you. My apologies i couldn't find a question relating to this on stackover flow and ive only just started on laravel
First of all if you have something like App\Game\User::Class
That means you will have a game folder inside the app folder and then user.php file in that Game folder with. You can use the full namespace to call the class like \App\Game\User if it's still not working try to run composer dump-autoload to regenerate the composer files.
Laravel model stores under app>user.php and make sure your file does exist in app>game folder

Laravel exceptions not working at all. It return phpinfo.php in every case

Hi guys i'm working with laravel 5.6, i have enabled debugging in .env but still it is not returning any exception and error.
instead of any error or exception it shows a complete page of phpinfo.php
here is an example image what i am actually getting
https://www.hostinger.com/tutorials/wp-content/uploads/sites/2/2017/03/inside-the-php-info-section.png
Let me show you my code
public function store(Request $request)
{
$request->validate([
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
]);
...
}
the desired output was that if i have not entered any field i.e first_name or last_name it should provide an error that first_name or last_name is required but instead of this it return complete phpinfo.php page
Although the code sample you have provided does not have any error
I think you should try using the validation like this
$this->validate($request, [
'first_name' => 'required|min:3',
'last_name' => 'required|min:3',
]);
If you still face the error please try commenting you validation code for instance or using a dd() after validation so that we could ensure that the error is in the validation part or somewhere else.

Laravel 5.4 additional Auth parameters

I have added an email verification to the register process with Laravel 5.4. I am using the built-in Auth configuration as well. I want to now add a "verified" parameter to the authentication process. This used to work in earlier versions of 5.x, but I can't get it to work now.
Editing this file:
project\vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
I would normally add the "verified" portion of the Login validation.
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required',
'password' => 'required',
'verified' => 1,
]);
}
This doesn't appear to be working in 5.4 now. I can login without verifed being true. Is there another way I can change this without touching any back-end classes or traits? Can I do this in the LoginController instead to make it easier to persist across Laravel upgrades?
Yes you can do it. Add this to your LoginController
use Illuminate\Http\Request;
protected function credentials(Request $request)
{
return array_merge($request->only($this->username(), 'password'), ['verified' => 1]);
}
Never modify the core files. You can always override the core class methods in your controller.

Laravel request validation error

Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in app\Http\Controllers\RegistrationController.php on line 23
It doesn't work here
Registration Controller
but at the same time works fine in another controller
AuthController
The reason you're getting this error is because you're passing your validation rules to the request() helper function and not as the 2nd param to $this->validate()
You can still use the request() helper function but you just need to do:
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'password|confirmed', //<-- Is the password rule something you've created?!?
]);
Hope this helps!
function store()should be function store(Request $request) if you want to use the request. However #CBroe is right: please learn to ask your questions better.

Resources