Inserting data into two different table with relation using Eloquent - mysql

I have to tables called customers and address.
Customer table
id
customer_name
address_id
address table
id
address
The form has customer name and address inputs together. I would like to insert address and get address Id and insert into customer table with address id and customer name. Is this possible?
my model looks like this
public function addCustomer($data)
{
$this->table('address')->insertGetId([
'address' => $data['address'],
]);
$this->table('customers')->insertGetId([
'customer_name' => $data['customer_name'],
'address_id' => //how can I get address ID,
]);
}
How to get address ID from freshly inserted row?
Thanks.

You were doing correctly. You need to initialize the address id variable and set it to the address_id in the insert of the customers table
public function addCustomer($data)
{
// storing address_id to the $address_id variable
$address_id = $this->table('address')->insertGetId([
'address' => $data['address'],
]);
$this->table('customers')->insertGetId([
'customer_name' => $data['customer_name'],
'address_id' => $address_id
// setting address_id from the above address id
]);
}

public function addCustomer($data)
{
$address = $this->table('address')->insertGetId([
'address' => $data['address'],
]);
$this->table('customers')->insertGetId([
'customer_name' => $data['customer_name'],
'address_id' => $address
]);
}

Related

Laravel 8 insert data to multiple connected tables

I have two tables: (Table A & TableB).
Table A = id(primary), name, email
Table B = user_id(foreign(id)), column1, column2
What I need to do is:
Insert a row in Table A.
Verify the insertion.
Insert row in Table B & store Table A (id) in Table B (user_id).
Currently, I'm handling it like the following.
// Create a row in Table A
$createUser = UserModel::create($userData);
// Verifying insertion of the by checking if id is set
if (isset($createUser['id'])) {
$adminData = [
'name' => $info['adminName'],
'user_id' => $createUser['id'], // insert into user_id
'email_address' => $info['adminEmail'],
'contact_number' => $info['adminNumber'],
];
$createAdmin = AdminsModel::create($adminData);
}
I'm looking for a better way for multiple chained tables to avoid an if-else ladder.
There is nothing wrong with what you are doing. But you can also use eloquent relationships.
First define this method in the UserModel.
/**
* Get the admin associated with the user.
*/
public function admin()
{
return $this->hasOne(AdminsModel::class);
}
And use it like so
$createUser = UserModel::create($userData);
$createUser->admin->create([
'name' => $info['adminName'],
'email_address' => $info['adminEmail'],
'contact_number' => $info['adminNumber'],
]);

Codeigniter: Know AI ID of query

When a user create an item, on my controller i need to send 2 SQL Query.
The first is easy to do:
$data = array(
'author' => $this->input->post('author'),
'name' => $this->input->post('name'),
);
$this->item_model->insertItem($data);
But on my second query, i need to recover, to find the ID of the query showing just before.
For example:
$data = array(
'user_id' => $_SESSION['user_id'],
'item_id' => ????,
);
$this->item_model->insertItem($data);
Thanks
For fetching the last inserted ID of variable $item_id in the same transaction of your controller, you can get the ID of record in the same session as follows:
$item_id = $this->db->insert_id();

Yii2 query for users in group

