Невозможно получить переменную из Laravel ORM отношений - PullRequest
0 голосов
/ 05 августа 2020

Я хочу получить здесь параметр «$ invoice-> form-> form», потому что хочу использовать его в поле зрения. Когда я использую dd ($ invoice-> form); чем получает ноль. Счет-фактура и форма - это другие таблицы и модели, но они связаны с laravel отношениями и внешними ключами в миграциях.

public function show(Invoice $invoice)
{
        dd($invoice->form);
    return view('invoices.show',compact('invoice'));
}

Это начало файла контроллера счета:

namespace App\Http\Controllers;
use Kyslik\ColumnSortable\Sortable;    
use App\Invoice;
use Illuminate\Http\Request;
use App\User;
use App\Proform;
use App\Form;
use App\Currency;
use DB; 

Вот модель счета

<?php


namespace App;

use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Invoice extends Model
{
    /**
     * The attributes that are mass assignable.
     *  
     * @var array
     */
    use SoftDeletes;
    use Sortable;
    
    
    protected $table = 'invoices';

    
    protected $fillable = [
        'invoicenumber', 'invoicedate', 'id', 'selldate', 'user_id', 'paymentmethod', 
        'paymentdate', 'status', 'comments', 'city', 'paid',  'autonumber', 'automonth', 'autoyear', 'name',
         'PKWIU', 'quantity', 'unit', 'netunit', 'nettotal',
         'VATrate', 'grossunit', 'grosstotal', 'form_id', 'currency_id',
    ];
    
    public $sortable = [ 'invoicenumber', 'invoicedate', 'id', 'selldate', 'user_id', 'paymentmethod', 
        'paymentdate', 'status', 'comments', 'city', 'paid',  'autonumber', 'automonth', 'autoyear', 'name',
         'PKWIU', 'quantity', 'unit', 'netunit', 'nettotal',
         'VATrate', 'grossunit', 'grosstotal', 'form_id', 'currency_id',
                       ];
    
    protected $dates = ['deleted_at'];
    public $primaryKey = 'id';
    
        public function user()
    {
        return $this->belongsTo('App\User');
    }

        public function form()
    {
        return $this->hasOne('App\Form');
    }

         public function currency()
    {
        return $this->hasOne('App\Currency');
    }
   
        public function proform()
    {
        return $this->belongsTo('App\Proform');
    } 
    
}

Вот модель формы:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Form extends Model
{
    //
    
        protected $fillable = [
        'id', 'form', 
    ];
    
        public function invoice()
    {
        return $this->belongsTo('App\Invoice');
    }  
    
    
    
        public function proform()
    {
        return $this->belongsTo('App\Proform');
    }
}

1 Ответ

0 голосов
/ 05 августа 2020

Потому что вы не загружаете отношение.

На вашем контроллере вы должны запросить, например: $invoice = Invoice::with('form')->find($invoice_id);, и вернуть его в представление, например: return view('invoices.show', [ 'invoice' => $invoice->form ]);

...