Asked  7 Months ago    Answers:  5   Viewed   47 times

I have two entities in laravel 5 Eloquent, Foo and FooType with oneToMany relation.

class Foo {

   public function fooTypes(){
    return $this->hasMany('AppFooType');

and FooType

class FooType{

   public function foo(){
     return $this->belongsTo('AppFoo');

question 1: how can I query using eloquent query builder and return Foos that has type_id(this column is in FooType table) of 5;

something I tried:

Foo::fooTypes()->where('type_id', 5);

and any link for good tuts about queries like this.

question two: I find out this is hard to use Eloquent query builder is it bad practice to use normal laravel queries with DB, I mean by using db I cannot use orm or something like that(or what is the benefits of using eloquent query builder):

$foos = DB::table('foos')
        ->join('fooTypes', 'foo_id', '=', '')
        ->where('fooTypes.type_id', '=', '5')

this type of queries are much easy and there more tutorials about it.



Answer to Question 1:
You can use advance where clause when using eloquent orm like in your case, use can try:

Foo::whereHas('fooTypes', function($query){

Answer to Question 2:

Data Mapper or Query builder is faster than ORM Active Record. So when you are writing very large applications with significant number of concurrent requests then Data Mapper / Query Builder is the way to go.

On the other hand ORM provides a much cleaner syntax and better code readability for someone who just started with Laravel or is writing small or medium size app where the number of concurrent requests will not be significantly large.

Query Builder syntax is nearer to the plain SQL queries, which anyone who has worked with databases and sql finds easy to relate to.
So its a choice you have to make depending your comfort with the Query builder syntax as well as the size of app you are writing.

Laravel Eloquent vs Fluent query builder

Saturday, May 29, 2021
answered 7 Months ago

Keep your session and cache separate.

In the file configdatabase.php

You can set many redis connections, by default there is a "default" but you can add more to it

let's say you created 'session-connection' and 'cache-connection'

now you need to make use of it

go to file 'configsession.php'

and set it to 'connection' => 'session-connection',

then go to file configcache.php

and set it to

    'redis' => [
        'driver'     => 'redis',
        'connection' => 'cache-connection',

and now you can get your redis session records.

use IlluminateSupportFacadesRedis;
Log::debug( Redis::connection('session-connection')->keys('*') );
Saturday, May 29, 2021
answered 7 Months ago
class T1 extends Eloquent {

    protected $table = 't1';


class T2 extends Eloquent {

    protected $table = 't2';

    public function customer()
        return $this->belongsTo('T1','c_id');//c_id - customer id
    public function staff()
        return $this->belongsTo('T1','s_id');//s_id - staff id
  1. With use "with":

    $list = T2::with('customer')->with('staff')->get();
    foreach ($list as $row) {
        echo 'ID: '.$row->id.', customer: '.$row->customer->name.', staff: '.$row->staff->name.'<br>';
  2. With joins:

    $list = T2::leftJoin('t1 as customer_table', '','=','t2.c_id')
         ->leftJoin('t1 as staff_table', '','=','t2.s_id')
         ->select(' as staff_name',' as customer_name')
    foreach ($list as $row) {
        echo 'customer: '.$row->customer_name.', staff: '.$row->staff_name.'<br>';

About second question - This is for subqueries. Look documentation:

Saturday, May 29, 2021
answered 7 Months ago

Each group can have multiple contacts, and each contact can have multiple phone numbers, you'll need to define one-to-many relation from group to contact and from contact to phone number:

class Group extends Model {
  public function contacts() {
    return $this->hasMany(Contact::class);

class Contact extends Model {
  public function phoneNumbers() {
    return $this->hasMany(PhoneNumber::class);

With relations defined, you will be able to load groups, their contacts and contacts' phone numbers with:

$groups  = Group::with(['contacts', 'contacts.phoneNumbers'])->get();

This will give you a collection of groups. Each of groups will contain a collection of contacts in their contacts property. Each of contacts will contain phone numbers collection in their phoneNumbers properties. By iterating through those collections you should be able to get data needed to render the structure you need, e.g.:

@foreach($groups as $group)
  {{ $group->name }}
  @foreach ($group->contacts as $contact)
    {{ $contact->name }}
    @foreach ($contact->phoneNumbers as $number)
      {{ $number->number }}

All necessary information on how to model your data with Eloquent can be found in the documentation:

Saturday, May 29, 2021
answered 7 Months ago

You are using join instead of left join

Try this

->leftjoin('B', 'A.program_id', '=', 'B.program_id')
->where('A.student_id', '=', 5)

It will produce a query like enter image description here

Saturday, August 7, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :