I am new in laravel, trying to store data into multiple tables through api from one controller - laravel

There will be four tables, in which first table id is the primary key and for other tables it will be foreign key.How to insert data.
data should be related to the first table id only.

you can manage it by retrieve the id(Primary Key) of the stored ligne. Just like that
$primId = PrimModel::create([$datas]);
/* where datas are the informations you want to store
eg : $primId = PrimModel::create(['arg1' => 'val1', 'arg2'=> $val2, ...]); */
//Now you can store the others like that
ForeignModel::create(['primId' => $primId->id, ...]);
...
Hope this could help you.

Related

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

Pivot table but not using table id

Is it possible to make a Pivot Table without using table id?
users
id
biometric_id
first_name
last_name
attendances
id
biometric_id
date
emp_in
emp_out
user_attendances
user_id
attendances_biometrics_id
I wanted to ask if this is available to link it like this? Because I need to show the attendance of the user that has his biometrics.
If it is possible, how?
If attendances.biometric_id has a unique constraint on it then there should be no reason why you cannot use it as a foreign key constraint.
Assuming your tables have been setup properly with foreign key constraints, your user model would probably have something like this:
public function attendances() {
return $this->belongsToMany('App\Attendances', 'user_attendances', 'user_id', 'attendances_biometrics_id');
}

'include' data from two tables with same column name in single query

I have two tables(Reviews and Comments) with same column name userId, how can i differentiate them in parseQuery so i may get the data using the 'include' option, query is as follow:
query.whereEqualTo("businessId", parseBusiness);
query.include("userId"); // from the reviews table
query.include("lastComment");
query.include("userId"); // from he comments table
userId is a pointer to the User table

Find the last record based on a attribute of a table

In laravel, I create a migration table named 'timelogs'. Assumed that, this table have three column id,userid,value. 'id' is primary key and have auto increment property , 'userid' is foreign key. I insert data 1,2,2,2,2 and 3,4,5,6,7 for'userid' and 'value' field respectively.
Now I want to find last inserted record.Such as userid = 2 and value = 7.Here userid field contain different user's id. I want to find specific user's last record. How can I do this without using primary key?
$last_record = DB::table('timelogs')->where('userid', $user_id)->orderBy('id', 'desc')->first();
//var_dump($last_record);

Soft delete on a intermediate table for many-to-many relationship

How do I set soft delete on an intermediate table which is connecting two different types of entities? I've added deleted_at column, but the docs say that I need to put this into the model:
protected $softDelete = true;
Of course, I don't have a model for an intermediate table.
Any idea?
You can put a constraint on the Eager Load:
public function groups()
{
return $this
->belongsToMany('Group')
->whereNull('group_user.deleted_at') // Table `group_user` has column `deleted_at`
->withTimestamps(); // Table `group_user` has columns: `created_at`, `updated_at`
}
Instead of HARD deleting the relationship using:
User::find(1)->groups()->detach();
You should use something like this to SOFT delete instead:
DB::table('group_user')
->where('user_id', $user_id)
->where('group_id', $group_id)
->update(array('deleted_at' => DB::raw('NOW()')));
You could also use Laravel's Eloquent BelongsToMany method updateExistingPivot.
$model->relation->updateExistingPivot($relatedId, ['deleted_at' => Carbon\Carbon::now()]);
So to use #RonaldHulshof examples you have a User model with a groups relationship which is a belongsToMany relationship.
public function groups() {
return $this->belongsToMany(Group::class)->whereNull('groups_users.deleted_at')->withTimestamps();
}
Then in order to soft delete the pivot table entry you would do the following.
$user->groups()->updateExistingPivot($groupId, ['deleted_at' => Carbon\Carbon::now()]);
As far as I understand it; an intermediate table is simply a length of string attaching one tables record to a record in another table and as such it does not require a soft delete method.
To explain, imagine you have a Users table and a Groups table, each user can have more than one Group and each Group can belong to more than one User. Your pivot table may be User_Group or something like that and it simply contains two columns user_id and group_id.
Your User table and Group table should have a deleted_at column for soft deletes, so when you "delete" say a Group, that group association will not appear in $User->Groups() while the pivot table row has remained unaffected. If you then restore that deleted Group, it will once again appear in $User->Groups().
The pivot table row should only be affected if that group record is hard deleted, in which case the pivot rows should also be hard deleted.
Now I have explained why I do not believe you need to add soft delete to a pivot table; is there still a reason why you need this behavior?

Resources