Laravel AuthController not updating function body - php

For a school project I am trying to remove Eloquent from the core of my project (have to work directly with DB, cannot use ORM).
I am now trying to modify the create function of the AuthController, but it does not seem to be updating. (Using XAMPP with PHP7.)
As you can see I tried to replace the User::create default with the DB::insert statement inside of AuthController's create function.
protected function create(array $data)
{
return DB::insert('INSERT INTO users (firstname,lastname,birthday,gender,email,password) values(?,?,?,?,?,?)',
array(
$data['firstname'],
$data['lastname'],
$data['birthday'],
$data['gender'],
$data['email'],
bcrypt($data['password'])
)
);
// OLD FUNCTION BODY:
/*return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'birthday' => $data['birthday'],
'gender' => $data['gender'],
'email' => $data['email'],
'password' => bcrypt($data['password'])
]);*/
}
But now I keep getting:
FatalThrowableError in AuthController.php line 71: Fatal error: Call to undefined method App\User::create()
I believe that the function needs more than DB::insert, as it says, that it should return a new instance of the User object, but at the moment I cannot even test what this would do as it is still trying to call the old function body. I have even deleted the User::create section alltogether.
Is this a XAMP thing? I had no problem updating and saving other files of this project, but this controller does not make it through it seems like.

Try to clean Laravel cache. Then use this:
return DB::insert(array(
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'birthday' => $data['birthday'],
'gender' => $data['gender'],
'email' => $data['email'],
'password' => bcrypt($data['password'])
)
);
If you will still get the error, please post contents of 71th line of AuthController.php

You are returning two entirely different thing.
return DB::insert
vs
return User::create
See what User::create returns and go from there.

Related

Laravel 9: testing get profile (by slug) for created row

I wrote test:
public function test_hello_world(){
$test = User::create(['name' => 'Test',
'email' => 'test#test.com',
'password' => 'password',
]);
Profile::create([
'user_id' => $test->id,
'name' => 'Test',
'slug' => 'test'
]);
$this->get('/profile/test')
->assertStatus(200);
}
What this code should testing? After get to this url it should display details about profile. If profile with this slug doesn't exist, we have error 404. In this test I create user and profile table (this 2 tables is connection) but after run test I have the error:
Expected response status code [200] but received 404. Failed
asserting that 200 is identical to 404.
Why I have this error since I created profile with this slug? For me the best option will be create testing database with a lot of records and conduct all test on it. Is it possible? How do that?
#Edit
I have a route and controller's method which display user's profile. If I go (in web browser) to localhost/profile/slug, it works, if this profile exist. My controller's method look like this:
public function show($slug) {
$profile = Profile::where('slug', $slug)
->firstOrFail();
return Inertia::render('Profile/Single', [
'profile' => $profile,
]);
}
And route:
Route::get('/profile/{slug}',
[ProfileController::class, 'show'])
->name('profile.show');
According to your requirement you have to create route for getting profile from slug name. You did wrong in single function. Without creating route it will not worked.
So below example may work for you.
For example:-
For creating data
public function createData(){
$user = User::create(['name' => 'Test',
'email' => 'test#test.com',
'password' => 'password',
]);
Profile::create([
'user_id' => $user->id,
'name' => 'Test',
'slug' => 'test'
]);
return redirect()->back()->with('success', 'Data created');
}
For Getting Data
public function getDataBySlug($slug){
$profile = Profile::where('slug',$slug)->firstOrFail();
return redirect('/dashboard')->with('slug', $slug);
}
In route file
you have to mention table name and column name {profile:slug} instead of id
Route::get('/profile/create', 'Controller#createData');
Route::get('/profile/{profile:slug}', 'Controller#getDataBySlug');
Your route definition is wrong please do as above

Can I ignore specific migrations when running Laravel unit tests

