I want to make validation on a joined table,I know that laravel 5.2 doesn't support this. what is the best practice to do that ?
any suggestions?
The best way to do this, manually authenticate the user with the values you've grabbed from your joined database.
The fastest solution I've brought up is like this:
//join the tables and name it as $joined_table_values
if(isset($joined_table_values)){
if($joined_table_values->field == 'theValueYouNeed'){
//Then authenticate manually with the values in default users table
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
}
Resource: https://laravel.com/docs/5.2/authentication#authenticating-users
Related
I'm trying to setup Laravel Sanctum to issue API tokens.
When I do $user->createToken('test');, it fails while attempting to insert the generated token into my tokens table which I had previously created to store third party API tokens.
I was expecting it to insert it into personal_access_tokens instead.
My guess is that somehow this tokens table and Token model interfere with Sanctum. In my User model, I have this method which return a user's third party API tokens:
public function tokens()
{
return $this->hasMany(Token::class);
}
Any idea why it does that and how to fix it?
The createToken method is like so :
public function createToken(string $name, array $abilities = ['*'])
{
$token = $this->tokens()->create([
'name' => $name,
'token' => hash('sha256', $plainTextToken = Str::random(40)),
'abilities' => $abilities,
]);
return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
}
but I had already in my User model a tokens method. That why is was creating the token in the wrong table.
I use Laravel 5.6, and I have a problem with my seeder.
I use this :
factory(\App\Models\Merchant::class)->create([
'email' => 'admin#domain.com',
])->each(function ($m) {
$m->stores()->save(factory(\App\Models\Store::class)->create()
->each(function ($s) {
$s->products()->save(factory(\App\Models\Product::class, 10)->create());
})
);
});
All are hasMany relations.
Doing this, I have this error :
General error: 1364 Field 'merchant_id' doesn't have a default value
(SQL: insert into stores ....)
It's like my first $stores->save( ... ) doesn't use the merchant created.
In my DB, I have one merchant created.
If I use ->make() instead of ->create(), it works for the Store, but I can't save products because it's not persisted...
Is it possible to use multiple save in factories like this ?
Your code may need a little refactoring as you may be chaining creation of models incorrectly:
factory(\App\Models\Merchant::class, 2)->create([
'email' => 'admin#domain.com',
])->each(function ($m) {
$store = factory(\App\Models\Store::class)->create(['merchent_id' => $m->id]);
factory(\App\Models\Product::class, 10)->create(['store_id' -> $store->id]);
);
});
Note: In order to use the ->each() method, you need to have a collection instance. I do not think creating only one merchant will return a collection. In the example, we create 2 merchents.
I'm using laravel 5.6 multi auth where i have 3 guards admins, professionals, users. I'm using this package https://github.com/richan-fongdasen/eloquent-blameable for created_by, updated_by fields.
I have articles table where admins can create a post and also professionals can create a post, which are viewed by the frontend users.
I have added the below code to the admin model and whenever i create a post the creared_by and updated_by(if i update) are filled in with the logged in admin user id.
public function blameable()
{
return [
'user' => \App\Admin::class,
'createdBy' => 'user_id',
'updatedBy' => 'user_id'
];
}
Now when i create a post from the professionals section i want the createdBy and updatedBy to be professional id. How to achieve this?
Do i need to create a separate model as professionalposts for the same table and use
public function blameable()
{
return [
'user' => \App\Professional::class,
'createdBy' => 'user_id',
'updatedBy' => 'user_id'
];
}
This is an important field as it
1. Displays the author name for the articles which makes users know who wrote the article.
2. Helps to list all the articles created by the professional in their dashboard.
I have different ideas in my head like created a extra column professional_id(nullable) which will be filled if posted from professional section and is set to null if posted from admin.
What is the best way to handle this?
You can use this simple library:
use Culpa\Traits\Blameable;
use Culpa\Traits\CreatedBy;
use Culpa\Traits\DeletedBy;
use Culpa\Traits\UpdatedBy;
use Illuminate\Database\Eloquent\Model
class Comment extends Model
{
use Blameable, CreatedBy, UpdatedBy;
protected $blameable = array('created', 'updated', 'deleted');
// Other model logic here
}
How to install:
https://github.com/nstapelbroek/culpa-laravel-5
I'm implementing a login API using Laravel 5.4 for a company that already has two tables where they store username and password. I'm using Tymons JWTAuth which uses the default auth. How can I override the authentication so that it gets the password from another table and not the users table?
Currently I'm using my localhost and the default users table
public function login() {
$credentials = $request->only('user', 'password');
try {
if ( ! $token = JWTAuth::attempt($credentials))
{
return response()->json([
'error' => 'Your email or password is incorrect'
], 401);
}
}
already has two tables where they store username and password
The best way you can do is copy your data to the Auth tables. If you don't want to use the password from the auth, then better rewrite it from scratch and don't use Auth to make your life simplier. Goodluck!
Decided to create a view in my database from the two tables. Seemed like the easiest solution
I was wondering if someone could point me in the right direction.
I have just started using laravel 4 and i want to add a login count to my application.
Im using the standard authentication from the laravel docs.
Im guess that i have to add something like the following SQL ... But do i add it from the routes file or the User.php model ... and then how would i call it from the login route?
$sql = "UPDATE some_table SET total_logins = total_logins + 1 WHERE user_id = $userID";
Any help would be awesome
First, instead of sql, i would use Eloquent.
Then, you can access your logged in user with the Auth::user() function, and use Eloquent increment function :
Auth::user()->increment('total_logins');
Auth::user()->save();
I would do that in your login function in your UserController file.
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
Auth::user()->increment('total_logins');
Auth::user()->save();
}