How to give Free Bonus in laravel? - php

I am runing a bitcoin investment website. I want to give user free bonus of 0.005 points on registration. I have tried following in my registration Controller to fill database table. It works perfectly but after 100 user it creat some type of bug and I get error in user login it says:
But I remove all rows of depostit table all functions work perfectly.
trying to get property of non object
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'under_reference' => $data['reference'],
'password' => bcrypt($data['password']),
'verifyToken' => Str::random(40),
'reference' => Str::random(12),
'status' => $status,
'image' => $image25
]);
$deposit = Deposit::create([
'deposit_number' => date('ymd').Str::random(6).rand(11,99),
'user_id' => $user->id,
'plan_id' => "7",
'percent' => "1",
'time' => "30",
'compound_id' => "2",
'amount' => "0.005",
'status' => "0"
]);

Related

Laravel: Factory Fake Car Data

I created a new factory in Laravel called "CarFactory.php" and I want to use https://github.com/pelmered/fake-car.
This is the fake data that I will insert into my database using Tinker:
return [
'name' => $this->faker->vehicleBrand(),
'founded' => $this->faker->biasedNumberBetween(1998,2017, 'sqrt'),
'description' => $this->faker->paragraph()
];
In the Laravel usage code, I'm not sure where to put this (e.g. Car Model, CarFactory):
$faker->addProvider(new \Faker\Provider\Fakecar($faker));
$v = $faker->vehicleArray();
return [
'vehicle_type' => 'car',
'vin' => $faker->vin,
'registration_no' => $faker->vehicleRegistration,
'type' => $faker->vehicleType,
'fuel' => $faker->vehicleFuelType,
'brand' => $v['brand'],
'model' => $v['model'],
'year' => $faker->biasedNumberBetween(1998,2017, 'sqrt'),
];
Help is needed to know how to set this up.

Laravel: Edit value only if it appears in the request?

