I have following tables.
Users
id
name
Events
id
name
Cards
id
name
Transfers
id
event_id
card_id
An user has many events and cards. An user can accepts cards from other user which he met at an event.A row will be added under transfers table whenever a card is accepted by a user, this row links a card_id with event_id. I am looking for a way to check a card (card_id) is added under the transfers table for a logged in user's event(event_id).
eg:-
auth()->user()->events->transfers->where(([['card_id', '=',$cardid]])->find());
Can someone help me by telling what is the best way to handle above situation using eloquent?
Add a belongsToMany-relation
Take a look at the official documentation: https://laravel.com/docs/5.1/eloquent-relationships
Related
I have an e-commerce project that contains there type of user admins,merchants and normall user in merchant section I need to make employees for merchant, like each merchant will have many employees with him and I'm confused between two structures for database first one:
In the same merchants table add another column named ( merchant_id ) it belongs to the parent of merchant.
id - name - email - password - merchant_id
The second:
Make a separate table like ( employees ) that the merchant has many employees.
id- name- email - password - merchant_id
The problem that I'm confused with is like when I give the employees permissions!
please guy's any suggestions for something like this probelm ?
I created a diagram to show my point of view. Please consider the following image:
Explaining the diagram:
A merchand hasMany Employees;
An employee belongsTo a merchand (foreign key merchand_id to tbl_merchands);
An employee belongsTo a permission (foreign key permission_id to tbl_permissions);
A permissions hasMany employees;
Code the relations in the models and you are good to go. To validate if a user has permission to access some Route(), you can use a middleware.
NOTE: I created a tbl_permissions because of your request. Maybe I would rename to tbl_groups (admins, users, etc) and use the table globally.
Hope it guides you. Regards!
COMMENT #1:
To control the page access you may use a middleware. You can create a table for products with foreign keys to merchand_id and employee_id. To get products associated to an employee, you need to create a Model plus the relation to the products. To retrive the data, do Employees::find($employee_id)->products. Hope you know how to code in laravel otherwise you will have an hard time coding this tips.
I have a form which is submitted by a student and needs approval by different people based on the type of form. How can I achieve this in Laravel?
Ex. Student 1 submits Form type 1 which requires approval of Instructor and advisor in that order. The advisor would get notification to approve only when instructor approves it.
I have tried creating columns for each type of approver around(5) and check if the approver has approved and set the field. The issue with this approach is that the table has many columns.
I created a table with approvers and the priority for the approver for the specific form and based on that send the notification.
I know there should be a better way to achieve this.
$firstApprover = $form->approvallist->where('priority', '1')->first();
** Here approvallist is a relationship to a ApprovalList table which contains the list of people who approve this particular form type.
You can maintain a many-to-many relation. The tables would be, students, form_types and the pivot table could be form_type_student. In the pivot table, you will store the type of the form, student id and additionally two fields. One is approver_id (the person who approves) and is_approved(boolean) whether it is approved or not.
And then you can check if the form type submitted by the student was approved or not and if approved who approved it
There are "approval" components ready to use for Laravel. For example first or second. Just google Laravel approval and pick whichever suits your needs.
Let's assume that you are using the Application and Reviewer tables. I suggest linking them with many to many relation. Pivot table can have custom fields "approved" (0 by default) and "priority" in the pivot table. Set the Application's approved flag only when the last confirmation is received.
See https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
Need some help.
We have installed and working on UserFrosting. In this we have 2 tables one for USER and another for CUSTOMER. So the thing that we want to achieve, want to provide a option from which we can assign a customer in USER.
For this we have created 3rd table where we want to make relationship between USER and CUSTOMER.
The issue is, we want to save Customer id in the 3rd table with User's id. So basically in 3rd table we will show user id and associated customer's id.
So please suggest.
Scenario:
I have the task to add a new field on a form, which is called account number. When a user clicks submit, it goes and submit the appropriate data to the appropriate tables...not important.
Currently, the tables that are involved are 3: customers, orders, and accounts.
The tables structures are:
Customers -> customer_id (PK), ...
Orders -> order_id (PK), customer_id (FK), ...
Accounts -> order_id (PK), account_number, ...
As you can see, a customer can have many orders, and each orders can have only one account.
Even though in the table structure I added PK and FK, the database engine is actually MyISAM, which doesn't support transactions and relationships. To add "relationship" between tables, the previous developer(s) decided to add the appropriate PK and FK to "fake" the relationships between tables.
PLEASE NOTE: I did not have any part of creating the database and tables; it was given to me as is.
The business logic is that a customer can have only one account.
As far as I know, the way the tables are mapped out, a customer has more than one account.
Without redesigning the tables, it looks like that I don't have a choice.
This is what I have in mind without redesigning the tables:
Create a SQL script to clean up and update account numbers in accounts, so that each account will have the same account number for a particular customer...even though a customer has many oders.
Anywhere in the web app that can insert an account number, I have to check to see if the customer has an account number. If so, then come back with a message stating that the customer already has an account number...maybe select the account number or update it with a new one. If not, then insert it.
Any better options?
By the way, I'm using MySQL and PHP/CodeIgniter.
I am trying to understand the database design for an e-commerce site. I am having trouble understanding on what to do in the following situation. Say the user creates an account for the first time, and makes an order. I can write php code which will add the user_id (primary-key), first name & last name. But what if I want to add the user_id in the orders table (user_id in the orders tables is a foreign key). How do I get the value, as the user_id in the customers table is auto incrementing?
Are you using two tables one for storing user details and other for placing orders ?(which is better) If so my recommendation would be to use username (not first name or last name) as primary key and store the username as SESSION variable for each login and for each order add the username in order table along with orders details
There are couple of ways to do that, you can access the newly generated user_id by LAST_INSERT_ID() and use it in subsequent transaction, or you can store it in session and use it later when inserting in Orders table.