2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Brand;
|
|
|
|
|
use App\Models\Customer;
|
2021-02-28 12:04:49 +01:00
|
|
|
use App\Models\CustomerInfo;
|
2020-12-01 17:02:10 +01:00
|
|
|
use App\Models\CustomerLogin;
|
|
|
|
|
|
|
|
|
|
class CustomerController extends Controller
|
|
|
|
|
{
|
2021-02-28 12:04:49 +01:00
|
|
|
public function index()
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
$brands = Brand::select('id', 'name')->get();
|
|
|
|
|
return view('customers.index', compact('brands'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
public function show($id, $brandId)
|
|
|
|
|
{
|
2021-03-14 10:45:42 +01:00
|
|
|
$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();
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$CustomerInfo) {
|
2020-12-01 17:02:10 +01:00
|
|
|
abort(404);
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
|
|
|
|
return view('customers.show', compact('CustomerInfo', 'customerLogin'));
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
public function filter($texte, $brandId = null)
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
if ($brandId !== null) {
|
2021-03-14 10:45:42 +01:00
|
|
|
$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([
|
2021-02-28 12:04:49 +01:00
|
|
|
['lastname', 'like', '%' . $texte . '%'],
|
|
|
|
|
['customer_infos.brand_id', $brandId],
|
|
|
|
|
['customer_login.brand_id', $brandId]
|
2021-03-14 10:45:42 +01:00
|
|
|
])->orWhere([
|
2021-02-28 12:04:49 +01:00
|
|
|
['email', 'like', '%' . $texte . '%'],
|
|
|
|
|
['customer_infos.brand_id', $brandId],
|
|
|
|
|
['customer_login.brand_id', $brandId]
|
2021-03-14 10:45:42 +01:00
|
|
|
])->get();
|
2020-12-01 17:02:10 +01:00
|
|
|
} else {
|
2021-03-14 10:45:42 +01:00
|
|
|
$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');
|
2021-03-14 10:45:42 +01:00
|
|
|
})->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 . '%')->orWhere('email', 'like', '%' . $texte . '%')->get();
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json($customers);
|
|
|
|
|
}
|
|
|
|
|
}
|