in my app the user can update the info of stripe connected account, however I ONLY want to actullay update the value of the fields that appear in the request payload, I could do this with a simple if check but the way I update the stripe array method makes this issue more complicated .
Is there any syntax sugar or trick to make this easier.
How my update method looks;
public function editConnectedAccount(Request $request)
{
$account = Account::retrieve($request->connectedAccountId);
Account::update(
$request->connectedAccountId,
[
'type' => 'custom',
'country' => 'ES',
'email' => $request->userEmail,
'business_type' => 'individual',
'tos_acceptance' => [ 'date' => Carbon::now()->timestamp, 'ip' => '83.46.154.71' ],
'individual' =>
[
'dob' => [ 'day' => $request->userDOBday, 'month' => $request->userDOBmonth, 'year' => $request->userDOByear ],
'first_name' => $request->userName,
'email' => $request->userEmail,
'phone' => $request->userPhone,
'last_name' => $request->userSurname,
//'ssn_last_4' => 7871,
'address' => [ 'city' => $request->userBusinessCity, 'line1' => $request->userBusinessAddress, 'postal_code' => $request->userBusinessZipCode, 'state' => $request->userBusinessCity ]
],
'business_profile' =>
[
'mcc' => 5812, //got it
'description' => '',
//'url' => 'https://www.youtube.com/?hl=es&gl=ES', //got it
],
'capabilities' => [
'card_payments' => ['requested' => true],
'transfers' => ['requested' => true],
],
]
);
return response()->json([
'account' => $account,
], 200);
Consider using a Form Request where you preform validation. This will neaten up your controller for a start and also make validation (never trust user input!) reusable.
Assuming validation is successful, calling $request->validated() from inside your controller method will return only the fields present and validated. You can then use either fill($request->validated()) or update($request->validated()).

keep getting an error while saving data into database in laravel

I am using laravel ver "laravel/framework": "5.8.*"
I want to save the data into database with post request and my post data is
I have searched on this many topics but I can't get any information on this specific topic. If anyone can, it will be very helpful to me.
I saw my previous codes they this type of code in laravel but they work fine why this is not saving and getting an error.
{
"userID": 11,
"fullname": "dharmendra shah",
"fatherName": "manohar shah",
"Mothername": "manorama devi",
"dobdate": "21",
"dobmonth": "August",
"dobYear": "1999",
"caste": "OBC",
"casteCertificateIs": "state",
"emailAddress": "dharmonly1999#gmail.com",
"gender": "Male",
"handiCappedStatus": "Not handicapped",
"nationality": "india",
"state": "Rajasthan",
"district": "bikaner",
"tahsil": null,
"village": "NA",
"maritalStatus": "UnMarried",
"spouseName": null,
"children": null
}
In laravel route my controller is :
public function store(Request $request)
{
$this->validate($request, [
'fullname' => 'required|max:25',
'fatherName' => 'required|max:25',
'Mothername' => 'required|max:25',
'dobdate' => 'required',
'dobmonth' => 'required',
'dobmonth' => 'required',
'dobYear' => 'required',
'caste' => 'required',
'casteCertificateIs'=>'required',
'emailAddress' => 'required|email',
'gender' => 'required',
'handiCappedStatus' => 'required',
'nationality' => 'required',
'state' => 'required',
'district' => 'required',
'block' => 'sometimes|max:20',
'tahsil' => 'sometimes|max:20',
'village' => 'sometimes|max:20',
'maritalStatus' => 'required',
'spouseName' => 'sometimes|max:20',
'children' => 'sometimes|max:5|integer'
]);
$userInformationSave = new BauserInformation([
'userID' => \Auth::user()->id,
'fullname' => $request->get('fullname'),
'fatherName' => $request->get('fatherName'),
'Mothername' => $request->get('Mothername'),
'dobdate' => $request->get('dobdate'),
'dobmonth' => $request->get('dobmonth'),
'dobYear' => $request->get('dobYear'),
'caste' => $request->get('caste'),
'casteCertificateIs' => $request->get('casteCertificateIs'),
'emailAddress' => $request->get('emailAddress'),
'gender' => $request->get('gender'),
'handiCappedStatus' => $request->get('handiCappedStatus'),
'nationality' => $request->get('nationality'),
'state' => $request->get('state'),
'district' => $request->get('district'),
'block' => $request->get('block'),
'tahsil' => $request->get('tahsil'),
'village' => $request->get('village'),
'maritalStatus' => $request->get('maritalStatus'),
'spouseName' => $request->get('spouseName'),
'children' => $request->get('children')
]);
$userInformationSave->save();
return redirect()->route('home')->with('message', 'Your information is saved now');
}
The model is
class BauserInformation extends Model
{
protected $table = ['userFinalInformation'];
protected $fillable = ['userID', 'fullname', 'fatherName', 'Mothername', 'dobdate', 'dobmonth', 'dobYear', 'caste', 'casteCertificateIs', 'emailAddress', 'gender', 'handiCappedStatus', 'nationality', 'state', 'district', 'Block', 'tahsil', 'village', 'maritalStatus', 'spouseName', 'children'];
}
the error argument i am getting is
ErrorException (E_NOTICE)
Array to string conversion
You are complicating things.
On top of your controller put: use Illuminate\Support\Facades\Validator;.
Delete $userInformationSave.
Validate inputs:
$validator = Validator::make($request->all(), [
'fullname' => ['required', 'max:25'],
... every other input like I have done
]);
if ($validator->fails()) {
return redirect('/bla bla bla/create')->withErrors($validator);
}
And save the model:
BauserInformation::create($request->all());
EDIT #1
In you model, you should declare table and A_I like this:
protected $table = 'userFinalInformation';
protected $primaryKey = 'userID';
I think this will do the job.

Braintree php back-end

I clone braintree project from https://github.com/braintree/braintree_php_example. Than I created account https://www.braintreepayments.com/sandbox. I must to return client_token. I debug this code
$result = Braintree\Transaction::sale([
'amount' => $amount,
'paymentMethodNonce' => $nonce,
'options' => [
'submitForSettlement' => true
]
]);
var_dump($result->transaction);
But token = null. Maybe my steps are incorrect?
////////////////////////////////
I did it!
I create user
$result = Braintree_Customer::create([
'firstName' => 'Mike',
'lastName' => 'Jones',
'company' => 'Jones Co.',
'email' => 'mike.jones#example.com',
'phone' => '281.330.8004',
'fax' => '419.555.1235',
'website' => 'http://example.com']);
Than I get customer_id
$result->customer->id;
Than I get token
$clientToken = Braintree_ClientToken::generate([
"customerId" => $result->customer->id
]);
Maybe problem with custom register in https://www.braintreepayments.com/sandbox.
Maybe I didn't put all information

Create record with Relation Laravel 5.1

Hi i have the next code for create my records
Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state' => 1,
'profile_id' => 1,
'institution_id' => Institution::max('id'),
]);
The last attributes for the User thats correct implement so?
The last 3 user attributes , it is correct to do it that way? or is there a better
Using Institution::max('id') creates a race condition. Since the create() static method of Eloquent::Model returns the newly-created model, you can just do:
$institution = Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state_id' => 1,
'profile_id' => 1,
'institution_id' => $institution->id,
]);
Creating a record with known parent ids is generally fine if your goal is to minimize the number of database queries and you have the ids of the related models.
Another way to do it, though it triggers more update queries, is to use Eloquent's built-in methods for adding related models. For example:
$institution = Institution::create($request->all());
$state = State::find(1);
$profile = Profile::find(1);
$user = new User([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password']
]);
$user->state()->associate($state);
$user->profile()->associate($profile);
$user->profile()->associate($institution);
$user->save();
However, in this situation, since the related models are not already loaded, and you know their ids, there is no need to fetch them only to associate them with the User.

Categories