i have three tables, products , discount ,and dicount_products.
the discount table have the discount data, and the discount_products have the id's of the product and the discount tables, to make a connection bettwen them.
in my model products , am trying to accsess the discount throught the relationship table by doing this
//the discount relationship table
public function discount_list(){
return $this->hasOne(Discount_products::class,'product_id','id');
}
//getting the discount throught the discount_list
public function discount(){
return $this->discount_list()->discount();
}
the Discount_products model
public function discount(){
return $this->belongsTo(Discounts::class,'discount_id','id');
}
PS: i know i can do this in the controller but am trying to make discount accessble in one step.
Related
Distributor Table - id
Product Table - id
Distributorprocuct Table - distributor_id, product_id
Distributor Model -
public function product() { return $this->hasMany(Distributorproduct::class); }
Product Model -
public function distributor() { return $this->hasMany(Distributorproduct::class); }
Distributorproduct Model -
public function distributor() { return $this->belongsTo(Distributor::class); }
public function product() { return $this->belongsTo(Product::class); }
If I write $product->distributor then it gives me all the details of distributorproduct but i need the details of distributor not distributorproduct .
If i write $distributor->product then it gives me all the details of distributorproduct but i need the details of product not distributorproduct .
Thanks in advance...
It is not clear to me what is the relationship between Product, DistributorProduct and Distributor, but I think what you want is to specify a different kind of relationship between Distributor and Product (and Product -> Distributor).
In your Distributor modal class you can try defining your product method like this:
public function product() {
return $this->hasOneThrough(Product::class, Distributorproduct::class);
}
and the distributor method in the Product modal class like this:
public function distributor() {
return $this->hasOneThrough(Distributor::class, Distributorproduct::class);
}
But your mileage may vary, this kind of relationship is ok only if a Product has a single distributor (likely), and a distributor has a single Product (unlikely I guess).
The Laravel docs have a pretty good documentation about relationships. Looks especially at the Has One Through and the Has Many Through paragraphs.
I have the following relation:
RAB hasMany products.
products belongsToMany RAB
but this products also hasMany currency that belongsToOne RAB itself.
it looks like this.
in RAB model:
public function products()
{
return $this->belongsToMany('App\ModelKeuangan\ProductsModel', 'rab_products', 'rab_id', 'products_id');
}
in products model:
public function rab()
{
return $this->belongsToMany('App\ModelUnitKerja\Perencanaan\RabModel', 'rab_products', 'products_id', 'rab_id');
}
rab_products is my intermediate/join table.
it works just fine when im syncing products data for RAB. but i cant get how to make eloquent model for syncing Currency data to rab and products.
do i need to make model for currency too? if yes, how do i define it?
my plan is make a pivot like this, but can i make relation inside pivot table?
class RabProducts extends Pivot {
//relation function to currency
}
and change my products model something like:
public function rab(){
return $this->belongsToMany('App\ModelUnitKerja\Perencanaan\RabModel')->using('App\RabProducts');
}
Create the currency model and add foreign_key product_id:
php artisan make:model Currency
Build the one-to-many between Product and Currency
and you need to add product_id in your currencies table.
In your Currency Model:
public function product() {
return $this->belongsTo('App\Product', 'product_id');
}
In your Product Model:
public function currencies()
{
return $this->hasMany(\App\Currency::class, 'product_id', 'id');
}
So that you can call it like this:
RAB::with(['products' => function($query) {
$query->with('currencies');
}])->get();
Unfortunately, the foreign_key between products and rabs is in pivot table, you cannot create the hasmanythrough with currency directly.
You can call the currency by products. Or you can create a pivot model, and then use hasmanythrough.
I'm building a platform where a user can sign up and add products to sell. Also another user can buy products listed. When a buyer place an order, i want it to be seen by a seller(user) who listed the product. Right now all users can see all orders even if they are not sellers.
I have no idea how to proceed but here is my
order function in product controller
//Orders View Function
public function viewOrders(){
$orders = Order::with('orders')->orderBy('id','Desc')->get();
return view('orders')->with(compact('orders'));
}
Any help will be appreciated.
Your viewOrders action should be hidden behind authentication process (obviously, user has to first sign in before he can view his orders). Once you do that (using auth middleware for it's route), Laravel can resolve authenticated user for you - you can simply hint it as a parameter of your viewOrders action:
public function viewOrders(User $user)
{
$orders = $user->orders()->orderBy('id', 'desc')->get();
return view('orders')->with(compact('orders'));
}
As you notice, here I've modified your query a little bit. Instead of selecting all orders we are now selecting them through authenticated user's relation to it's orders. Orders obviously belong to user, but through products (as an intermediate) - one user can have many products, one product can have many orders, thus one user can have many orders. In Laravel this kind of relationship between eloquent models is called HasManyThrough. Therefore you should declare it in your User model:
class User extends Model {
...
public function orders()
{
return $this->hasManyThrough(Order::class, Product::class);
}
}
Note that your Product will probably also have orders() relation, but it will be of HasMany type instead since it's a direct one without any intermediates.
Filter your query base on your needs
Example:
Model Relationship
public function orders(){
return $this->hasMany(Order::class);
}
public function getMyOrders(User $user){
$orders = Order::with('orders')->orderBy('id','Desc')->where('user_id','=', $user->id)->get();
return view('orders')->with(compact('orders'));
}
To get sellers Orders
public function getSellerOrders(User $user){
$products = Product::where('user_id', '=', $user->id)->get();
$orders = [];
foreach($products as $product){
array_merge($orders, $product->order);
}
return view('orders')->with(compact('orders'));
}
In your DB table for orders you are suggested to have 2 IDs, one which denotes Buyer and Other which denotes Seller, i.e., Whenever a user places an order, then it must be a buyer then insert it's(buyer) ID into buyer_id in Order's table, similarly the product_id for denoting produc from Products' Table should be there and from Product's Table you can get seller_id which is suggested to be there for denoting which seller owns a particular product. Insert seller_id into Order's Table and use it to your query as:
$seller = Order::where('seller_id',$variable_for_product_id)->with('orders')->get();
Do this for all sellers in your Order's Table
I'm using Larvel 5.3 and I have four tables I wish to set up relations with.
- Users
- Orders
- Products
- Brands
An order belongs to a user and a user has many orders.
A product belongs to many orders and additionally, to one brand.
A brand has many products.
I'm trying to find a way to get a user's order, with the products AND the brand of the products.
User.php
public function orders(){
return $this->hasMany('App\Order');
}
Order.php
public function user() {
return $this->belongsTo('App\User');
}
public function products(){
return $this->hasMany('App\Product');
}
Product.php
public function orders() {
return $this->belongsToMany('App\Orders');
}
public function brand(){
return $this->belongsTo('App\Brand');
}
Brand.php
public function products() {
return $this->hasMany('App\Flavour');
}
I wish to get back an array containing the users orders, along with each orders products and the products' brand.
Could anyone advise?
First, Products and Orders have a many to many relationship.
public function products(){
return $this->belongsToMany('App\Product');
}
Get a user's order first. Because users have many orders, you need to specify which order you want to view the products list from.
$order = $user->orders->first();
Then get all products and their brand
foreach($order->products) ...
For your super array, you can try Eager Loading
$arr = User::with('orders.products.brand')->get(); //Not tested
I have 3 Tables
Product:[Table]
Person:[Table]
Payment:[Table]
Many To Many Relationship Between Product and Person
One To Many Relationship Between Product and Payment (One Product Has Many Payments)
One To Many Relationship Between Person and Payment (One Person Has Many Payments)
Payment:[Table]
id
person_id
product_id
amount
The thing is that i am trying to get All persons with products and Product payments filtered by person_id.
Reason is that i dont want to have any other persons record in payments.
This is actually the query i am running yeah i know its wrong cuz i cant filter it by person_id.
$query = $person::with('product', 'payment')->where('is_active', 1);
I want to achieve something like this..
$query = $person::with(array('product', 'payment' => function ($query) {
$query->where('person_id', '=', 'person.id');
}))->where('is_active', 1);
If you setup your relations like:
class Person extends Model
{
public function payments()
{
return $this->hasMany(Payment::class);
}
}
class Payment extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
Then you should be able to do:
$person = Person::with('payments.product')->where('id',$personId)->where('is_active',1)->first();
Which will return a Person with all the relations loaded and you can access those like:
#foreach($person->payments as $payment)
{{$person->name}} bought {{$payment->product->name}} for {{$payment->amount}}
#endforeach