Laravel 5 from Pivot-Table to Many - laravel

how can I access in Laravel 5 from the pivot table the users if i have a relation like this:
users (id)
roles (id)
user_role (user_id, role_id)
I select the user_id's and now i want the users.
Thank you

You need to set up your relations in User model and Role model. Please refer to the laravel documantation.
https://laravel.com/docs/5.1/eloquent-relationships#many-to-many

Related

Join database table based on Json colum having multiple key

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.
You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

Where do I find the customer ID in a Magento 2 database?

I'm using the CreativeMinds Multi User Accounts plugin. In the cminds_multiuseraccounts_subaccount table it references customer_id and parent_customer_id. Where is the customer_id defined?
You can find customer id in 'customer_entity' Table

Prevent non-exists data insertion - Laravel

assuming that customers table contains 20 customers from id 1 to 20.
Any idea to prevent user submitted customer_id = 21 in invoices table?
User may try to alter the customer_id from html form before submit.
Problem solved, use Laravel "exists" validation.

Relational Ado Without Primary Foreign Key Relation in yii

I have 2 tables. one is groups and other is frames.
groups has id, name, owner as columns.
grouppage has id,name and user_id.
users can have group pages. users can edit page if they are owner of the group .Means user_id = owner. Otherwise users can not edit the group page.
Hence i have a public property called can_edit.this is true if user_id = owner.
now i want to connect these 2 table using relational ADO. But how do i do it? because i have to connect the user_id in grouppage table to owner in groups. Neither owner nor group_id are primary keys?
Any solution /ideas
Thanks to all
Smith

Codeigniter: how should I restructure db schema?

I don't even know if that's the right term.
May it be known that I'm a major novice!
I have three tables: users, profiles, and survey. Each one has user_id as it's first field, (auto-increment for users), and they're all tied by a foreign key constraint, CASCADE on DELETE.
Currently, for each user, let's say user_id 1, they have a corresponding db entry in the other tables. For profiles it lists all their information, and the survey table holds all their survey information.
Now I must change things...darn scope creep. Users need the ability to have multiple survey results. I imagine that this would be similar to a comment table for a blog...
My entire app runs around the idea that a single user is linked to a constraining profile and survey.
How should I structure my db?
How should I design my app's db so that a user can have multiple tests/profiles for the test?
Please assist! Any advice, information and personal-knowledge is appreciated!
Right now the only way I know how to accompany my client is to create a pseudo-user for each test (so unnecessary) and list them in a view table (called "your tests")-- these are obtained from the db by saying: where user_id=manager_id
Right now, survey has a foreign key to user, identifying the user who took the survey. This is good, and does not change.
But add an autoincrement primary key to survey (and to profile, too). Now a user can have multiple surveys. Each individual survey has its key, and its taker is identified by the user foreign key.
To find all surveys for all users:
select a.*, b,*
from user a
join survey b on (b.user_id = a.user_id);
To find all surveys for one user:
select a.*, b,*
from user a
join survey b on (b.user_id = a.user_id)
where a.user_id = some_user_id;
To just get the survey data, just select b.* only. For example:
select b.*
from survey b on
where a.user_id = ( select user_id from user a where a.name = 'Bob' );
Again, all you need to do is add a primary key (which should be an autoincrement) to survey.

Resources