I stumbled upon different articles about implementing Repository pattern in Laravel and all of them making me confused. Everyone of them is putting their weight on "keeping it ORM independent" but no one is actually showing the example code.
I am here with my repository sample structure which I believe is not ORM independent in a way. But I need it with REAL REPOSITORY PATTERN solution where I can change the ORM from Eloquent
model to something else like Doctrine. And keep the Business logic separate some way so i no need to change it. currently my Business logic lies in Repository (not recommended i think.)
Basic Questions:
- My repository, uses Eloquent method name inside it which will not be there if i change the ORM.
$this->model = $shops;
- In Controllers and Blade templates what we are playing with is
Collections
ofEloquent Model
. How should we handle them if we change the ORM? - Where to put create/delete methods if not in repository.
Please don't just use
Domain Object
word because I am tired of understanding it without an coded example. It will be highly appreciated if you try to explain [Domain object] using a real code example by modifying this. [How to return it in controller and use it in Blade]
Interface:
<?php
namespace Modules\ShopOwner\Entities\Repository\Contract;
interface ShopsRepository {
public function getall();
public function create($data);
public function update($id, $data);
public function delete($id);
}
Eloquent:
<?php
namespace Modules\ShopOwner\Entities\Repository\Eloquent;
use Illuminate\Database\Eloquent\Model;
use Modules\ShopOwner\Entities\Repository\Contract\ShopsRepository;
use Modules\ShopOwner\Entities\Shops;
class ShopsEloquent implements ShopsRepository
{
protected $model;
public function __construct(Shops $shops)
{
$this->model = $shops;
}
public function getall()
{
return $this->model::with('shopadmin')->get();
}
public function create($data)
{
$this->model::create($data);
}
public function update($id, $data)
{
$this->model::findOrFail($id)->update($data);
}
public function delete($id)
{
$this->model::findOrFail($id)->delete();
}
}
Controller:
<?php
namespace Modules\ShopOwner\Http\Controllers;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\ShopOwner\Entities\Repository\Contract\ShopsRepository;
use Modules\ShopOwner\Entities\Shops;
use Validator;
class ShopsController extends Controller
{
protected $shops;
public function __construct(ShopsRepository $shops)
{
$this->shops = $shops;
}
public function index()
{
$shops = $this->shops->getall();
return view('shopowner::shops.list', compact('shops'));
}
public function store(Request $request)
{
$data = $request->all();
$this->shops->create($data);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.create_success'));
}
public function update(Request $request, $id)
{
$data = $request->all();
$this->shops->update($id, $data);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.update_success'));
}
public function delete($id)
{
$this->shops->delete($id);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.remove_success'));
}
}
Aucun commentaire:
Enregistrer un commentaire