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

68 lines
2.7 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'));
}
2021-07-22 15:14:07 +02:00
public function show(Customer $customer, Brand $brand)
{
$customerInfos = CustomerInfo::where('customer_id', $customer->id)
2021-07-22 15:14:07 +02:00
->where('brand_id', $brand->id)->firstOrFail();
$customerLogin = CustomerLogin::where('customer_id', $customer->id)
->where('brand_id', $brand->id)->first();
return view('customers.show', compact('customerInfos', 'customerLogin'));
2020-12-01 17:02:10 +01:00
}
2021-07-22 15:14:07 +02:00
public function filter($texte, Brand $brand = null)
{
2021-07-22 15:14:07 +02:00
if ($brand) {
$customers = Customer::join('customer_infos', 'customers.id', '=', 'customer_infos.customer_id')
->join('customer_logins', 'customers.id', '=', 'customer_logins.customer_id')->select(
2021-03-14 10:45:42 +01:00
'customer_infos.customer_id',
'customer_infos.firstname',
'customer_infos.lastname',
'customer_infos.opt_in',
'customers.email',
'customer_infos.brand_id',
'customer_logins.last_connection'
2021-03-14 10:45:42 +01:00
)->where([
['lastname', 'like', '%' . $texte . '%'],
2021-07-22 15:14:07 +02:00
['customer_infos.brand_id', $brand->id],
['customer_logins.brand_id', $brand->id]
2021-03-14 10:45:42 +01:00
])->orWhere([
['email', 'like', '%' . $texte . '%'],
2021-07-22 15:14:07 +02:00
['customer_infos.brand_id', $brand->id],
['customer_logins.brand_id', $brand->id]
2021-03-14 10:45:42 +01:00
])->get();
2020-12-01 17:02:10 +01:00
} else {
2021-07-22 15:14:07 +02:00
$customers = CustomerInfo::join('customers', 'customers.id', '=', 'customer_infos.customer_id')
->join('customer_logins', function ($join) {
$join->on('customer_logins.customer_id', '=', 'customer_infos.customer_id');
$join->on('customer_logins.brand_id', '=', 'customer_infos.brand_id');
2021-07-22 15:14:07 +02:00
})->select(
'customer_infos.customer_id',
'customer_infos.firstname',
'customer_infos.lastname',
'customer_infos.opt_in',
'customers.email',
'customer_infos.brand_id',
'customer_logins.last_connection'
2021-07-22 15:14:07 +02:00
)->where('lastname', 'like', '%' . $texte . '%')
->orWhere('email', 'like', '%' . $texte . '%')->get();
2020-12-01 17:02:10 +01:00
}
return response()->json($customers);
}
}