66

Laravel, the right way - PHPConference 2016

Embed Size (px)

Citation preview

To put it simply, Repository pattern is a kind of container where data access logic is stored. It hides the details of data access logic from business logic. In other words, we allow business logic to access the data object without having knowledge of underlying data access architecture. Mar 7, 2015

https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5

https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5

andersao/l5-repository

├──├──├──├──├──

├──├──

http://kamranahmed.info/blog/2015/12/03/creating-a-modular-application-in-laravel

├──├──├──├──├──

├──├──

├──├──

├──├──├──├──

├──

public function newDeal(Request $request) { $data = $request->all(); // some stuff $page = "new_deal/new_deal_" . $data['page'];

return view($page, [ 'active_step' => $data['page'], 'template_step' => 'app_deal_steps', 'origin' => 'new_deal', 'title' => 'New Deal', 'customers' => new App\Deals(), ]);}

public function newDeal(Request $request) { $data = $request->all(); // some stuff

$page = "new_deal/new_deal_" . $data['page'];

return view($page, [ 'active_step' => $data['page'], 'template_step' => 'app_deal_steps', 'origin' => 'new_deal', 'title' => 'New Deal', 'customers' => new App\Deals(), ]);}

X

public function newDeal(Request $request, App\Deals $deal) { $data = $request->all();

// some stuff

$page = "new_deal/new_deal_" . $data['page'];

return view($page, [ 'active_step' => $data['page'], 'template_step' => 'app_deal_steps', 'origin' => 'new_deal', 'title' => 'New Deal', 'customers' => $deal, ]);}

public function store(Request $request){ $result['message'] = 'Success'; try { $data = $request->all();

$userId = Auth::user()->id;

$company = \App::make(Company::class) ->where('created_by', $userId) ->first();

$company->save($data); } catch (\Exception $error) { $result['message'] = $error->getMessage(); }

return response()->json($result);}

$invoices = [];

foreach ($request as $each) { $invoices[$each['send_date']] = [ 'deal_id' => $each['deal_id'], 'company_id' => $companyId, 'invoice_id' => $each['invoice_id'], 'invoice_term' => Invoices::TERM_30, 'send_same_invoice' => $each['send_same_invoice'], ];}

$invoices = [];

foreach ($request as $each) { $invoices[$each['send_date']] = [ 'deal_id' => $each['deal_id'], 'company_id' => $companyId, 'invoice_id' => $each['invoice_id'], 'invoice_term' => Invoices::TERM_30, 'send_same_invoice' => $each['send_same_invoice'], ];}

$invoices = [];

foreach ($request as $each) { $invoices[$each['send_date']] = [ 'deal_id' => $each['deal_id'], 'company_id' => $companyId, 'invoice_id' => $each['invoice_id'], 'invoice_term' => Invoices::TERM_30, 'send_same_invoice' => $each['send_same_invoice'], ];}

$invoices = new Collection();

foreach ($request as $each) { $invoices->push(new Invoice([ 'deal_id' => $each['deal_id'], 'company_id' => $companyId, 'invoice_id' => $each['invoice_id'], 'invoice_term' => Invoices::TERM_30, 'send_same_invoice' => $each['send_same_invoice'], ]));}

// Retrieve the first item in the collection$collection->get(0);

// Default value as fallback$collection->get(0, 'default value');

// Retrieve using custom key$collection->get('my_key');

// Custom key and fallback$collection->get('my_key', 'default value');

// Retrieve the first item in the collection$collection->get(0);

// Default value as fallback$collection->get(0, 'default value');

// Retrieve using custom key$collection->get('my_key');

// Custom key and fallback$collection->get('my_key', 'default value');

// Retrieve the first item in the collection$collection->get(0);

// Default value as fallback$collection->get(0, 'default value');

// Retrieve using custom key$collection->get('my_key');

// Custom key and fallback$collection->get('my_key', 'default value');

// Retrieve the first item in the collection$collection->get(0);

// Default value as fallback$collection->get(0, 'default value');

// Retrieve using custom key$collection->get('my_key');

// Custom key and fallback$collection->get('my_key', 'default value');

$invoices = [];

foreach ($request as $each) { $invoices[$each['send_date']] = [ 'deal_id' => $each['deal_id'], 'company_id' => $companyId, 'invoice_id' => $each['invoice_id'], 'invoice_term' => Invoices::TERM_30, 'send_same_invoice' => $each['send_same_invoice'], ];}

public function update(Request $request){ // do something

return Auth::user()->id;}