Alexandro Giles
Alexandro Giles

Reputation: 492

Populate a field using a foreign key on create template using Laravel 8

I have 1 table: "Transactions".
1 table "Employee".

I'm trying to retrieve some information from table "Employee" in Transactions context in order to populate a dropdown menu with id from "Employee" (since every transaction needs to be linked to an Employee). At this time when I run my code, I get no results.

I have a foreign key 'codigo_referido_id' that references to 'id' (from Employee table).

Transaction Model

class Transaction extends Model
{
    use HasFactory;

    public function employees(){

       return $this->belongsTo(Employee::class , 'codigo_referido_id');

    }
}

Employee Model

class Employee extends Model
{
    use HasFactory;

    protected $table = 'employees';
    public $timestamps = true;

    protected $fillable = [
        'nombre',
        'codigo_referido'
    ];


}

TransactionController

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Transaction $transaction)
    {
        $transactions = Transaction::all();
        

        return view('transaction.create' , compact('transactions'));
    }

Blade template (at the moment I only have this to check the result):

{{ dd($transactions->employees()) }}

Nonetheless, It'll fill a Select field with the employees.

At this time I get this error:

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::employees does not exist.

Can someone help me?

Regards

Upvotes: 0

Views: 239

Answers (2)

Peppermintology
Peppermintology

Reputation: 10210

Transaction::all() returns a collection, even if that collection only contains a single record. employees() is a method on an instance of Transaction therefore you will need to iterate over the collection of Transactions and call the employees method on each iteration.

As an example:

@foreach ($transactions as $transaction)
    {{ dump($transaction->employees) }}
@endforeach

Note that if you want the actual employees you need to call the relationship without the () as otherwise you'll get an instance of the BelongsTo relationship back.

Update

TransactionController.php

public function create()
{
    return view('transactions.create', [
        'employees' => Employee::all()
    ]);
}

public function show(Transaction $transaction)
{
    return view('transactions.create', [
        'transaction' => $transaction->with('employees')
    ]);
}

transactions.create

{{-- loop over all employees --}
@foreach ($employees as $employee)
    {{ $employee->id }}
@endforeach

transactions.show

{{-- get the employee associated to the transaction --}}
{{ $transaction->employees }}

Upvotes: 1

Mohammad Mirsafaei
Mohammad Mirsafaei

Reputation: 954

You're passing all of transactions to $transactions variable. In your blade file you are using {{ dd($transactions->employees()) }}. The problem is model relation functions should be run on each instance of model not collection of model. $transactions is collection of Transaction mdoel. Use foreach loop to iterate over them:

@foreach($transactions as $transaction)
    {{ $transaction->employee() }}
@endforeach

Upvotes: 1

Related Questions