Answer the question
In order to leave comments, you need to log in
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');
}
}
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);
}
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;
}
}
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);
}
}
Answer the question
In order to leave comments, you need to log in
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);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question