I had a DB that had a user table and a group table and the group table had a column user_id which made it simply to return a list of users in a group:
$users = User::find()
->where(['{{user}}.group_id' => $group_id])
->all();
Now the user_id column is gone and there is a third table group_user with user_id and group_id columns for the relationship.
I tried this:
$users = User::find()
->innerJoinWith('group_user)
->where(['{{group_user}}.group_id' => $group_id])
but received this error:
User has no relation named "group_user"
But I set the relationship in the User model:
public function getGroupUser() {
return $this->hasOne(GroupUser::className(), ['user_id' => 'id']);
}
What am I missing? This is used in a Humhub API.
I would reprogram your getGroupUser (renaming it to getGroups) relation using viaTable:
public function getGroups() {
return $this->hasMany(Group::className(), ['user_group.id_group' => 'group.id'])
->viaTable('user_group', ['user.id' => 'user_group.id_user']);
}
That would give you the Group(s) a User belongs to. But I think you are aiming to get the Users that belong to a given group, so similarly I would create a getUsers relation in your Group model:
public function getUsers() {
return $this->hasMany(User::className(), ['id' => 'user_id'])
->viaTable('group_user', ['group_id' => 'id']);
}
Then:
$group = Group::findOne($id_group);
$users = $group->getUsers()->all();

Yii2 order by 3rd table's value

I'm working with Yii2 and I can't solve it out. I need to OrderBy first table's rows by the third table's column.
First table: user [id, ....]
Second table: info [id, user_id, city_id, ...]
Third table: city [id, title, latitude, longitude]
models/User:
public function getInfo()
{
return $this->hasOne(InfoClear::className(), ['user_id' => 'id']);
}
models/Info
public function getCity()
{
return $this->hasOne(City::className(), ['id' => 'city_id']);
}
models/RecommendedSearch
$query = User::find()->joinWith(['info']);
Also I have to connect somehow 'city' table
Somthing like this
User::find()->joinWith(['info' => function(\yii\db\ActiveQuery $q){
$q->joinWith('city');
}]);
Or try
User::find()->with('info.city')->all();

Laravel 5 Creating a New Record that BelongsTo 3 Other Tables

I have 4 tables Users, Regions, Rentals, and Locations
Each User hasMany Rentals
Each Region hasMany Rentals
Each Location hasMany Rentals
Basically a Rental belongsTo all 3 tables
Region - id, name
Location - id, street_address, city, province, postal_code
Rental - id, region_id, location_id, user_id
I've set up the hasMany and belongsTo relationships between these tables and tested them in Tinker, but now I want to create and update rentals.
The region_id is passed up through the request - $regionId
The user_id is the Auth::id() - $userId
The location_id is found by taking only() part of the request and doing a check of the locations table, and if it exists I grab the location_id - $locationId
The remaining post data is grabbed using only() again for that data - $rentalData
All this works to this point, but how do you create and update rentals using the ids and data I've extracted, this almost works:
Location::find($locationId)->rentals()->create($rentalData);
But, need to get the $locationId and $userId into the mix somehow and it doesn't seem right to make them fillable.
I've been playing with it like this so far:
// Retrieve the chosen rental region
$regionId = Region::find($request->input('region_id'));
// Retrieve authenticated user id
$userId = Auth::id();
// Retrieve rental location data
$rentalLocationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$locationData = RentalLocation::firstOrCreate($rentalLocationData);
$locationId = $locationData->id;
// Create the rental...?
$rental = Location::find($locationId)->rentals()->create($rentalData);
UPDATE
So I can keep going dropped using the ORM and did this, but I'd still like to understand how Eloquent works beyond the basics I learnt watching Laracast videos so any help would be appreciated, right now I just find it really confusing:
// Retrieve rental location data from the request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$rentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the foreign key ids not included in request
$foreignKeyIds = [ 'user_id' => Auth::id(), 'rental_location_id' => $rentalLocation->id ];
// Retrieve request data for creating a rental, and merge with foreign key ids
$requestData = $request->only('region_id', 'stall', 'level', 'description');
$rentalData = array_merge($foreignKeyIds, $requestData);
// Insert new rental with all field attributes included
DB::table('rentals')->insert($rentalData);
UPDATE
What about this solutions?
RentalRequest checks the region_id exists, and the user will always be the Auth::user().
public function store(RentalRequest $request)
{
// Retrieve the authenticated user
$User = Auth::user();
// Retrieve rental location data from request
$requestData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
// Does the location already exist? If not create and persist it to the database
$RentalLocation = RentalLocation::firstOrCreate($requestData);
// Retrieve the region for inserting the rental
$Region = Region::find($request->input('region_id'));
// Retrieve rental data from request
$requestData = $request->only('stall', 'level', 'description');
// Create a new rental, fill with request data, and add relationships
$rental = new Rental;
$rental->fill($requestData);
$rental->owner()->associate($User);
$rental->location()->associate($RentalLocation);
// Persist rental to database
$rental = $Region->rentals()->save($rental);
// Return rental data for capture by AngularJS interceptor
// TODO: filter rental data don't need it all returned to client
return response()->json([
'rental' => $rental,
'action' => 'rental_registered',
'message' => trans('rental.registered')
]);
}
I don't understand why to make it so complex?
try this:
Solution 1
$User = User::find(Auth::id());
if(!$User) {
return 'no user';
}
$Region = Region::find($request->input('region_id'));
if(!$Region) {
return 'no region';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);
Solution 2
// ->remember() for caching request to prevent many queries, for faster result use apc cache in config files, no need to use memcache when You're using 1 app server
if(!User::where('id', '=', Auth::id())->remember(1440)->exists()) {
return 'no user';
}
if(!Region::where('id', '=', $request->input('region_id'))->remember(1440)->exists()) {
return 'no user';
}
$locationData = $request->only('street_address', 'city', 'province', 'country', 'postal_code');
$Location = Location::firstOrCreate($locationData);
$rentalData = [
'user_id' => $User->id,
'region_id' => $Region->id,
'location_id' => $Location->id
];
$Rental = Rental::firstOrCreate($rentalData);