Code Monkey home page Code Monkey logo

Comments (4)

andersao avatar andersao commented on July 28, 2024

@syflipper

Well, the repository will you return a instantiates your model , then you could use this method without problems. If you are using a presenter , simply ignore the presenter using the skipPresenter () method.

Try this:

//GamesController.php

public function __construct(GameRepository $repository){
    $this->repository = $repository;
}

public function show($id)
{
    try
    {
        $game = $this->repository->skipPresenter(true)->find($id);
        $game->verifyOwnership(Auth::user());

        //more logic
    } catch (Exception $e) {
        throw $e;
    }
}

from l5-repository.

 avatar commented on July 28, 2024

Thanks for your answer @andersao. The problem is that I have to present the data after it gets verified again (API output). Any tips on doing that?

from l5-repository.

andersao avatar andersao commented on July 28, 2024

@syflipper

You can do this by creating a customized presentation. Thus it will extract your model information to be displayed in their API , at that time you can treat the information that is displayed . See the example below :

https://github.com/prettus/l5-repository#create-a-presenter

//GameTransformer.php

use App\Post;
use League\Fractal\TransformerAbstract;

class GameTransformer extends TransformerAbstract
{
    public function transform(Game $game)
    {
        $data = [];
        $data = [
            'id'=>$game->id,
            'title'=>$game->title
        ];

        if( $game->verifyOwnership(Auth::user()) )
        {
            $data = array_merge($data,[
                'dataOnlyIfOwnership'=>$game->privateData,
                ....
            ]);
        }

        return $data;
    }
}

//GamePresenter.php

use Prettus\Repository\Presenter\FractalPresenter;

class GamePresenter extends FractalPresenter {

    /**
     * Prepare data to present
     *
     * @return \League\Fractal\TransformerAbstract
     */
    public function getTransformer()
    {
        return new GameTransformer();
    }
}

//GameRepository.php

use Prettus\Repository\Eloquent\BaseRepository;

class GameRepository extends BaseRepository {

    ...

    public function presenter()
    {
        return "YOUR_NAMESPACE\\GamePresenter";
    }
}

I hope this works for you

from l5-repository.

 avatar commented on July 28, 2024

@andersao 👍

from l5-repository.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.