when to create a service in laravel

Solutions on MaxInterview for when to create a service in laravel by the best coders in the world

showing results for - "when to create a service in laravel"
Aitana
20 Sep 2018
1<?php
2
3namespace App\Services;
4
5use Illuminate\Support\Collection;
6use Illuminate\Session\SessionManager;
7
8class CartService {
9    const MINIMUM_QUANTITY = 1;
10    const DEFAULT_INSTANCE = 'shopping-cart';
11
12    protected $session;
13    protected $instance;
14
15    /**
16     * Constructs a new cart object.
17     *
18     * @param Illuminate\Session\SessionManager $session
19     */
20    public function __construct(SessionManager $session)
21    {
22        $this->session = $session;
23    }
24
25    /**
26     * Adds a new item to the cart.
27     *
28     * @param string $id
29     * @param string $name
30     * @param string $price
31     * @param string $quantity
32     * @param array $options
33     * @return void
34     */
35    public function add($id, $name, $price, $quantity, $options = []): void
36    {
37        $cartItem = $this->createCartItem($name, $price, $quantity, $options);
38
39        $content = $this->getContent();
40
41        if ($content->has($id)) {
42            $cartItem->put('quantity', $content->get($id)->get('quantity') + $quantity);
43        }
44
45        $content->put($id, $cartItem);
46
47        $this->session->put(self::DEFAULT_INSTANCE, $content);
48    }
49
50    /**
51     * Updates the quantity of a cart item.
52     *
53     * @param string $id
54     * @param string $action
55     * @return void
56     */
57    public function update(string $id, string $action): void
58    {
59        $content = $this->getContent();
60
61        if ($content->has($id)) {
62            $cartItem = $content->get($id);
63
64            switch ($action) {
65                case 'plus':
66                    $cartItem->put('quantity', $content->get($id)->get('quantity') + 1);
67                    break;
68                case 'minus':
69                    $updatedQuantity = $content->get($id)->get('quantity') - 1;
70
71                    if ($updatedQuantity < self::MINIMUM_QUANTITY) {
72                        $updatedQuantity = self::MINIMUM_QUANTITY;
73                    }
74
75                    $cartItem->put('quantity', $updatedQuantity);
76                    break;
77            }
78
79            $content->put($id, $cartItem);
80
81            $this->session->put(self::DEFAULT_INSTANCE, $content);
82        }
83    }
84
85    /**
86     * Removes an item from the cart.
87     *
88     * @param string $id
89     * @return void
90     */
91    public function remove(string $id): void
92    {
93        $content = $this->getContent();
94
95        if ($content->has($id)) {
96            $this->session->put(self::DEFAULT_INSTANCE, $content->except($id));
97        }
98    }
99
100    /**
101     * Clears the cart.
102     *
103     * @return void
104     */
105    public function clear(): void
106    {
107        $this->session->forget(self::DEFAULT_INSTANCE);
108    }
109
110    /**
111     * Returns the content of the cart.
112     *
113     * @return Illuminate\Support\Collection
114     */
115    public function content(): Collection
116    {
117        return is_null($this->session->get(self::DEFAULT_INSTANCE)) ? collect([]) : $this->session->get(self::DEFAULT_INSTANCE);
118    }
119
120    /**
121     * Returns total price of the items in the cart.
122     *
123     * @return string
124     */
125    public function total(): string
126    {
127        $content = $this->getContent();
128
129        $total = $content->reduce(function ($total, $item) {
130            return $total += $item->get('price') * $item->get('quantity');
131        });
132
133        return number_format($total, 2);
134    }
135
136    /**
137     * Returns the content of the cart.
138     *
139     * @return Illuminate\Support\Collection
140     */
141    protected function getContent(): Collection
142    {
143        return $this->session->has(self::DEFAULT_INSTANCE) ? $this->session->get(self::DEFAULT_INSTANCE) : collect([]);
144    }
145
146    /**
147     * Creates a new cart item from given inputs.
148     *
149     * @param string $name
150     * @param string $price
151     * @param string $quantity
152     * @param array $options
153     * @return Illuminate\Support\Collection
154     */
155    protected function createCartItem(string $name, string $price, string $quantity, array $options): Collection
156    {
157        $price = floatval($price);
158        $quantity = intval($quantity);
159
160        if ($quantity < self::MINIMUM_QUANTITY) {
161            $quantity = self::MINIMUM_QUANTITY;
162        }
163
164        return collect([
165            'name' => $name,
166            'price' => $price,
167            'quantity' => $quantity,
168            'options' => $options,
169        ]);
170    }
171}
172
Cristina
23 Aug 2017
1use App\Services\Transistor;
2
3$this->app->resolving(Transistor::class, function ($api, $app) {
4    // Called when container resolves objects of type "HelpSpot\API"...
5});
6
7$this->app->resolving(function ($object, $app) {
8    // Called when container resolves object of any type...
9});
Constantine
04 Mar 2016
1cartService
queries leading to this page
laravel call servicelaravel inject into viewservice container in laravel examplelaravel app servicelaravel service provider and service containerservice container in laravel with examplewhat is laravel service containerlaravel service class examplelaravel 8 create serviceregister class to container laravelservices laravelwhy we use service provider in laravelservice class laraveluse service laravelcreate your own service container laravelregister class to bind laravellaravel constructor injection uselaravel call service provider in controlleruse laravel service container in other applicationlaravel bind data on migrationlaravel getting servieswhat is a service container in laravel 3f 2aservice provider and service container in laraveldefine service provider in laravellaravel service 5claravel how to dependency inject classlaravel make service providerdefine laravel serviceservice provider laraveluse of service container laravellaravel service provider examplelaravel create service providerlaravel create service classwhere we are use service container in laravellaravel how to create custom service functionslaravel service containers explainedbind method laravelwhat is laravel servicelaravel app makewithhow to make binding in laravellaravel make new service as modellaravel service providerbinding in laravellaravel 8 use serviceslaravel service container example for registerlaravel get service in controllerlaravel 8 create new service classhow to crate instance in laravelarchitecture of service containers in laravelcheck service container classes laravellaravel service containersservice pattern laravellaravel create service and inject to controllerhow to make a service in laravellaravel serviceslaravel bind invoke settterwhen to use the container laravelcreate services laravelhow to make my own call service in laravel service container in a service layer laravelaravel bind parameter to return class namelaravel app bindhow to call service in model in laravellaravel create service containercreate service container in laraveluse service provider laravel in bladewhat is a service container in laravel 3fcreate service provider in laravellaravel service container and facadelaravel service layerlaravel service container bindingcreate service provider laravelwhen to create laravel service containerlaravel service layaerservice container in laravel 3fmake new service in laravelwhat is a service laravelcreate a new service in laravelcontoller call services laravelmake services in laravelwhat is the laravel service containerphp service containerlaravel service classes tutorialcreate service in laravellaravel app 28 29laravel service container vs service providerlaravel bind class with view name 24this 3eapp 3einstance 28laravel service exampleservice container laravel examplewhen to use service container in laravelservice layer laravelapp 28 29 3ehas laravelsc create service laravelcreate new service laravellaravel service class patternwhat is service container and service provider in laravellaravel resolveapp make in laravelcreate service command in laravellaravel app replace servicewhy laravel service containers are cool 3flaravel with containerwhat is a service provider in laravellaravel service container explainedlaravel write servicecreate a laravel servicehow to use service container in laravelwhat is service class and service method in laravelcreate servicers laravellaravel add service to aplaravel dependency injection vs service containerapp 3eapp 3esingleton get first registerlaravel 5 service container exampleservice container laravellaravel service provider injectlaravel create new services classlaravel service provider get service containerlaravel use serviceshow to make service in laravel 8how to create service in laravelcreate service laravelcreate new service in laravellaravel 8 create new servicemake service in laravel via artisanservice provider and service container laravelhow to create services in laravellaravel create service commandlaravel create service class commandwhen i use service container laravelwhat is the use of service container in laravelhow write service for controller laravel laravel create service provider examplehow to use service provider in laravel step by stephow to create service inside module laravelapp service provider laravelhow to get a instance in laravelhow to use a service provider in laravellaravel service container binding exampleexample of dependency injection in laravelwhat is service in laravellaravel 8 service container examplelaravel how to create servicephp artisan make 3aservicecreate service container in laravel 8service container laravellaravel lumen create servicelaravel inject servicelaravel use service provider in controllerservice provider example in laravelservices in laravel 8laravel tutorial service containerservice provider make laravelmake service class laravelmake service in laravellaravel make service commandlaravel service controllerservice container example laravel 8service container in laravelcustom service provider in laraveldependency injection sample in laravellaravel app 3a 3amakeapp container laravellaravel resolve controllerhow to create service class in laravellaravel dependency injection conteiner laravel 8how to use service in laravel 8laravel service pattern exampledependency injection with laravelcreate service in laravel with instancewriting a service provider in laravelmake service laravelhow to create service in laravel 3flaravel call service from modelservice in laravellaravel8 service containerlaravel get app singletonlaravel service container name when to create a laravel serviceservice provider in laravel worksservice classes laravellaravel injectexplain laravel e2 80 99s service containerlaravel service container examplelaravel service container tutorialhow to use service provider in laravellaravel get service in service providerhow to use service in laravellaravel call service containerwhat is the laravel service container for 3flaravel app 3ebindwhats is service provider in laravel what is service container in laravel 3flaravel 27s service container 3flaravel app service providerhow to add services in laravellaravel make serviceservice containers laravelcall service function laravel controllerlaravel construct servicecreating services in laravellaravel application as servicelaravel service patternlaravel service classlaravel create a service provider and containerhow to create service container in laravelhow to create service laravelwhat is laravel service classcreate a service provider laravelwhat is a service provider laravelservice container vs service provider laravelhow to create custom service provider in laravelwhat is a service in laravelhow to use app service provider in controller laravellaravel what is a servicelaravel decorate service providerlaravel service container example with repositorylaravel service container patternservice container and provider in laravelservice provider in laravellaravel create app instancelaravel injection singleton container laravel 8laravel resolve class dependencieswhat is service provider in laravellaravel service container with exampllaravel create new service calsslaravel create servicelaravel create a service providerservice container laravel in simplewhy we use service provider unless use to controller in laravelapp 3esingleton get firstwhich pojrect need laravel service containerlaravel 7 5 make servicehow to get a class instance in laravelphp laravel servicelaravel service provider bindhow to create a service in laravelhow to make service in laravelservice container laravel explainedwhat laravel service containersservice container laravel in easy way construct userrepository laravel 8how to add service provider in laravelhow to utilize service containers in laravelcustome service in laravel 8laravel create crate new ap instanceservice container laravel examplehow to work laravel dependency injection laravel make 3aservicelarave lx jet bindlaravel service container conceptlaravel use service in controllerwhat is service container laravellaravel service provider samlhow to start laravel servicelaravel 8 serviceslaravel make from containerwhat is service container in laravel 3fhow implement service pattern laravellaravel make appcreate service laravellaravel resolverdependency injection laravel controllerlaravel service container fileservice containerlaravel 8 bind interface to implementationcreate service in laravel with instance in larave 3b laravel development serviceservice container in service layer laravellaravel dependency injection service providerlaravel what is container used forlaravel service createlaravel 8 servicelaravel what is a service container bindinglaravel serviceworkerservice provider laravel examplelaravel 27s service containercreate service class in laravelservice contract in laravellaravel custom servicewhen to use laravel service containerlaravel service container and service providerlaravel singleton serviceapp bind laravelwhat is laravel service container and provider with exampleservice container structure laravellaravel 5 service containerservices in laravelhow to call service in model laravelhow to create a service in laravel artisancreate services in laravelwhen should we use service container in laravelhow to get service class in laravel controlleruse of service container in laraveldependency injection laravel laravel get servicewhat is the reason of use laravel service containerlaravel make service classlaravel add service to appwhen to create a service in laravelcreating your own service provider in laravelmake your own service container laravelcreate service in laravel 8laravel artisan make 3aserviceservice container in laracastservice container in laravelapp service laravelwhat is service container in laravellaravel use applaravel generate servicelaravel service pattern laravel dailylaravel services examplelaravel call a servicecontroller to service laravelwhat is service container in laravel with exampleregister new service laravelcreating a laravel serviceproviderlaravel singletonlaravel service container nedirlaravel service provider vs service containerhow the service container work in laravellaravel app make interface of classlaravel create servicessingleton laravelapp 28 29 laraveldependency injection laravel examplelaravel location of container phplaravel get service from containerwhen to use service provider in laravellaravel containershow to make services in laravelcall service provider laravellaravel how to create a service containerlaravel resolve methodlaravel add service to service providelaravel when to use service containerlaravel services explainedcreate a service class in laravellaravel service containerservice laravellaravel serviceservice container example in laravelwhen to create a service in laravel