-
Notifications
You must be signed in to change notification settings - Fork 880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fractal presenters: Manually include relationship presenter #101
Comments
My current solution: _Step 1:_ Create new class that extends use Prettus\Repository\Presenter\FractalPresenter;
abstract class FractalRelationshipsPresenter extends FractalPresenter
{
public function parseIncludes($includes = array())
{
$request = app('Illuminate\Http\Request');
$paramIncludes = config('repository.fractal.params.include','include');
if ( $request->has( $paramIncludes ) ) {
$includes = array_merge($includes, explode(',', $paramIncludes));
}
$this->fractal->parseIncludes($includes);
return $this;
}
} _Step 2:_ Create new class that extends use Prettus\Repository\Eloquent\BaseRepository;
abstract class PresentableRepository extends BaseRepository
{
public function present($relations)
{
$this->presenter->parseIncludes($relations);
return $this;
}
} _Step 3:_ Update your Presenters and Repositories to extend the new classes in previous steps (I'm assuming a Transformer was already created with all desired relationships): class MyPresenter extends FractalRelationshipsPresenter
{
public function getTransformer()
{
return new MyTransformer();
}
} class MyRepositoryEloquent extends PresentableRepository implements MyRepository
{
public function model()
{
return MyModel::class;
}
public function presenter()
{
return MyPresenter::class;
}
} _Usage:_ Call the class MyController extends Controller
{
protected $repository;
public function __construct(MyRepository $repository){
$this->repository = $repository;
}
public function index()
{
$collection = $this->repository->present(['myrelationship'])->all();
}
} |
I've been very busy in recent months with my work projects, which eventually pulling away of my personal projects, so it could not provide the necessary support to you, I am more relaxed now and I'm checking all the issues in order to work on fixes and new implementations, apologize to everyone. |
first thanks @jonagoldman for this nice tip ! Second, is possible to implement this in the attempt is to have more standart behavior who manage the needed relations in background |
I want to include relationships in my presenter.
I know I can _automatically_ include default relationships by adding this to my Transformer class:
Now, I want to choose _manually_ when to add a relationship.
Reading the Fractal docs I see that I need to add this to my Transformer class
But now I need a way to tell the presenter to include my relationship ('author' in this case)
How can I do that?
The text was updated successfully, but these errors were encountered: