giordanolima / eloquent-repository

Repository pattern for Eloquent ORM with focus in cache.
MIT License
30 stars 4 forks source link

Eager loading not working #10

Open romarioliveira25 opened 5 years ago

romarioliveira25 commented 5 years ago

Hi,

When I try use the eager loading at the repository model query the result not contains the relations requested.

PD: If I use eager loading directly at the model class works fine.

Repository class

<?php

namespace App\Repositories;

use GiordanoLima\EloquentRepository\BaseRepository;

use App\Entities\Worker;

/**
 * Class WorkerRepositoryEloquent.
 *
 * @package namespace App\Repositories;
 */
class WorkerRepositoryEloquent extends BaseRepository
{
    protected $perPage = 10;

    protected function model()
    {
        return Worker::class;
    }

    public function getAll()
    {
        return $this->with(['user', 'department', 'occupation', 'address', 'naturalness'])->get();
    }

    public function filterByCpf($cpf)
    {
        return $this->where('cpf', $cpf)->first();
    }
}

Model class

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Worker.
 *
 * @package namespace App\Entities;
 */
class Worker extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = array(
                            'name', 'registration', 'cpf', 'phone',
                            'mobile_phone', 'email', 'situation',
                            'department_id', 'birth_at', 'admitted_at',
                            'occupation_id', 'naturalness_id'
                        );

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'birth_at',
        'admitted_at'
    ];

    /**
     * Get department related with that worker
     * 
     */
    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    /**
     * Get naturalness related with that worker
     * 
     */
    public function naturalness()
    {
        return $this->hasOne(City::class, 'id', 'naturalness_id');
    }

    /**
     * Get occupation related with that worker
     * 
     */
    public function occupation()
    {
        return $this->belongsTo(Occupation::class);
    }

     /**
     * Get user related with that worker
     * 
     */
    public function user()
    {
        return $this->hasOne(User::class);
    }

     /**
     * Get address related with that worker
     * 
     */
    public function address()
    {
        return $this->morphOne(Address::class, 'addressable');
    }

    /**
     * Get absences related with that worker
     *
     */
    public function absences()
    {
        return $this->hasMany(Absence::class);
    }
}

Controller class

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Repositories\WorkerRepositoryEloquent as WorkerRepository;

class WorkerController extends Controller
{
    /**
     * Worker repository instance
     *
     * @var WorkerRepository
     */
    private $repository;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         // It not works fine
        $workers = $this->repository->getAll();

       // It works fine
        //$workers = \App\Entities\Worker::with(['user', 'department', 'occupation', 'address', 'naturalness'])->get();

