Relationship returning null

Posted 9 months ago by martt50

I have two models :

  • Job
  • EstimateDocument

The job model is :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Job extends Model
{
    protected $table = 'vgJcoJob';
    protected $primaryKey = 'Id';

    public function estimateDocument()
    {
        return $this->belongsTo(EstimateDocument::class, 'EstimateDocumentLink');
    }

    // -- SCOPES --
    public function scopeActive($query)
    {
        return $query->where('QuantityOfOpenWorkorder', '!=',0);
    }
}

The EstimateDocument is:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class EstimateDocument extends Model
{
    protected $table = 'vgQesEstimateDocument';
    protected $primaryKey = 'DocumentNumber';

    public function jobs()
    {
        return $this->hasMany(Job::class, 'EstimateDocumentLink');
    }

    public function estimates()
    {
        return $this->hasMany(Estimate::class, 'EstimateDocumentLink');
    }
}

I didnt choose the naming of the tables and columns, I had to use those who were already existing.

In one of my controllers, I'm trying to get the associated estimate document from a job, so I do this :

App\Job::find(9)->estimateDocument;

But then it return null.

When I enable the query log, I see this :

array:2 [▼
  0 => array:3 [▼
    "query" => "select top 1 * from [vgJcoJob] where [vgJcoJob].[Id] = ?"
    "bindings" => array:1 [▼
      0 => 9
    ]
    "time" => 25.0
  ]
  1 => array:3 [▼
    "query" => "select top 1 * from [vgQesEstimateDocument] where [vgQesEstimateDocument].[DocumentNumber] = ?"
    "bindings" => array:1 [▼
      0 => "104"
    ]
    "time" => 2.0
  ]
]

I tried to execute the last SQL statement inside of SSMS with the binding value (104) and it returned me something.

What am I doing wrong guys ? Thanks for the help !

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.