belongsToMany with nullable foreign - Laravel 5.8 - laravel

in my Laravel project i get this database structure:
Products
Id
Name
Orders
Id
Total
Order_Product
Product_id (nullable)
Order_Id
Details
In my Order model I make belongsToMany retaltionship with Product model:
public function products() {
return $this->belongsToMany(Product::class)->withPivot('Details');
}
The Problem is when I try to get the Order Products Collection
$order->products();
I don't get rows with nullable product_id, Any solution please ? Thank you.

Laravel support "Null Object Pattern" since version 5.5 which allows you to define a default model that will be returned if the given relationship is null.
Try using the following code:
public function products() {
return $this->belongsToMany(Product::class)->withDefault()->withPivot('Details');
}

Most likely, you don't get "nullable" products because you have a relation Order->Products. When you call $order->products(), eloquent tries to get all Product entities that are connected to your Order via product_id field. So, if field is empty, then you can't get Product because there is no connection.
One of the solutions is:
create another entity like OrderLine(Order_Product table); add methods like $orderLine->details() and relation to Product like $orderLine->product()
add relation to OrderLine inside Order - $order->line() or $order->info() etc. -> Order hasMany OrderLine
then use $order->line() to get an order's details and products (if exists)
p.s. I've compiled it in my head so it might need some code adjustments.
Good luck

Related

Laravel Relationships, don't know how to display

I have two models. Business and City.
Business:
id
title
-some columns--
city_id
City:
id
name
How to display the city name, when I get business data to view
I was able to display cities using the laravel voyager lessons
When I want to get it like $business->city_id
If you are using models, you can create a relationship by adding hasOne or hasMany to your model codes.
When you call it from your controller, you can call the relationship you wrote in your model with the 'with' query.
Model
public function city()
{
return $this->hasOne(City::class,'id','cityid');
}
Controller
$business=Business::with('city')->first();
$cityname=$business->city->name;
If you don't use model, you can connect with 'join'
You have 2 options. First, you can get city details on the controller:
Business::with('city')...;
On the blade
$business->city->name;
Other option fetching it on the blade directly. I don't recommend this because of extra database queries.
$business->city()->name;

Laravel HasManyThrough CrossDatabaseRelation

strange question:
I have 3 Models
Order
with id as PK
Orderline
with id as PK and order_id as FK. brand and partnumber are two separatet colums
Article
with combined PK brand and partnumber
**which is on another database **
One Order hasMany Orderlines. Every Orderline hasOneArticle.
i had make a function within order:
public function articles()
{
$foreignKeys = [
'brand_id',
'article_number',
];
$localKeys = [
'brand',
'partnumber',
];
return $this->hasManyThrough('App\Models\Masterdata\Articles','App\Models\Oms\OrderLine',$foreignKeys,$localKeys,'id','id');
}
How can i retrieve all Attributes from articles through Order?
I tried something like this:
$order = Order::find($orderid)->articles();
dd($order);
//did not work
$order = Order::with('orderlines.articles')->where('id','=',$orderid)->get();
Do you have an idea for me?
You can configure more than one database in the database.php config, and specify the $connection name in the Model class.
For the most part, Eloquent doesn't do JOINs, it just does IN statements on the key values from the preceding query, and programmatically marries the results together after the fact. Partly to avoid the mess of keeping table aliases unique, and partly to offer this kind of support- that your relations don't need to live in the same database.
In other words, what you have there should work just fine. If there's a specific error getting thrown back though, please add it to your post.

Eloquent - check if id exists on distant relation

I need to check if a country id exists on a distant relation to a product. I only need a true or false as result. I haven't figured out how to put the query. The different relations are defined in each model:
Product - belongsToMany ShippingProfile
ShippingProfile - hasMany Methods
Method - belongsTo ShippingZone
ShippingZone - belongsToMany Countries
So if I have product id 2, and country id 5, I want to find out if the country id is available in any zone within any of the methods belonging to the profile of the product. (actually it is only one profile per product but it is defined as belongsToMany with a pivot table).
I tried to get access to countries as a first step, but I can't get access to it by using this syntax:
$prod = \App\Product::find(2)
->shipping_profiles()
->methods()->zones()->countries();
I get the error: "Call to undefined method Illuminate\\Database\\Query\\Builder::methods()
(methods() is correctly defined in shippingProfiles though).
Edit2:
Using "with", I can do a "where" check on the countries, but if I provide a non existing country, it doesn't fail - it just returns the product with the properties and a zone with an empty countries array:
return $prod = \App\Product::find(2)->with([
'shipping_profiles.methods.zone.countries' => function($query) {
$query->where('countries.id', 10000);
}])->firstOrFail();
So if you can advice any other method that will return either true/false or return the zone or null, that's what I'm looking for..
Use whereHas():
return $prod = \App\Product::whereKey(2)->whereHas(
'shipping_profiles.methods.zone.countries', function($query) {
$query->where('countries.id', 10000);
})->firstOrFail();

Accessing Related Model inside the mutator

I have question about my problem, so let me explain..
I have these Model which is Product and ProductVariant , the ProductVariant has "belongsTo" relation into the Product model.. and Product model have "hasMany" relation into the ProductVariant
My Question is...
i did createMany for variation from the product which is like
Product->variant()->create($variantProducts);
and i'd like to create some mutator on the ProductVariant model to manipulate one column called "sku".
my problem is, how to get some column value from the mutator to get data from the related model ( Product ) , in this case i want to get the category_id of the Product from the ProductVariant mutator
i try to do this , but it seems doesnt work
public function setSkuAttribut($v){
// product() is belongsTo relation to the product model
$cat = $this->product()->category_id;
dd($cat);
}
but it returns me this error
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$category_id
Any helps would be really appreciated.. thankyou :)
You're calling the product() relation method as a method, which means you're receiving a Builder instance. In order to get the model instance, you'll either have to execute ->product()->get()->category_id, which will make a new query on each occasion or use the lazy eager loading with ->product->category_id which will use an already loaded relation instance if present.

Laravel relationship between two tables

I'd like your input on this.
I have a Customer_table with a field name. I have another table called Reservation_table with a Customer_name field.
How can I relate them in such a way that I'd see all the bookings by the specific customer?
In Reservation_table you should have a field(foreign key) userid in order ta have a user for each reservation.
You can using primary key - foreign key relationship to relate/join those two tables. Also, instead of having a 'Customer_name' field as your FK referring to 'name' field in 'Customer_table' table, it is better to have an id (unique) generated for each customer; This way you can have an efficient way of uniquely identifying and relating customer across tables; can save space on Database side as well. Hope this helps!
If you want to use eloquent you must first define a relationship.
One reservation belongs to a user. Here is how to define the relationships:
Inside the Reservation model:
public function user()
{
return $this->belongsTo('App/User'); //User model
}
To define the inverse you do the following:
Inside User model:
public function reservations()
{
return $this->hasMany('App/Reservation'); // Reservation Model
}
Now you can do the following in your controller:
$reservations = Auth::user()->reservations;
Now you have all reservations by the currently logged in user.
I am not sure if I got the question right so ask away.

Resources