        return response()->json($workers, 200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $worker = $this->service->getOne($id);

        return response()->json($worker, 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Json response

{
     "admitted_at": "2018-11-15 00:00:00"
     "birth_at": "2018-11-14 00:00:00"
     "cpf": "01499324219"
     "created_at": "2018-11-15 06:13:44"
     "department_id": 1
     "email": "teste@teste.com"
     "id": 1
     "mobile_phone": "9999999999"
     "name": "Testerson"
     "naturalness_id": 1
     "occupation_id": 1
     "phone": "99999999999"
     "registration": 990908
     "situation": 1
     "updated_at": "2018-11-15 06:13:44"
}
giordanolima commented 5 years ago

Please... Need some informations:

romarioliveira25 commented 5 years ago

Woops!

Package version: "giordanolima/eloquent-repository": "^2.0" Laravel version: "laravel/framework": "5.6.*"

romarioliveira25 commented 5 years ago

FYI

I have checked at the class BaseRepository (GiordanoLima\EloquentRepository\BaseRepository), more specifically the function with, and in relation of the same function in Laravel Eloquent Database (https://github.com/illuminate/database/blob/5.6/Eloquent/Model.php#L435), has some diferences about the way how de relations params are passed and the what is returned by the function.

I have tried a little change making the function with returns the same what is returned in Laravel Database Eloquent and it's works fine to me.

Before

protected function with($relations) {
     $this->model->with($relations);
     return $this;
}

After

 protected function with($relations) {
        return $this->model->with(is_string($relations) ? func_get_args() : $relations);
}
giordanolima commented 5 years ago

Fixed in https://github.com/giordanolima/eloquent-repository/commit/6a74c3f4184d30d41f1c950d2591691709c550c2 Please update your version to 2.0.8 from composer and check... Thanks a lot!!

romarioliveira25 commented 5 years ago

Hi,

I think that you hasn't understand the change. The return of the function isn't $this but the $this->model->with(....);

Before - Not working

protected function with($relations) {
     $this->model->with($relations);
     return $this;
}

After - Working

protected function with($relations) {
        return $this->model->with(is_string($relations) ? func_get_args() : $relations);
}

And more, I think that you need make the same change in all function where is returned $this.

giordanolima commented 5 years ago

It is necessary to return `$ this' in the methods so that it is possible to align the methods inside the repositories, like this:

$repository->where("colunm","value")->orderBy("colunm")->get();

and this does not interfere with the returns ... Only in methods where the data is actually returned is the treatment different ... Did you try to update the package for verification?

romarioliveira25 commented 5 years ago

Fixed in 6a74c3f Please update your version to 2.0.8 from composer and check... Thanks a lot!!

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove laravel/framework v5.6.39
    - Conclusion: don't install laravel/framework v5.6.39
    - Conclusion: don't install laravel/framework v5.6.38
    - Conclusion: don't install laravel/framework v5.6.37
    - Conclusion: don't install laravel/framework v5.6.36
    - Conclusion: don't install laravel/framework v5.6.35
    - Conclusion: don't install laravel/framework v5.6.34
    - Conclusion: don't install laravel/framework v5.6.33
    - Conclusion: don't install laravel/framework v5.6.32
    - Conclusion: don't install laravel/framework v5.6.31
    - Conclusion: don't install laravel/framework v5.6.30
    - Conclusion: don't install laravel/framework v5.6.29
    - Conclusion: don't install laravel/framework v5.6.28
    - Conclusion: don't install laravel/framework v5.6.27
    - Conclusion: don't install laravel/framework v5.6.26
    - Conclusion: don't install laravel/framework v5.6.25
    - Conclusion: don't install laravel/framework v5.6.24
    - Conclusion: don't install laravel/framework v5.6.23
    - Conclusion: don't install laravel/framework v5.6.22
    - Conclusion: don't install laravel/framework v5.6.21
    - Conclusion: don't install laravel/framework v5.6.20
    - Conclusion: don't install laravel/framework v5.6.19
    - Conclusion: don't install laravel/framework v5.6.18
    - Conclusion: don't install laravel/framework v5.6.17
    - Conclusion: don't install laravel/framework v5.6.16
    - Conclusion: don't install laravel/framework v5.6.15
    - Conclusion: don't install laravel/framework v5.6.14
    - Conclusion: don't install laravel/framework v5.6.13
    - Conclusion: don't install laravel/framework v5.6.12
    - Conclusion: don't install laravel/framework v5.6.11
    - Conclusion: don't install laravel/framework v5.6.10
    - Conclusion: don't install laravel/framework v5.6.9
    - Conclusion: don't install laravel/framework v5.6.8
    - Conclusion: don't install laravel/framework v5.6.7
    - Conclusion: don't install laravel/framework v5.6.6
    - Conclusion: don't install laravel/framework v5.6.5
    - Conclusion: don't install laravel/framework v5.6.4
    - Conclusion: don't install laravel/framework v5.6.3
    - Conclusion: don't install laravel/framework v5.6.2
    - Installation request for giordanolima/eloquent-repository 2.0.8 -> satisfiable by giordanolima/eloquent-repository[2.0.8].
    - Conclusion: don't install laravel/framework v5.6.1
    - Conclusion: don't install laravel/framework v5.6.0
    - giordanolima/eloquent-repository 2.0.8 requires illuminate/support 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* -> satisfiable by illuminate/support[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44], laravel/framework[5.5.x-dev].
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support 5.5.x-dev
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.0
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.16
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.17
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.2
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.28
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.33
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.34
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.35
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.36
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.37
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.39
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.40
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.41
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.43
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.5.44
    - Can only install one of: laravel/framework[5.6.x-dev, 5.5.x-dev].
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support 5.1.x-dev
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support 5.2.x-dev
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support 5.3.x-dev
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support 5.4.x-dev
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.1
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.13
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.16
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.2
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.20
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.22
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.25
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.28
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.30
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.31
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.41
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.6
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.1.8
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.0
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.19
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.21
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.24
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.25
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.26
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.27
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.28
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.31
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.32
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.37
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.43
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.45
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.6
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.2.7
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.3.0
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.3.16
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.3.23
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.3.4
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.0
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.13
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.17
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.19
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.27
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.36
    - don't install laravel/framework 5.6.x-dev|don't install illuminate/support v5.4.9
    - Installation request for laravel/framework 5.6.* -> satisfiable by laravel/framework[5.6.x-dev, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.18, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9].
romarioliveira25 commented 5 years ago

Because the error on update version from composer, I cannot get the code update, BTW I have done the change manually at the BaseRepository.php for test purposes and not working yet.

romarioliveira25 commented 5 years ago

It is necessary to return `$ this' in the methods so that it is possible to align the methods inside the repositories, like this:

$repository->where("colunm","value")->orderBy("colunm")->get();

and this does not interfere with the returns ... Only in methods where the data is actually returned is the treatment different ... Did you try to update the package for verification?

Returning $this at the methods, to this code example works, I need change like this:

$repository->model->where("colunm","value")->orderBy("colunm")->get();
giordanolima commented 5 years ago

How is your with method now?

romarioliveira25 commented 5 years ago

How is your with method now?

As you have changed at the fix.

romarioliveira25 commented 5 years ago

Hi, any news?