Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
How to allow models with belongsToMany relationship to be searchable using datatables?

I have the following model that has a belongsToMany relationship:

class Sale extends Model
{
    public function items()
    {
        return $this->belongsToMany(Item::class)->select(
            [DB::raw("COUNT(*) as quantity"), 
            DB::raw("CONCAT('(', GROUP_CONCAT(serial_number SEPARATOR ', '), ')') AS serial_number"),
            'items.name', 
            'items.upc',
            'items.id',
            'items.with_serial_number', 
            'items.selling_price', 
            'item_sale.sold_price',
            'item_sale.item_purchase_id'])
        ->join('item_purchase', 'item_sale.item_purchase_id', '=', 'item_purchase.id')
        ->orderBy('item_sale.id', 'ASC')
        ->groupBy('item_sale.item_id', 'item_sale.sale_id');
    }

and on my controller

public function demo(Request $request)
    {
        if ($request->ajax()) {
            $data = Sale::with(['branch', 'items', 'customer', 'user'])->select(['*']);

            return Datatables::of($data)
                    ->addIndexColumn()
                    ->make(true);
        }
    }

and on my jquery

$('#search_sale_list').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('rma.demo') }}",
        "columns": [
            {data: 'created_at'},
            {data: 'branch.address'},
            {data: 'sale_number'},
            {data: 'items'}
        ]
    });

My problem is i cant seem to search for the 'items' in my view when i use the searchbox of datatables, it gives out an error. is this even possible to do using datatables?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda