Support the ongoing development of Laravel.io →
posted 11 years ago
Eloquent

I have this models:

app/Models/Instance.php


<?php

namespace App\Models;

use Eloquent;

class Instance extends Eloquent {

    protected $table = 'instances';
    protected $guarded = ['id'];
    protected $softDelete = true;

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'id_users');
    }
}

app/Models/InstanceDisk.php


<?php

namespace App\Models;

use Eloquent;

class InstanceDisk extends Eloquent {

    protected $table = 'instances_disk';
    protected $guarded = ['id'];

    public $timestamps = false;

    public function instance()
    {
        return $this->belongsTo('App\Models\Instance', 'id_instances');
    }
}

app/Commands/InvoicesGenerate.php


<?php

namespace App\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

use \App\Models, \App\Libs;

class InvoicesGenerate extends Command {

    protected $name = 'invoices:generate';

    public function __construct()
    {
        parent::__construct();
    }

    public function fire()
    {
        $start = date('Y-m-d 00:00:00', strtotime('first day of last month'));
        $end = date('Y-m-d 23:59:59', strtotime('last day of last month'));

        $instances = Models\Instance::where('created_at', '<=', $end)
            ->where('deleted_at', '>=', $start)
            ->where('deleted_at', 'IS', 'NULL', 'OR')
            ->with(['InstanceDisk' => function ($query) use ($start, $end) {
                $query->where('date', '>=', $start)->where('date', '<=', $end);
            }])
            ->withTrashed()->get();
    }
}

Using this code I get this error:

 
php artisan invoices:generate
                          
  [BadMethodCallException]                                                           
  Call to undefined method Illuminate\Database\Query\Builder::InstanceDisk()  

But I can use Models\InstanceDisk::where without problem.

Also I have tried with with(['App\Models\InstanceDisk' and with(['\App\Models\InstanceDisk'

Final query should be:

 
$users = Models\User::with(['Instance' => function($query) use ($start, $end) {
    $query->where('created_at', '<=', $end)
        ->where('deleted_at', '>=', $start)
        ->where('deleted_at', 'IS', 'NULL', 'OR')
        ->withTrashed()
        ->with(['InstanceDisk' => function ($query) use ($start, $end) {
            $query->where('date', '>=', $start)->where('date', '<=', $end);
        }]);
}])->get();

but I think that is too much complicated.

with(['Instance' not fails, only with(['InstanceDisk'. For example, this code works fine:

 
$users = Models\User::with(['Instance' => function($query) use ($start, $end) {
    $query->where('created_at', '<=', $end)
        ->where('deleted_at', '>=', $start)
        ->where('deleted_at', 'IS', 'NULL', 'OR')
        ->withTrashed();
}])->get();

What is my fault?

Thanks a lot :)

Last updated 2 years ago.
0

Ok, my fault, Instance model had not the relation with InstanceDisk:

public function instanceDisk()
{
    return $this->hasMany('App\Models\InstanceDisk', 'id_instances', 'id');
}

Regards, Lito.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

eusonlito eusonlito Joined 2 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.