1
votes

I want to send nama column from Supplier table to Transaction_in table, but iget this error.

Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [get_transactions_in] on model [App\Transaction_in].

Transaction_in Model

class Transaction_in extends Model
{
    protected $guarded = [];

    public function get_suppliers(){
        return $this->belongsTo(Supplier::class, 'Supplier_id');
    }
}

Supplier Model

class Supplier extends Model
{
    protected $guarded = [];

    public function get_transactions_in(){
        return $this->hasMany(Transaction_in::class);
    }
}

Transaction_in Controller

public function index()
    {
        $transaction_ins = Transaction_in::with('get_transactions_in')->get();
        return view('transactionsIN.index', compact('transaction_ins', $transaction_ins, 'supplierList'));
    }

The foreign key is Supplier_id based on id from Supplier table.

2
Why isn't the error message clear? It says that there is no method with that name in your Transaction_in model, which is right, because that method is part of the Supplier model, isn't it?nakov

2 Answers

1
votes

You called wrong relationship in with it should be get_suppliers instead of get_transactions_in

Transaction_in Model has get_suppliers method so,

$transaction_ins = Transaction_in::with('get_suppliers')->get();
0
votes

just change your controller code with below

public function index()
    {
        $transaction_ins = Transaction_in::with('get_suppliers')->get();
        return view('transactionsIN.index', compact('transaction_ins', $transaction_ins, 'supplierList'));
    }

you got an error because there is no relation like "get_transactions_in" in your Transaction_in model