Appending a new key into a existing array - php

it may seem to be a silly question, but i dont know why, this thing isn't working for me.
I have an exisiting array
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => $enc_password,
'date_of_adding' => date('d/m/Y'),
'support_timestamp' => $timestamp
);
now i want to insert or append a new key with value into this existing array.
where the new key and its value is
'username' => $username
what i did is that
$data['username'] = $username;
but still the thing isn't working.
Can anybody tell what i am doing wrong??

try this
$data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'password' => $enc_password,
'date_of_adding' => date('d/m/Y'),
'support_timestamp' => $timestamp
);
$data2 = array('username'=>$username);
$data = array_unshift($data,$data2);

You can do like this:
$data[]=array('username' => $username)
It will append the new array into an existing array

It should work using $data['username'] = $username;
Have a look at it here. Click the run icon

You may use
$data2 = array('username'=>$username);
$data = array_push($data,$data2);
or visit
demo

Related

Response Customized Object Transaction in Laravel

I've created a transaction but I'd like to return all data which has been created in one json. I'd like to do this using Resources in Laravel 5.5.
This is my transaction:
$firstUser = DB::transaction(function () use ($request) {
//Create Group Campany
$groupCompany = \App\GroupCompany::create([
'name' => $request['nameGroupCompany'],
'cod' => $request['codGroupCompany'],
]);
//Create Company
$company = \App\Company::create([
'group_company_id' => $groupCompany->id,
'name' => $request['nameCompany'],
'cod' => $request['codCompany'],
]);
//Create Group User
$groupUser = \App\GroupUser::create([
'name' => 'Admin',
'admin' => 1,
]);
$groupUser->companies()->attach($company->id);
//Create Person
$person = \App\Person::create([
'firstName' => $request['name'],
'middleName' => $request['middleName'],
'lastName' => $request['lastName'],
'dateOfBirth' => $request['dateOfBirth'],
]);
$person->groupUsers()->attach($groupUser->id);
//Create User
$newUser = \App\User::create([
'person_id' => $person->id,
'name' => $request['name'],
'email' => $request['email'],
'password' => bcrypt($request['password']),
]);
return ??????;
});
And finally I call my Resource:
return new FirstUserResource($firstUser);
What should I do?
Thank you very much.
Marcel
Save info in a array before save them like below:
$groupCompanya = array('name' => $request['nameGroupCompany'],
'cod' => $request['codGroupCompany']);
$groupCompany = \App\GroupCompany::create($groupCompanya);
Gather all data into a new array:
$result = array("groupCompany"=> $groupCompanya, "company" => $companya, "groupUser" => $groupUsera, "person"=>$persona, "newUser"=>$newUsera);
after convert it to json and return it:
$json = array("result" => $result);
return json_encode($json,JSON_UNESCAPED_UNICODE);
If you are returning all the data as a single json I think Collections will help you here?
$collection = collect([var1,var2]);
return $collection->toJson();

Laravel Seed - Argument 1 must be array, string given

I have the following code in a seeder:
public function develop_run()
{
echo "Running ParentUsersTableSeeder\r\n";
DB::table('parent_users')->truncate();
$faker = Factory::create('en_AU');
$rand = rand(100, 300);
for ($i = 0; $i < $rand; $i++) {
$email = $faker->email;
// Create the new user
$parentUser = ParentUser::create([
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $email,
'country_id' => rand(1, 209),
'last_access_at' => Carbon::today(),
]);
}
}
I cannot understand why the seed fails (at the $parentUser = step) with:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /home/vagrant/src/facultyenews/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 681
The items each individually show correctly if I dd() them. I have other seeds with the same syntax working perfectly, so I assume it is a simple screw up, but I cannot find it.
Any ideas welcome.
Results of dd():
dd($email);
"xlueilwitz#yahoo.com"
dd($faker->firstName);
"Seamus"
dd($faker->firstName);
"Jones"
From another seeder in the same project, this works:
private function createRows($school, $role) {
$faker = Factory::create('en_AU');
$firstName = $faker->firstName;
$lastName = $faker->lastName;
$email = $firstName.'_'.$lastName.'#'.$school->website;
// Preset Users and Schools
$school_user = SchoolUser::create([
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $email,
'password' => bcrypt('a'),
'validated_email' => 1,
'validated_at' => Carbon::now()->subDays(rand(0, 30)),
'last_login_at' => Carbon::now()->subDays(rand(0, 30)),
]);
// Preset Users and Schools
SchoolRoleUser::create([
'school_user_id' => $school_user->id,
'school_id' => $school->id,
'role_id' => $role->id,
]);
}

