How to access to paypal credentials from database? - laravel

I'm trying to find a optimal way to save and access my paypal credentials on laravel from my Database.
I know it is common to save this kind of data on the .env file or a config file, but I need the user to save his credentials instead of mine.
So what I really need is save the paypal credentials and give the opportunity to personalize their credentials and other data.

There are probably many ways to do this, but the most secure way in my opinion is to use a database. You can use Laravel's Eloquent to talk with your database. It is also a good idea to encrypt the user's PayPal credentials before storing them in your database.
Here is a sample of what I have done in the past to start my database. You will of course have to create the table in your database.
<?php
namespace Application\UserCredentials;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Credentials extends Eloquent
{
protected $table = 'credentials';
protected $fillable = [
'clientID',
'clientSecret',
'user_id'
];
}
Here is an example of what I have used to add this to your database.
<?php
use Credentials;
$userCredentials = Credentials->create([
'clientID' => $clientId,
'clientSecret' => $clientSecret
]);
This should get you started with what you can do with Eloquent in Laravel.

Related

Laravel - Specify database connection on the fly

I'm using 2 databases in my Laravel application. I have an SQLite DB which is local and a remote MySQL DB. Right now I need to switch env files to connect to each DB when I need to. My question is, is it possible to switch env files so the models which I'm using for both of the DB work on the corresponding DB.
This project is fairly new so if anyone knows a better way to handle this I'm all ears.
You may define several connections on your config/database.php file and access each connection via the connection method on the DB facade:
$sqliteUsers = DB::connection('sqlite')->select(...);
$mysqlUsers = DB::connection('mysql')->select(...);
Check "Using Multiple Database Connections" Section on Laravel docs for more info.
You may use on method on eloquent models:
use App\User;
$sqliteUsers = User::on('sqlite')->get()
$mysqlUsers = User::on('mysql')->get();
You may also specify a connection for an eloquent model statically:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The connection name for the model.
*
* #var string
*/
protected $connection = 'sqlite';
}
Check "Database Connection" Section on Laravel docs for more info.

Laravel's uuid type

I was reading Laravel's documentation that I noticed Laravel 7 offers a uuid type in the page for database migrations
Unfortunately I couldn't find much information about using it in the documentation. How is such a column filled when I insert a new record? Does Laravel fill it on its own? Does Laravel guarantee it to be unique or should I worry about collisions? Can I use this field to create short links for the posts of a blog, for example?
Laravel should fill this on its own by setting the keyType(untested) on your model:
protected $keyType = 'string';
The key, whether you fill it manually or not, is unique as laravel depends on Ramsey.
To manually acquire a uuid you may use the provided helper:
use Illuminate\Support\Str;
return (string) Str::uuid();

database seeding what is this why we are using

what is database seeding and why we use in laravel i am new in laravel i do not understand why we are using seeding and what is that and seeding use are in laravel relationship why we are using please explain what is that seeding
public function run()
{
DB::table('users')->insert([
'name' =>Ste::random(10),
'email' =>Ste::random(10).'#gmail.com',
'password' =>crypt('secret'),
]);
}
}
what is seeder and why should use this seeder.
Seeding a database is a process in which an initial set of data is provided to a database when it is being installed . It is especially useful when we want to populate the database with data we want to develop in future. When we are building an application we often need the database to be present before we are finishing the module for inserting data, then we can seed the database. Or in simple words seeding is creating dummy datas.
Seeding is not a must or mandatory, but in some case you will need it.
in your code we are seeding the user data with name of random string, email of randomstring#gmail.com,and password of secret.
further reading https://laravel.com/docs/5.8/seeding
DO THE RESEARCH DON'T BE LAZY

Setting default value for Laravel Migration

I am using Laravel 5.6 and has a multi-auth funcitonality and it works fine. I have user and admin tables in my DB. How can i set a default value on admin table like NAME, EMAIL and PASSWORD? So everytime i run php artisan migrate:refresh i will have the default NAME EMAIL and PASSWORD in admin table? Thank you!
Without a little clarification there are two possible answers to your question. If you are asking about columns in your database tabled having default values this has already been answered but it is simply by chaining the method default on your attribute.
$table->tinyInteger('status')->default('1');
If you are asking about populating a row in your database whenever you migrate refresh. You can use seeders:
Laravel includes a simple method of seeding your database with test
data using seed classes. All seed classes are stored in the
database/seeds directory. Seed classes may have any name you wish, but
probably should follow some sensible convention, such as
UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined
for you. From this class, you may use the call method to run other
seed classes, allowing you to control the seeding order.
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'default_name',
'email' => 'default#mail.com',
'password' => 'password',
]);
}
}
And afterwards just call:
php artisan migrate:refresh --seed

Laravel 5 creating custom registration and login from default Authentication

After 2 years experience in cakePhp , recently i started learning laravel framework. i already created the database for my new project and create a default authentication system with laravel 5 using the
php artisan make:auth
but i cant modify it according to my database design
i have a
users table(id,username,password,remember_token)
user_profiles table (id,first_name,last_name etc)
group table(id,group_name)
group_users(group_id,users_id)
users_user_profiles (user_id,user_profile_id)
so using the above table i want to implement a group based user management system.
if i use the default authentication system ,
how can i store login and registration details into two tables with a single registration from? and how can i insert the id's into pivot table users_user_profiles table automatically ?
How to create a view for the registration which contain both the login details and registration details, how can i separate them and how to access the request data separately for storing into two separate tables?
if anybody can provide a detailed description on this, it will help all laravel 5 beginners, i think its a common problem for all newbies..
please help me
Thanks
First define good your models, so you can use eloquent, for example making connection with user_profiles:
public function profiles()
{
return $this->belongsToMany('UserProfile');
}
(It will be possible only when you get UserProfile model)
Then you extend your register form by adding first_name, last_name, etc. In auth controller after creating user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Add code that create user profile:
$userProfile = UserProfile::create([
'first_name' => $data['name'],
'last_name' => $data['email']
]);
Then add it to user by attach command (if you have many of them you can use sync) :
$user->userProfiles()->attach($userProfile->id);
More info about how user Authentication work you can find exterminating file: \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php

Resources