2

I have a table with the name cotacaoitensfranqueado and I'd like to call it using just cif, I know I can do this DB::table('cotacaoitensfranqueado as cif') how I saw in this question, but I'd like to do that using my model.

    $user = Auth::user();

    $model_cotacaoitensfranqueado = new Cotacaoitensfranqueado();

    $query = $model_cotacaoitensfranqueado->newQuery();
    $query->select('cotacaoitensfranqueado.*', 'c.status');
    $query->join('cotacao as c', [
       ['c.codigoconcentrador', 'cotacaoitensfranqueado.codigoconcentrador'],
       ['c.codigoempresa', 'cotacaoitensfranqueado.codigoempresa'],
       ['c.codigocotacao', 'cotacaoitensfranqueado.codigocotacao'],
    ]);
    $query->where('cotacaoitensfranqueado.codigoconcentrador', (int)$user->codigoconcentrador)
        ->where('cotacaoitensfranqueado.codigoempresa', (int)$codigoempresa)
        ->where('cotacaoitensfranqueado.codigofilial', $user->codigofilial)
        ->where('cotacaoitensfranqueado.codigocotacao', (int)$codigocotacao)
        ->where('c.status', 'A');

    $cotacao = $query->get();
SpaceDogCS
  • 2,808
  • 3
  • 20
  • 49

2 Answers2

1

You don't need to use newQuery() or (int) with Eloquent:

Cotacaoitensfranqueado::where('codigoconcentrador', auth()->user()->codigoconcentrador)->get();

But if you really want to use it, use query() instead because newQuery() is deprecated.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • thanks for answering, how do I alias the table in this way? – SpaceDogCS Jan 26 '18 at 12:03
  • @SpaceDogCS you don't need to alias data, because Eloquent will return a collection of objects. – Alexey Mezenin Jan 26 '18 at 12:06
  • @SpaceDogCS I mean Eloquent doesn't use this info anyway. Just look into the result of `dd(Cotacaoitensfranqueado::where('codigoconcentrador', auth()->user()->codigoconcentrador)->get());` do understand what I'm talking about. – Alexey Mezenin Jan 26 '18 at 12:10
  • I understand, I just want to shorten the name of the table, so it get easier to make joins, etc – SpaceDogCS Jan 26 '18 at 12:11
  • 1
    @SpaceDogCS ok. When you use `join()`, it's Query Builder you use, not Eloquent ORM. So, you can use the `DB::table()` way to create an alias. – Alexey Mezenin Jan 26 '18 at 12:24
0

I realize how to solve the problem, you can do the alias in the model

Cotacaoitensfranqueado.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cotacaoitensfranqueado extends Model
{
    protected $table = 'cotacaoitensfranqueado as cif';
    public $timestamps = false;
}

Controller/CotacaoController.php

...
use App\Cotacaoitensfranqueado;

class CotacaoController extends Controller
{
   ...

   public function canSendBuy($codigoempresa, $codigocotacao){
        $user = Auth::user();

        $model_cotacaoitensfranqueado = new Cotacaoitensfranqueado();

        $query = $model_cotacaoitensfranqueado->query();
        $query->select('cif.*', 'c.status');
        $query->join('cotacao as c', [
           ['c.codigoconcentrador', 'cif.codigoconcentrador'],
           ['c.codigoempresa', 'cif.codigoempresa'],
           ['c.codigocotacao', 'cif.codigocotacao'],
        ]);
        $query->where('cif.codigoconcentrador', (int)$user->codigoconcentrador)
            ->where('cif.codigoempresa', (int)$codigoempresa)
            ->where('cif.codigofilial', $user->codigofilial)
            ->where('cif.codigocotacao', (int)$codigocotacao)
            ->where('c.status', 'A');

        $cotacao = $query->get();

        dd($cotacao);
    }

    ...

}
SpaceDogCS
  • 2,808
  • 3
  • 20
  • 49