Or whats the way to create a new collection with this 2 models calling them by the slug? that's the other option but I don't know how to do it...
Aug 8, 2020
4
Level 12
Help calling information from products model
Hi, I want to know whats the best way to make this action, I am calling all the products of a model table like this and get this object model
return $products = Product::all();
[
{
"id": 1,
"icon": "fa fa-house-user",
"name": "Client Licence",
"slug": "Client-Licence",
"value": 25,
"income": 0.1,
"description": "A client Licence is an user that only can hire services on book, also can upgrade his account to service provider or to Independent Distributor.",
"created_at": "2020-08-08T23:53:56.000000Z",
"updated_at": "2020-08-08T23:53:56.000000Z"
},
{
"id": 2,
"icon": "fa fa-house-user",
"name": "Distributor Licence",
"slug": "Distributor-Licence",
"value": 90,
"income": 0.45,
"description": "A Independent Distributor Licence is an user that can affiliate Clients, Service Providers and Independent Distributors, also can hire and offer services on book",
"created_at": "2020-08-08T23:53:56.000000Z",
"updated_at": "2020-08-08T23:53:56.000000Z"
},
{
"id": 3,
"icon": "fa fa-users",
"name": "Provider Licence",
"slug": "Provider-Licence",
"value": 45,
"income": 0.25,
"description": "A Service Provider Licence is an user that can hire and offer services on book, also can upgrade his account for Independent Distributor Licence.",
"created_at": "2020-08-08T23:53:56.000000Z",
"updated_at": "2020-08-08T23:53:56.000000Z"
},
{
"id": 4,
"icon": "fas fa",
"name": "new product",
"slug": "new-product",
"value": 43,
"income": 0.45,
"description": "soy un boton de pruebas",
"created_at": "2020-08-08T23:57:08.000000Z",
"updated_at": "2020-08-08T23:57:08.000000Z"
}
]
nothing fancy, it returns all the products , BUT now I want to remove all products EXEPT
Client-Licence - want to call this from its slug no from ID
Provider-Licence - want to call this from its slug no from ID
so what is the correct way to say like, okay got the collection now remove all exept->( 'slug' , Client-Licence )
Any help is well received
Regards Daniel
Level 28
Your approach is bad performance wise. You're querying the database twice when you could call it once just by tweaking your where closes :
Product::orWhere([
'slug' => 'Client-Licence',
'slug' => 'Provider-License'
])->get();
// or a more "verbose" equivalent
Product::where('slug', 'Client-License')
->orWhere('slug', 'Provider-License')
->get();
Please or to participate in this conversation.