So you want to query from category table to all products with its latest price right?

|
nekooee 2022-06-03 18:27:17
Each time the price changes, I save the new price in a new row in its table. That table is related to the product table.
anujrock1 2022-06-03 18:27:35
nekooee 2022-06-03 18:25:34
Price is not a column. It has a one-to-many relationship with the product.

So you want to query from category table to all products with its latest price right?

Price table is different

nekooee 2022-06-03 18:27:50
Yes
anujrock1 2022-06-03 18:28:07
nekooee 2022-06-03 18:27:17
Each time the price changes, I save the new price in a new row in its table. That table is related to the product table.

Understand, but only first latest price you want

nekooee 2022-06-03 18:28:16
yes
anujrock1 2022-06-03 18:29:11
nekooee 2022-06-03 18:27:50
Yes

See you can add accessor in product table also which will append latest price

Or make a relationship latestOfMany()->first()

On product table with price

nekooee 2022-06-03 18:31:41
anujrock1 2022-06-03 18:29:11
See you can add accessor in product table also which will append latest price

Or make a relationship latestOfMany()->first()

On product table with price

I appended the first latest price. but orderBy doesn’t work with appended attribute. Then I added the HasOne latestOfMany() in product model

nekooee 2022-06-03 18:32:04
final public function latestPrice(): HasOne
{
return $this->hasOne(Price::class)->latestOfMany();
}
anujrock1 2022-06-03 18:32:41
Main question look like this
Category::with([‘product’, ‘product.latestPrice’ => function ($query) {
$query->orderBy(‘price’, desc) ;
}])->get()

Like this

nekooee 2022-06-03 18:33:15
Thanks. I will test this code now.
anujrock1 2022-06-03 18:35:15
nekooee 2022-06-03 18:33:15
Thanks. I will test this code now.

Let me know worked or not

I am assuming that your main modal is category, in which you have relationship with product

And product model has relationship with price

nekooee 2022-06-03 18:36:20
anujrock1 2022-06-03 18:32:41
Main question look like this
Category::with([‘product’, ‘product.latestPrice’ => function ($query) {
$query->orderBy(‘price’, desc) ;
}])->get()

Like this

You got the category here with its products. But I take the products of the category. Because I need to send the products to the front in the form of pagination, and with this method, it is not possible to paginate the products.

anujrock1 2022-06-03 18:38:33
nekooee 2022-06-03 18:36:20
You got the category here with its products. But I take the products of the category. Because I need to send the products to the front in the form of pagination, and with this method, it is not possible to paginate the products.

So you want with product no problem you can

But as I can see there you have used category for main model

nekooee 2022-06-03 18:41:17
This code is in the show method in the category controller. And I get the category like this:
final public function show( Category $category):
nekooee 2022-06-03 18:41:37
then :
$products = $category->products()->orderBy ………. ->paginate(30);
anujrock1 2022-06-03 18:51:15
nekooee 2022-06-03 18:41:37
then :
$products = $category->products()->orderBy ………. ->paginate(30);

Okay you are using route modal binding.

Check dm

JonsM90 2022-06-03 19:41:06
Hi everyone when using input type = “checkbox” in the forech operator, how do I get the name of each item from it?
M 2022-06-03 20:31:30
laravel_discuss-123679.jpg

M 2022-06-03 20:31:43
Always this message
M 2022-06-03 20:31:54
Tooken true
M 2022-06-03 20:32:48
Url localhost/oauth/tokens
M 2022-06-03 21:28:50
laravel_discuss-123684.jpg

M 2022-06-03 21:29:17
I’m getting tokens bu
M 2022-06-03 21:29:46
laravel_discuss-123686.jpg

M 2022-06-03 21:29:59
but it does not validate the token
M 2022-06-03 21:30:31
it gives this error whether the token is correct or not
“message”: “Unauthenticated.”
M 2022-06-03 21:31:05
http://localhost/public/oauth/tokens ı am use this verify address
M 2022-06-03 21:31:17
please helep
M 2022-06-03 21:31:49
I am use Password Grant Token
Asadbek_fayzulloev 2022-06-03 22:34:12
M 2022-06-03 21:31:49
I am use Password Grant Token

Send me routes

Asadbek_fayzulloev 2022-06-03 22:34:57
Did you protect this route with auth middleware?
M 2022-06-03 22:36:26
no I did not add a route
. 2022-06-03 22:37:34
I want course to learn laravel ? Any suggestion
M 2022-06-03 22:37:35
Asadbek_fayzulloev 2022-06-03 22:34:57
Did you protect this route with auth middleware?

I am using localhost/oauth/token url when retrieving token
I’m using the /oauth/tokens url when validating the token

M 2022-06-03 22:38:33
token is formed
but i can’t confirm
|