How to update data in pivot table with laravel - laravel-5

First of all, I'm using laravel 5.1. Now, the scenario is; I have an exam page where they can click a button to take the exam, the action of the button is to attach the exam_id and user_id to the pivot table called examattempt_user. Now that we have a record there, I want to save the grade of a user after the user submitted the exam (clicking the button), and save/update the grade into the pivot table where the exam_id and user_id was saved. Here's my table:
Pivot table Fields:
id
exam_id
user_id
grade

Related

Keep track of quantity and some columns in multiple tables

I'm trying to build a farm management app with Laravel and I need help with my database schema. Here are some of my tables
users (id, name)
products (id, trade_name, state, category_id)
expenses (id, category_id)
activities (id, equipment_id)
equipment (id, name, type)
Equipment can either be a tractor or implement. For tractors I want to have the following columns total_distance_traveled and a fuel_quantity
Product can be fertilizers, Insecticide, Herbicides and their state can either be liquid or solid and I want to to keep track of quantity of my products as well (quantity is in liters or kilograms)
I'm not sure if it's a good idea to include the total_distance_traveled and fuel_quantity in the equipment table and a quantity column in the products table
I've thought of creating a new stocks table and have a stockable_id, and stockable_type but that would only work for quantity since it's kind of a common column between products and equipment tables
I'ld like to keep track of total_distance_traveled, fuel_quantity for the equipment table and quantity for products table. e.g. when the total_distance_traveled is changed I'd like to know when that happened so I can have logs of my activities over time
You can create a stock table if you know that this table will only use for product and equipements:
id
stockable_id
stockable_type
quantity
total_distance_traveled // nullable as they have value only when item will be tractor,
fuel_quantity // nullable as they have value only when item will be tractor,
but for same table or if you think there can come other data too then a you can have a json column in which you can save extra attributes for specific item
id
stockable_id
stockable_type
quantity
sepecific_attributes //json where you can save any additional values based on your product
"{'total_distance_traveled':'100','fuel_quantity':20,'some_other_info':'value'}

How to Delete Related Data from pivot table in Laravel Voyager

enter image description herei have 3 tables made in Voyager and with BREAD (news - catg - news_catg).
My 'news' Table have Relationship (Belongs To Many) to 'catg' and the news_catg is the pivot table
Every think is working will except the Delete i have to Delete the Records manually from the pivot table it should be automatic like add and update
finally i found that there is bug in Voyager and there is no way to fix this issue from the admin panel so i did the flowing :
1 - deleted the Pivot table .
2- create new migration file in my app
3 -
` Schema::create('Relation table name', function (Blueprint $table) {
$table->integer('first table id')->unsigned();
$table->foreign('first table id')->references('id')
->on('first table')->onDelete('cascade');
$table->integer('second table id')->unsigned();
$table->foreign('second table id')->references('id')->on('second table');
});`
Relation table name should be like :secondtablename_firsttablename
for ex : catgs_news or it wont work !!!
and the first table id should be tablename_id like news_id
4- then you go to voyager and it will work but you have to edit the new table and add timestamps and you dont have to make bread for it
You can use the detach() function to delete data from pivot table.

3 Tables Relations by Using Eloquent

I have some relations on my db and I'm very confused how I can do this with eloquent. I have three tables that I want to connect eachother.
Users table - Contains users and these users have 3 roles (customers, managers, employee etc.) And users with manager role have more than one customers or employes.
Comps table - Contains company infos, (customers company, manager company, employee company etc.) employes can have same company.
Images table- this table contains user profile images, comp logo images etc.
I want to take a user's customers informations with images. I made the following relation between comp table and user table in User model. But I can't take comp image with this relation.
public function customers() {
return $this->belongsToMany('App\Comp', 'user_customers', 'user_id', 'comp_id');
}
user_customers table is a relation table which shows which user is a customer of which user.
user_customers table:
id,
user_id,
customer_user_id,
comp_id,
How can I get comp's logo image?

Laravel many to many pivot table accessor

I have a Users table and a Votes table and a vote_users table (many to many relationship)
In the vote_user table I have
user_id|vote_id| anonymous.
How can I change the user_id to -1 (only when sending to client) , if anonymous column has value TRUE ?

Save on different tables using Eloquent Laravel Relationships

Hi I have 3 tables "temp_quotes", "quotes" and "quote_items"
Every quote generated goes to the "temp_quotes" table at first.
At the end of my multi-step form I need the following:
1- Receive all the quotes from the "temp_quotes" table, send it to the "quote_items" table and delete it from the "temp_quotes" table
2- Generate a new entry in the "quotes" table referencing the "users" table and
the "quote_items" table
Here is all the data and how needs to be save/removed on the tables:
temp_quotes Table
ID
BRAND
MODEL
YEAR
quotes Table:
ID
USER_ID
QUOTE_ID
quote_items Table:
ID
QUOTE_ID
BRAND
MODEL
YEAR
Any help will be appreciated.

Resources