If I need to retrieve a single row of data from the database, why can't I do this when I want to insert that id into another table:
'user_id' => $query->row()->id
Is there any way to do this without a foreach loop if all I want is a single piece of data?
The full code is:
$this->db->where('username', $this->input->post('username'));
$query = $this->db->get('members');
$data = array(
'first' => $this->input->post('first'),
'last' => $this->input->post('last'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zip' => $this->input->post('zip'),
'comments' => $this->input->post('comments'),
'username' => $this->input->post('username'),
'user_id' => $query->row()->id
);
$this->db->insert('contact', $data);
$query->row()->id is correct, yes. Given that the field is called 'id' in your table, and the row exists that you want. You can check $query->num_rows() to see how many rows were returned.
$the_data= $query->row();
$user_id= $the_data->id;
don't think you can do method chaining like in your example.
Related
I am new to laravel and try to add record in db. record is successfully inserted but now i need to getLast inserted id.
Insert record array here:
User::insert(
[
'first_name' => $data->first_name,
'last_name' => $data->last_name,
'email' => $data->signup_email,
'contact' => $data->signup_contact,
'role_id' => $data->role_id,
'password' => bcrypt($data->password_contact),
'confirm_passsword'=>bcrypt($data->confirmpassword_contact),
'created_at' => date('Y-m-d'),
'updated_at' => date('Y-m-d'),
]
);
Thanks to all for any help.
Just replace "insert" with "insertGetId". Thats it.
I have 2 tables within one function that I'd like to save data to. One is the Users table, and the second one is a Clinic table.
My user's table is currently working correctly, but I'm unsure if it's 'best practice':
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])
->clinic()->create($request->only([
'name' => Str::title('clinic_name'),
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
My problem occurs at the 'name' column of the Clinic table. It just doesn't save it, even though it's in the $fillable array in my Clinic column:
protected $fillable = ['user_id', 'name', 'telephone'', 'address_1',
'address_2', 'city', 'postcode'];
I have attempted to 'Chain' the methods together, as I want to save the 'user_id' within the Clinic table because they're related.
Many thanks for your help.
You're overriding the name key in your $request->only() call:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create($request->only([
'name' => Str::title('clinic_name'), // This is overriding your 'name' field.
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
If you want to run Str::title() over the requests clinic_name, you'll need to assign the attributes manually like so:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create([
'name' => Str::title($request->get('clinic_name')),
'telephone' => $request->get('telephone'),
'address_1' => $request->get('address_1'),
'address_2' => $request->get('address_2'),
'city' => $request->get('city'),
'postcode' => $request->get('postcode'),
]);
Note: Just as a tip, you can also just retrieve request input as a property like so:
->create([
'name' => $request->name // Performs $request->get('name').
])
You can't have 'name' => Str::title('clinic_name') when using create(), it must be a single key as 'name'.
You can use the following before creating the user:
$request->replace('name' => Str::title('clinic_name'));
Trying to insert random emails like test''#test.com, etc.. and they are inserted into the database. Shouldn't this be automatically prevented by Active Record?
My query:
$data = array(
'name' => ''.$name.'' ,
'email' => ''.$email.'' ,
'country' => ''.$country.'' ,
'phone' => ''.$phone.'' ,
'compid' => ''.$compID.''
);
$this->db->insert('people', $data);
Where all variables are taken from user POST input, such as
$this->input->post('address')
for address.
shouldn't this be:
$data = array(
'name' => $name,
'email' => $email,
'country' => $country,
'phone' => $phone,
'compid' => $compID
);
$this->db->insert('people', $data);
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.
//here is my function block
public function create_accnt()
{
$post = $this->input->post(); //gets all possible posts in array form
echo "Post array: ";print_r($post); // just to make sure that my array has values
$data = array(
'userid' => $post['user_id'],
'lastname' => $post['lname'],
'firstname' => $post['fname'],
'username' => $post['username'],
'password' => $post['password'],
'usertype' => $post['user_type'],
'status' => 'A'
); // assigning my values to their specific fields where they will be inserted
$query = $this->db->insert('accounts', $data); //insertion via active record
echo "<br/>Result of db last query: ";
echo $this->db->last_query();//to see how my query looks in code form
if($query)
{
return true;
}
else
return false;
// end if
} // end function
//here is the result of the code above
//Post array: Array ( [user_id] => 123456 [lname] => Smith [fname] => John [username] => John [password] => password [c_password] => password [user_type] => S [btn_create] => Create )
//Result of db last query: INSERT INTO accounts (userid, lastname, firstname, username, password, usertype, status) VALUES ('', '', '', '', '', '', '')
here why is it that after the query my VALUES were all spaces? by the way i am using CodeIgniter and my db driver is PDO and my database is DBFoxPro.
I ended up using $this->db->query() function of CI and manualy providing my query statement with it. I think $this->db->insert() wont work on my db DBF-foxpro via PDO driver. I also had a problem about $this->db->where() function. now Im just using $this->db->query(). Thank you for all your help.
instead of
$data = array(
'userid' => $post['user_id'],
'lastname' => $post['lname'],
'firstname' => $post['fname'],
'username' => $post['username'],
'password' => $post['password'],
'usertype' => $post['user_type'],
'status' => 'A'
);
use
$data = array(
'userid' => $this->input->post('user_id'),
'lastname' => $this->input->post('lname'),
'firstname' => $this->input->post('fname'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'usertype' => $this->input->post('user_type'),
'status' => 'A'
);