3 Tables Relations by Using Eloquent - laravel

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?

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'}

laravel eloquent ORM Joint multiple table without Foreign key And Multiple model

I have two tables. One is user and the other is category, I have written this Eloquent ORM query to retrieves data from category
$category = Category::where(['status'=>'active'])->orderBy('id','asc')->get();
In category table here has a field called user_id. I want to join the user table and want to retrieve the user name where category table 'user_id' == user table 'id'.
How can I do this in Laravel eloquent ORM without Foreign key And Multiple models.

how to get data from pivot table inside a many to one relation in laravel 5.6

I have 3 tables name like "product" , "user", "product_type" so in my case user and product_type having many to many relationship and user and product having one to many relationship and product and product_type having one to one relationship.
I create one pivot table for user and product_type. inside that pivot table, I added one more column for description. so in product listing page I need to display description from that pivot table.
My code look like this:
Product::with('user)->with('product_type')->get();
To get extra fields from pivot table you need to use withPivot in function of your model Like this:
public function product_type() {
return $this->belongsToMany('App\Product','product_type','product_id','user_id')->withPivot('column1', 'column2');
}
you may also refer laravel docs for it:
https://laravel.com/docs/5.7/eloquent-relationships

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 ?

Laravel 5 query to sync manytomany data with extra column, to database

I have on my database these 3 tables:
users (model User)
accounts (model Account)
user_accounts (relationship table many to many) (id, user_id, account_id, role_id)
The problem is in the user_accounts I have an additional column role_id the meaning is to invite many users with different access levels.
maybe using $user->accounts()->sync($account, false);
how to save the data in this table maybe using
From the documentation, you can do it as follows:
$user->accounts()->sync([$accountId => ['role_id' => $roleId]], false);

Resources