0

I would like to know if is it possible to work in a model using query builder to make a join between two tables, I don't want to use eloquent

this is my model

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;

class Tbl_Perimetro extends Model
{
puplic function perimetros(){
$carteras = DB::table('tbl_perimetros')
        ->join('tbl_equipo_postventaatcs', 'tbl_equipo_postventaatcs.id', '=', 'tbl_perimetros.postventaatc_id')
        ->join('tbl_lista_carteras', 'tbl_equipo_postventaatcs.carteras_id', '=', 'tbl_lista_carteras.id')
        ->get();

return $carteras;
}
}
7
  • I want to work with Query Builder in a model to make a join of a table, I don't want to use the method of eloquent hasMany or belongsTo Commented Feb 6, 2020 at 19:12
  • there is missing a return in perimetros() method, and you can call the method by $t = new Tbl_Perimetro(); $t->perimetros(); Commented Feb 7, 2020 at 12:26
  • something like this? I updated my question.. but I still obtaining nothing Commented Feb 7, 2020 at 12:53
  • you are put the method in model why u return view, you need to return $carteras. Commented Feb 7, 2020 at 13:18
  • I don't understandyou a lot... something like that? I want to the controller and the model works together with the view .blade Commented Feb 7, 2020 at 13:24

1 Answer 1

1

If you just want to use query builder you could to do so. But I realy recomend to use eloquent methods. Also, is important to look about mutators, appends and scopes options to models that could help you more than query builder alone.

Other appointments:

  • DB::table('tbl_perimetros') could be replaced by $this in this model
  • Do not forget return for the method.
  • Try protect $appends = ['perimetros'] and rename method to getPermietrosAttribute to return perimetros as an item in model object.
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for answer..how could I convert my code with Query Builder in a model which works.. because I got a lot of errors
First of all, what erros? In general, start with migrations, a good migrantion is a key to a good model relationship. Draw your database before start writing code, design tables and realtionships. Transfer this scratch to migrations with correct field types and foreign keys. Morph realtionships are powerfull but need more attention. The second step is write models. Start writing relationships that you have constructed in migrations. Than you can use other stuffs like mutators our appends to implement other data binds.
In this case, it seems to be 3 different models: carteras, postventaatcs and perimetros, all this tables should have english names without tbl_ prefix. The name in tables are used by eloquent to handle relationships automatically. In the same way, forenig keys should be in english too, but you are correct in add _id to then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.