A
A
andressc2019-04-16 00:34:43
Software design
andressc, 2019-04-16 00:34:43

Where to place logic in Laravel?

Hello! Please don't hit me hard. I am new to Laravel. Before that, I had nothing to do with frameworks at all. I got my first impression of him. I more or less understand the simplest basis. But I don't understand one thing. Where to store logic, business logic? (For me, logic and business logic is an algorithm that takes data and writes it to the database along the way, maybe doing something with it sometimes. well, not only writes, but also pulls it out, etc. Yes, I'm most likely wrong)
I read articles, videos, etc. Everywhere there are some contradictions that I can’t decide how to more or less do it right. The project is not the easiest (an online store), so I think it's not worth writing in controllers. And where all the same to write logic? DB calls, etc. so that it is not very difficult to understand. In models? I don't think so. I dug up the opportunity to do everything through services, service containers, service provider. There methods to prescribe, well, something like select everything from the database, save, etc. And keep everything there. And in the controller, just access these methods. I read about the repositories. I don't want to think about them. The brain melted from them.
What I dug up, I want to ask if this is a normal application structure or a collective farm? Is it possible to do this without big problems from this? ps I will mainly support the application myself, but I want everything to be more or less correct.
It will develop over time, so I would not like to get a rake on my forehead after a while.
HERE IS CODE EXAMPLE
one service provider. To shove everything into it, for example, or you can do several

class SaveStrServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton('App\Helpers\Contracts\SaveStr','App\Helpers\SelectUsers');       
    }
}

here is their contract, for example, to make several by model names, for example, roughly speaking
namespace App\Helpers\Contracts	;
use Illuminate\Http\Request;
  
interface SaveStr {
  public static function select($type);	
  public static function selectuser($id);
  public static function selectpurchase($id);
}

here is the executor class, which is tied to the contract (they can also be divided into several, depending on the actions. Well, for example, to divide the area of ​​responsibility)
namespace App\Helpers;

use App\Helpers\Contracts\SaveStr;
use App\Order;
use App\Good;
use App\User;
use App\Purchase;
use App\Status_order;
use App\Status_purchase;
use App\Page;

class SelectUsers implements SaveStr {
  public static function select($type) {
    $pages = Page::where('type','=', $type)->get();
    return $pages;
  }
  
  public static function selectuser($id) {
    $users = User::with(['orders.purchase','orders.status_order','orders','orders.goods','orders.purchase.status_purchase'])->where('id','=',$id)->get();
    return $users;
  }
  
  public static function selectpurchase($id) {
    $purchases = Purchase::with(['orders','orders.status_order','orders.goods','status_purchase','orders.user'])->where('id','=',$id)->get();
    return $purchases;
  }	
}

well here is the controller
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Helpers\Contracts\SaveStr;

use App\Order;
use App\Good;
use App\User;
use App\Purchase;
use App\Status_order;
use App\Status_purchase;

use App\Admin_role;
use App\Admin;
use App\Banner;
use App\Category;
use App\City;
use App\Point;
use App\User_role;
use App\Page;

class TestController extends Controller
{
    public function index(SaveStr $saveStr)
    {
        $purchase = $saveStr->selectpurchase(2);
        dump($purchase);
    }
}

This is just an example and the question is, is it possible to do this? Is it possible to write everything there? All calls to the database and other logic. And in the controller, use the methods from there and already pass the collection to the view. Will it be normal or is the collective farm deep?
Well, or all the same, explain to me in a simpler way how to do everything, so that later I myself would not get confused in all this. The option that I described seems simple to me to understand. But is this really the case? I don't know what to do or how to start.
THANKS IN ADVANCE FOR YOUR ANSWERS!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmitriy, 2019-04-16
@dmitriylanets

the direction is correct
in this example SelectUsers is a hodgepodge of
this UserRepository and PurchaseRepository
according to the principle of single responsibility, it is necessary to distribute these two classes
, respectively, the controller will be

class TestController extends Controller
{
 protected $purchaseRepository;
   public function __construct(PurchaseRepositoryInterface $purchaseRepository){
$this->purchaseRepository = $purchaseRepository;
   }
    public function index()
    {
        $purchase = $this->purchaseRepository->selectpurchase(2);
        dump($purchase);
    }
}

to organize business logic, you can into services, but again, these services are conveniently generated from use cases and not from entities, so the following classes and methods are formed:
Shopping->addItemToShoppingCart()
Shopping->remoteItemFromShoppingCart()
Shopping->checkout()
Searching ->searchByAuthor()
Searching->advancedSearch()
or for each action its own class, pattern command
Shopping/AddItemToShoppingCart
Shopping/RemoteItemFromShoppingCart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question