ldc-crm/app/Http/Controllers/CustomerController.php

76 lines
2.8 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Http\Controllers;
use App\Models\Brand;
use App\Models\Customer;
use App\Models\CustomerInfo;
2020-12-01 17:02:10 +01:00
use App\Models\CustomerLogin;
class CustomerController extends Controller
{
public function index()
{
2020-12-01 17:02:10 +01:00
$brands = Brand::select('id', 'name')->get();
return view('customers.index', compact('brands'));
}
public function show($id, $brandId)
{
$CustomerInfo = CustomerInfo::where([['customer_id', $id], ['brand_id', $brandId]])->first();
2020-12-01 17:02:10 +01:00
$customerLogin = CustomerLogin::where([['customer_id', $id], ['brand_id', $brandId]])->first();
if (!$CustomerInfo) {
2020-12-01 17:02:10 +01:00
abort(404);
}
return view('customers.show', compact('CustomerInfo', 'customerLogin'));
2020-12-01 17:02:10 +01:00
}
public function filter($texte, $brandId = null)
{
2020-12-01 17:02:10 +01:00
if ($brandId !== null) {
$customers = Customer::join('customer_infos', 'customers.id', '=', 'customer_infos.customer_id')
->join('customer_login', 'customers.id', '=', 'customer_login.customer_id')
->select(
'customer_infos.customer_id',
'customer_infos.firstname',
'customer_infos.lastname',
'customer_infos.opt_in',
'customers.email',
'customer_infos.brand_id',
'customer_login.last_connection'
)
->where([
['lastname', 'like', '%' . $texte . '%'],
['customer_infos.brand_id', $brandId],
['customer_login.brand_id', $brandId]
])
->orWhere([
['email', 'like', '%' . $texte . '%'],
['customer_infos.brand_id', $brandId],
['customer_login.brand_id', $brandId]
])
2020-12-01 17:02:10 +01:00
->get();
} else {
$customers = CustomerInfo::join('customers', 'customers.id', '=', 'customer_infos.customer_id')
->join('customer_login', function ($join) {
2020-12-01 17:02:10 +01:00
$join->on('customer_login.customer_id', '=', 'customer_infos.customer_id');
$join->on('customer_login.brand_id', '=', 'customer_infos.brand_id');
})
->select(
'customer_infos.customer_id',
'customer_infos.firstname',
'customer_infos.lastname',
'customer_infos.opt_in',
'customers.email',
'customer_infos.brand_id',
'customer_login.last_connection'
)
2020-12-01 17:02:10 +01:00
->where('lastname', 'like', '%' . $texte . '%')
->orWhere('email', 'like', '%' . $texte . '%')
->get();
}
return response()->json($customers);
}
}