how to check a value of variable/data in codeigniter controller

i want to ask how do i check/see the value inside of this $datame in my code, i tried to var_dump it, i am using mozilla, it didn't show the value in console/network . Where or how do i check if there were data stored in my variable array.
public function facebook_request(){
$this->load->library('facebook/src/facebook', array('appId' => '1027885817316593', 'secret' => '6db175699897b514bf4881955b2944bb'));
$this->user = $this->facebook->getUser();
if ($this->user) {
$data['user_profile'] = $this->facebook->api('/me?fields=id,first_name,last_name,email,link,gender,locale,picture');
// Get logout url of facebook
$data['logout_url'] = $this->facebook->getLogoutUrl(array('next' => base_url() . 'index.php/oauth_login/logout'));
$datame = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $data['user_profile']['id'],
'first_name' => $data['user_profile']['first_name'],
'last_name' => $data['user_profile']['last_name'],
'email' => $data['user_profile']['email'],
'gender' => $data['user_profile']['gender'],
'locale' => $data['user_profile']['locale'],
'picture' => $data['user_profile']['picture']['data']['url'],
'link' => $data['user_profile']['link']
);
$userData = $this->user_model->checkUser2($datame);
var_dump($datame);
exit();
// Send data to profile page
$this->load->view('admin/profile.php', $data);
}
The server doesn't have access to write to that. You could pass $datame to the view you are trying to render and print it there or you could replace var_dump($datame) with error_log($datame) and then view your error log to see what it printed
change from
$userData = $this->user_model->checkUser2($datame);
var_dump($datame);
exit();
to
echo "<pre>";
print_r($datame);
exit();
$userData = $this->user_model->checkUser2($datame);

Update CC info of Braintree in PHP

I am trying to update CC info of braintree but function provided in docs is not working . and I am unable to find out the reason.
Here is my function :
public function updateCC(){
$fname = $this->input->get_post('fname');
$lname = $this->input->get_post('lname');
$expirationYear = $this->input->get_post('expirationYear');
$expirationMonth = $this->input->get_post('expirationMonth');
$cardholderName = $this->input->get_post('cardholderName');
$cvv = $this->input->get_post('cvv');
$cc_no = $this->input->get_post('cc_no');
$token = $this->input->get_post('token');
$BTCustomerID = $this->input->get_post('BTCustomerID ');
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true
)
)
));
echo json_encode(array('error'=>-1));
}
Due to this code my application is crashing .
$result = Braintree_Customer::update(
$BTCustomerID,
(
'firstName' => $fname,
'lastName' => $lname,
'creditCard' => (
'paymentMethodNonce' => 'fake-valid-nonce',
'options' => (
'updateExistingToken' => $token,
'verifyCard' => true,
)
)
));
I’m a developer at Braintree. In your call to Braintree_Customer::update(), paymentMethodNonce must be passed inside of creditCard at the same level as options. In your code you are passing the it inside of options. See this code example for reference.

Big or small characters on registration

I need add function, if someone enter username with big characters, it will be saved with small characters to db.
if(empty($_POST) === false && empty($errors) === true){
$register_data = array(
'username' => $_POST['username'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'password' => $_POST['password'],
'joined' => date('Y-m-d H:i:s'),
'email_code' => md5($_POST['username'] + microtime()),
);
strtolower()
strtolower($_POST['username']);
complete code
if(empty($_POST) === false && empty($errors) === true){
$register_data = array(
'username' => strtolower($_POST['username']),
'first_name' => strtolower($_POST['first_name']),
'last_name' => strtolower($_POST['last_name']),
'email' => $_POST['email'],
'password' => $_POST['password'],
'joined' => date('Y-m-d H:i:s'),
'email_code' => md5($_POST['username'] + microtime()),
);
}
So you mean, change the case to lower. You can use:
php in built function i.e. strtolower function like this,
$username = $_POST['username'];
$username = strtolower($username);
try strtolower() to convert a string in lowercase
strtolower($_POST['username'])
Also you need to escape your post strings to prevent sql injection
use strtolower()
$usernamestr = strtolower($username);
and insert this value in database

Categories