Jump to content
Fórum Script Brasil
  • 0

Depois de três anos, consegui ajuntar duas tabelas pelo Eloquent


Frank K Hosaka

Question

Em 2022, não existia a Gemini, o Copilot e tantos outros serviços de Inteligência Artificial. Assim, para eu ajuntar duas tabelas no Laravel, tive que usar a classe DB.

Aqui em 2024, mostrei o model do tbhistprod para a Gemini:

<?php // app > Models > tbhistprod.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class tbhistprod extends Model
{
    use HasFactory;
    protected $table="tbhistprod";
    const UPDATED_AT=null;
    const CREATED_AT=null;
    protected $fillable=['codprod','dia','docto','qt','custototal','codp','lcto','pessoa'];

    public function produtos()
    {
        return $this->belongsTo(tbprod::class,'codprod');
    }

    public function pessoa()
    {
        return $this->belongsTo(tbpessoa::class,'codp');
    }
}

Perguntei para ela como ajuntar a tbhistprod com a tbprod pelo Eloquent, e aqui a resposta:

 <?php // app > Http > Controllers / Estoque.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\tbdiario;
use App\Models\tbentraprod;
use App\Models\tbhistprod;
use App\Models\tbpessoa;
use App\Models\tbprod;

class Estoque
{

    function teste()
    {
        $entrada=tbdiario::where('contad',123)->sum('valor');
        $verificar=tbhistprod::with('produtos')
            ->whereHas('produtos',function($query)
                {$query->where('loc','<>','a24');})
            ->where('custototal','>',0)
            ->sum('custototal');
        return "entrada $entrada <br> verificar $verificar";
    }

	//...
}

 

Edited by Frank K Hosaka
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652.1k
×
×
  • Create New...