Aggregate injections in Laravel - laravel

I have a class to build a report about course registration in Laravel. Following is my class:
class Report {
protected $user;
protected $course;
protected $registration;
public function __construct(User $user, Course $course, Registration $registration) {
$this->user = $user;
$this->course = $course;
$this->registration = $registration;
}
public function build() {
// build report
}
}
Laravel will auto inject the instance of User, Course and Registration Models into Report.
If I need more other Model classes that should be used to build the report, I will need to add more arguments to the constructor of Report.
class Report {
protected $user;
protected $course;
protected $registration;
public function __construct(User $user, Course $course, Registration $registration, Another1 $another1, Another2 $another2, ... ) {
$this->user = $user;
$this->course = $course;
$this->registration = $registration;
}
public function build() {
// build report
}
}
Is this a correct way ?
Does have any other way to aggregate those classes that will be used in the Report class ? Should I use Facade Pattern to refactor it ?
Any help is appreciated.

If you really need that many Model injections it seems highly likely that you need to refactor your code and re-consider how a Report class is constructed.
Instead of Models, learn more about Repositories.
I recommend you also learn more regarding the Single Responsibility principle.
wiki

Since Laravel will inject new instances of these classes, you can consider doing this instead:
public function __construct()
{
$this->createInstances();
}
protected function createInstances()
{
$this->user = new User;
$this->course = new Course;
$this->registration = new Registration;
...
}
EDIT:
Or this to resolve any dependencies of these classes:
protected function createInstances()
{
$this->user = $this->app->make('User');
$this->course = $this->app->make('Course');
$this->registration = $this->app->make('Registration');
...
}

Related

How to use laravel repository pattern searchable array?

I am using laravel-repository pattern ,i have one api which is responsible for getting all users it's working fine ,if we are using that package by default search should support for that i set $fieldSearchable array in the repository class.
i hit an api like this localhost.com/api/lists?search=foo,it's not working can you please help me where did i mistake
UserController.php
public function __construct(UserRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function getUsers(){
$data = $this->repository->show();
return response()->json(fractal($data, new UserTransformer()));
}
UserRepositoryInterface.php
interface UserRepositoryInterface extends RepositoryInterface
{
public function show();
}
UserRepository.php
<?php
namespace App\Repositories;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\User as AppUser;
use App\UserSection;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\DB;
/**
* Class UserRepositoryEloquent.
*
* #package namespace App\Repositories;
*/
class UserRepository extends BaseRepository implements UserRepositoryInterface
{
protected $fieldSearchable = ['phone_number'];
/**
* Specify Model class name
*
* #return string
*/
public function model()
{
return AppUser::class;
}
/**
* Boot up the repository, pushing criteria
*/
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
public function show(){
return $this->model()::get();
}
}
It maybe resolved by utilising pre-difined methods No need to write show() function logic because by default l5-Repository pattern contains some methods to get all the data all()or paginate().in your controller write like this in getUsers()
$data = $this->repository->all();
or
$data = $this->repository->paginate('25');
all() is for fetch all the data from DB and paginate($limit) is fetch the data per page based on the limit.
if you are using any one of the above mentioned method then automatically search functionality will work

Laravel: use extended controller or Traits or something else?

To maintain my Laravel application and save myself from a lot of duplicate code I have made the following solution:
BaseController
class BaseController extends Controller
{
public function get($id){
return $this->baseService->get($id);
}
public function getAll(){
return $this->baseService->getAll();
}
}
BaseService
class BaseService
{
protected $model;
public function __construct($model){
$this->model = $model;
}
public function get($id){
return response()->json($this->model->where('id', $id)->first());
}
public function getAll()
{
return $this->model->get();
}
}
MyController
class MyController extends BaseController
{
protected $model;
protected $baseService;
public function __construct(){
$this->model= new Model();
$this->baseService = new BaseService($this->model);
}
/**
* This controller has all the functionality from BaseController now
*/
}
What I'm wondering if this is a good method. Should I stick with this or should I use a different approach? I've heard about Traits but not sure if they are doing the same thing. It's Laravel 5.5 I'm using.
Yes, traits are used to move methods out of a controller regularly. A good example that the Laravel framework uses is the ThrottlesLogin trait. Take a look at https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Auth/ThrottlesLogins.php#L20
to see how the methods are moved outside of a controller but can be still accessed by importing the trait using the use keyword.
While traits would work for your use case I wouldn't use them here for the functionality you are looking for. I would use the repository pattern. It would better separate your code and make it more reusable.
Take a look at https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/ for more information on the repository pattern. Basically, you would separate your code into a separate repository and use Laravel's built in IoC to inject the repository into your controller.
MyController
class MyController extends Controller
{
protected $repo;
public function __construct(MyRepository $myRepository)
{
$this->repo = $myRepository;
}
public function index()
{
$myStuff = $this->repo->all();
}
// you can also inject the repository directly in the controller
// actions.
// look at https://laravel.com/docs/5.5/controllers#dependency-injection-and-controllers
public function other(MyRepository $repo)
{
$myStuff = $repo->all();
}
}
This is the perfect use case for a Trait. Traits are intended for reusable functions. They're super simple to implement, and won't take more than a few minutes to change what you have.
Here is a great article on them: https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5

Laravel 5.4 relationships with all()

I have two tables, QA and QACategories.
QA has the usual fields (increment etc) and also a field category_id.
QA categories has the usual plus a field "category".
The model for QA is:
class QandA extends Model
{
protected $table = 'qa';
public function category()
{
return $this->hasOne('QACategories::class');
}
}
and the QACategories is
class QACategories extends Model
{
protected $table = 'qacategories';
public function question()
{
return $this->hasMany('QandA::class');
}
}
All I want to do is return them all from a controller and pass them to a view with the category. If I do
class QandAController extends Controller
{
public function Datatable()
{
$questions = QandA::all();
dd($questions);
return view('datatables.qa',['questions'=>$questions]);
}
}
(I have referenced the QA class. If I use the code above the dd is fine, but when I try to add ->category in anyway I am told
Property [category] does not exist on this collection instance.
Help, please! I know it is something very stupid.

Eloquent : delete rows from multiple table with same id

I am a bit new to Laravel. I am trying to delete a project from a table along with its images and plans from 2 other tables. How to do this in Laravel Eloquent?
Here is the delete controller of the project:
public function destroy($id)
{
$project = Projects::findOrFail($id);
$project->delete();
return Redirect::to('admin/view-project')->with('message', 'Project deleted successfully');
}
How can I get this to be done from the model? I didn't understand that.
Here is the Projects model:
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
protected $table = 'project_info_arabic';
public function projectImages()
{
return $this->hasMany('ProjectsImage');
}
public function projectPlans()
{
return $this->hasMany('ProjectsPlans');
}
}
Can kindly anybody help?
The better way when thinking about data consistency is the implementation of foreign keys on the database layer. That does it automatically for you and you don't need to think about it anymore.
See https://laravel.com/docs/5.3/migrations#foreign-key-constraints
You could try model events in laravel.Check this
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
public static function boot()
{
parent::boot();
Projects::deleted(function($project)
{
$project->projectImages()->delete();
$project->projectPlans()->delete();
});
}
}

Symfony2 Use Doctrine in Service Container

How do I use Doctrine in a service container?
The Code just causes an error message "Fatal error: Call to undefined method ...::get()".
<?php
namespace ...\Service;
use Doctrine\ORM\EntityManager;
use ...\Entity\Header;
class dsdsf
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function create()
{
$id = 10;
$em = $this->get('doctrine')->getEntityManager();
$em->getRepository('...')->find($id);
}
}
services.yml
service:
site:
class: ...\Service\Site
According to your code, you already have an EntityManager injected. You don't need to call $em = $this->get('doctrine')->getEntityManager() — just use $this->em.
If you don't inject an EntityManager already, read this.
UPDATE:
You need to make the container inject an EntityManager into your service. Here's an example of doing it in config.yml:
services:
your.service:
class: YourVendor\YourBundle\Service\YourService
arguments: [ #doctrine.orm.entity_manager ]
I prefer to define bundles' services in their own services.yml files, but that's a bit more advanced, so using config.yml is good enough to get started.
For easily accessing the Entitymanager use the following one:
//services.yml
your service here:
class: yourclasshere
arguments: [#doctrine.orm.entity_manager]
And in the class itself:
class foo
{
protected $em;
public function __construct(\Doctrine\ORM\EntityManager $em)
{
$this->em = $em;
}
public function bar()
{
//Do the Database stuff
$query = $this->em->createQueryBuilder();
//Your Query goes here
$result = $query->getResult();
}
}
This is my first answer so any comments are appreciated :)
Please try this code:
$em=$this->container->get('doctrine')->getEntityManager();
$rolescheduels=$em->getRepository('OCSOCSBundle:RoleScheduel')->findByUser($user->getId());
For Symfony 3.x
The most easy-peasy solution for me was to just turn on autowiring/autoconfiguring, and then injecting the service I needed via the constructor. Note that I have also allowed any controller to be injected as a service by setting resource: '../../src/AppBundle/*'
#services.yml or config.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
# Allow any controller to be used as a service
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests,DataFixtures,Form}'
Then in any service, you can inject & use the entity manager $em (or any other service/controller) via the constructor like this:
// class xyz
private $em;
// constructor
public function __construct(\Doctrine\ORM\EntityManager $em) {
$this->em = $em;
}
public function bar() {
//Do the Database stuff
$query = $this->em->createQueryBuilder();
//Your Query goes here
$result = $query->getResult();
}
for anyone who works with symfony3: u need to do the following inside config/services.yml in order to use doctrine in Service Container:
servicename_manager:
class: AppBundle\Services\MyServiceClass
arguments: [ "#doctrine.orm.entity_manager" ]
in the Symfony 3.4. If you want to use Doctrine in a service you can do it:
Only this method worked for me
services.yml:
YourBundle\PatchService\YourService:
public: true
arguments: [ '#doctrine.orm.entity_manager' ]
Service:
class YourService
{
private $em;
public function __construct($em) {
$this->em = $em;
}
Controller:
use YourBundle\PatchService\YourService;
/**
* #Route("/YourController/",name="YourController")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$Notification = new YourService($em);
I am using Symfony 3.4. If you want to create a service in a bundle this works for me:
services:
Vendor\YourBundle\Service\YourService:
arguments:
$em: '#doctrine.orm.entity_manager'
In your Service.php
<?php
namespace Hannoma\ElternsprechtagBundle\Service;
use Doctrine\ORM\EntityManager;
use Hannoma\ElternsprechtagBundle\Entity\Time;
class TimeManager
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
}
Since 2017 and Symfony 3.3 you can register Repository as service, with all its advantages it has.
Your code would change like this.
1. Service configuration
# app/config/services.yml
services:
_defaults:
autowire: true
...\Service\:
resource: ...\Service
2. Create new class - custom repository:
use Doctrine\ORM\EntityManagerInterface;
class YourRepository
{
private $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(YourEntity::class);
}
public function find($id)
{
return $this->repository->find($id);
}
}
3. Use in any Controller or Service like this
class dsdsf
{
private $yourRepository;
public function __construct(YourRepository $yourRepository)
{
$this->yourRepository = $yourRepository;
}
public function create()
{
$id = 10;
$this->yourRepository->find($id);
}
}
Do you want to see more code and pros/cons lists?
Check my post How to use Repository with Doctrine as Service in Symfony.

Resources