When running tests in Laravel (php artisan test), I get an error 'duplicate column name: created_at'. This only occurs when i have field additions in the migrations directory. Am i able to ignore specific files when running Laravel tests? Or is there another way to get round this issue?
migrations:
if i just have this table, the tests work fine:
2021_06_18_134444_create_users_tables.php
after creating this migration, the tests fail:
2021_07_08_135544_add_timestamps_to_all_tables.php
a simple test:
use RefreshDatabase;
public function test_redirect_to_home_page_after_login()
{
$user = User::factory()->make([
'name' => 'Test',
'email' => 'test#hotmail.com',
'password' => bcrypt('123456')
]);
$response = $this->post('login', [
'name' => 'Test',
'email' => 'test#hotmail.com',
'password' => '123456'
]);
$response->assertRedirect('/');
$response->assertSessionHasErrors();
}

Show error of two strings are equal in unit testing

I have created a small Laravel project and I am applying unit testing on my project. When I fill wrong credentials in function, it doesn't redirect to the login page and shows error on terminal saying Failed asserting that two strings are equal. Here is my code...
$credentials = [
'email' => 'test#gmail.com',
'password' => 'wrongcode'
];
$this->post('/login', $credentials)->assertRedirect('/login');
But when i change assertRedirect('/login') to assertRedirect('/') , it works fine
$credentials = [
'email' => 'test1234#gmail.com',
'password' => '98756412'
];
$this->post('/login', $credentials)->assertRedirect('/');
assertRedirect checks two string, one of them is an argument of method, 2nd is redirect path. Looks like everything works fine. You write a test, test failed, you have feedback to improve application. In that case, the redirect path is different than expected by you.
There should be 2 different methods for an individual scenario like below.
public function testCorrectCredential() {
$credentials = [
'email' => 'test1234#gmail.com',
'password' => '98756412'
];
$this->post('/login', $credentials)->assertRedirect('/');
}
public function testInCorrectCredential() {
$credentials = [
'email' => 'incorrect#gmail.com',
'password' => '98756412'
];
$this->post('/login', $credentials)->assertRedirect('/incorrect-url');
}

Laratrust attachRole returns 404 error on staging

I am using the Laratrust package for my roles management. Everything worked fine on my local developement. However, I've tried to upload my project to my server. Now, if I want to attach or detach a role, my application just redirects to a 404 error page.
What is going on?
This occures on registration, this is my registration logic:
myController.php
protected function create(array $data)
{
$user = User::create([
'firstname' => $data['firstname'],
'surname' => $data['surname'],
'email' => $data['email'],
'gender' => $data['gender'],
'password' => Hash::make($data['password']),
]);
$user->attachRole('user');
$verifyUser = \App\VerifyEmail::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user, $verifyUser));
return $user;
}
If i remove the $user->attachRole, everything works fine.
I am not getting any errors, just the 404 page. How can I fix this?
I've created a new application key, I have run all my migrations and installed all composer packages.

A four digit year could not be found Data missing

I'm trying to seed using factories in Laravel 5.2
My code dies in the User factory:
$factory->define(App\User::class, function (Faker\Generator $faker) {
$countries = Countries::all()->pluck('id')->toArray();
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'grade_id' => $faker->numberBetween(1, 5),
'country_id' => $faker->randomElement($countries),
'city' => $faker->city,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude,
'role_id' => $faker->numberBetween(1, 3),
'verified' => true,
'remember_token' => str_random(10),
'provider' => '',
'provider_id' => str_random(5)
];
});
Giving me this error:
A four digit year could not be found Data missing
I found the cause, but don't know how to fix it.
When I call the factory, I call it like that:
factory(User::class)->create(['role_id',2]);
If I call it like that:
factory(User::class)->create();
I get no more error.
But I really need to seed different kind of users...
Any idea???
have you tried using key value array in the create method:
factory(User::class)->create(['role_id' => 2]);
I might be late to the party, I was having the same problem and it turns out its because I provided a key without a value in the array returned.
get rid of 'provider' => ''.
As to the cause of the problem i really don't know but it has something to do with Carbon
I had the same issue when using mass assignment and it turned I had forgotten to wrap my array of inputs in the request helper function
That is I had
Model::create([form inputs]);
Instead of
Model::create(request([form inputs]);
Just incase someone comes across it.
I recently encounter the same problem, lately I found out that the only problem is I put an invalid value to the timestamp column type.
In my case I have column email_verified_at [timestamp]. I miss understood I put a string like company#example.com instead of date values, it should be now().

Categories