0

I need to be able to join an entry from Laravel's job_batches table onto one of my own tables & model which contains reference to a batch ID.

I'd like to do this through the model using the hasOne relationship but when I try to do this I'm getting an error:

Class 'App\Models\Illuminate\Support\Facades\Bus\Batch' not found

What am I missing here?

My model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class CSVUploadSchedule extends Model
{
    use SoftDeletes;

    /**
    * The table associated with the model.
    *
    * @var string
    */
    protected $table = 'csv_upload_schedules';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'csv_name',
        'csv_path'
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'processed_at' => 'datetime',
        'start_processing_at' => 'datetime'
    ];

    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = [
        'batch'
    ];

    /**
     * Get the batch associated with the schedule.
     */
    public function batch()
    {
        return $this->hasOne(Illuminate\Support\Facades\Bus\Batch::class, 'id','batch_id');
    }
}
lagbox
  • 48,571
  • 8
  • 72
  • 83
Ryan H
  • 2,620
  • 4
  • 37
  • 109
  • 1
    all class references in a namespaced file are references as the namespace as the base unless you use an alias "import" or use the FQCN with the `'\'` first, `'\'` means root ... the reference to `Batch` there is neither of those 2 options so the base is the current namespace plus what you have there – lagbox Dec 10 '21 at 14:09
  • Sorry, I'm not sure I understand. Could you provide an example please? – Ryan H Dec 10 '21 at 14:10
  • https://www.php.net/manual/en/language.namespaces.basics.php ... this is all in the manual – lagbox Dec 10 '21 at 14:13
  • also why are you referencing a facade as a model? ... and there is no `Illuminate\Support\Facades\Bus\Batch` class ... i am assuming you mean `Illuminate\Bus\Batch` which is not a Model – lagbox Dec 10 '21 at 14:16
  • 2
    As stated by @lagbox you are reference a 'non-model' class, and expect it to work as model. Unfortunately Laravel does not provide a model itself. However it does provide repositories. You can easyly define a model yourself, based on the table, and then you can use it in any relation. Something like here: https://stackoverflow.com/questions/67921072/laravel-job-batching-table-relationship-with-user-model – Danny Ebbers Dec 10 '21 at 15:59

0 Answers0