Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boldstar's avatar

Eloquent Method Does Not Exist

So I am trying to call a method from my eloquent model however it tells me it doesn't exist. Looking to see if some body could tell me what I am doing wrong.

the issue is with the mehtod statuses()

here is the controller

public function updateWorkflowStatuses(Request $request)
    {
    
        // validate form data
        $validated = $request->validate([
            'id' => 'required|integer',
            'statuses' => 'required|array',
        ]);

        $workflow = Workflow::where('id', $validated['id'])->get();

        $statuses = $validated['statuses'];

        $workflow->statuses()->whereIn('workflow_id', $validated['id'])->delete();
       
        foreach($statuses as $status){
            $workflow->statuses()->create([
                'status' => $statuses['status'],
                'order' => $statuses['order']
            ]);
        };

        return response('Update Succesful', 200);

    }

here is the model

class Workflow extends Model
{
    protected $fillable = [
        'workflow'
    ];

    public function statuses()
    {
        return $this->hasMany('App\Status');
    }

    public function engagements()
    {
        return $this->hasMany('App\Engagement');
    }
}
0 likes
15 replies
D9705996's avatar

Change $workflow->statuses()->whereIn() to $workflow->statuses->whereIn()

boldstar's avatar

@D9705996 - Getting this alarm when I do that

Property [statuses] does not exist on this collection instance.

I am trying to access the Status table, don't I have to call $workflow->statuses() in order to do that? based off my eloquent model description...

Sergiu17's avatar
$workflow = Workflow::where('id', $validated['id'])->get(); // returns a collection

$workflow->statuses() // can't call statuses() on collection
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@BOLDSTAR -

$workflow = Workflow::where('id', $validated['id'])->firstOrFail();

Try to use firstOrFail(), instead of get()

boldstar's avatar

@SERGIU17 - Looks like it work! Thank you for the help. I'm not sure if this is outside the scope of this discussion but now I am getting an issue with trying to use whereIn()

$workflow->statuses()->whereIn('workflow_id', $validated['id'])->delete();

Do I have to iterate over the Status table separately to delete multiple records?

D9705996's avatar

I would leverage some of Laravels built in functions to help make you life easier. Route model binding and form requests.

php artisan make:request WorkflowRequest then add you validation here

<?php

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;

class WorkflowRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'id' => 'required|integer',
            'statuses' => 'required|array',
        ];
    }
}
...
    public function updateWorkflowStatuses(WorkflowRequest $request, Workflow $workflow)
    {
    $workflow->statuses->each->delete();
    
    foreach($statuses as $status){
            $workflow->statuses()->create([
                'status' => $status['status'],
                'order' => $status['order']
            ]);
        };

The above is equivalent to your original post.

boldstar's avatar

@D9705996 - @d9705996 , I appreciate you taking the time to make your example, however I don't understand this line

$workflow->statuses->each->delete();

How is it accessing the Status table? Also how does it know which statuses to delete?

D9705996's avatar

statuses is the name of your relationship which returns only the statuses related to the user (rather than all statuses). The data returned is a collection so you have lots of methods you can use and there is a higher order function called each that allows you to do something e.g. delete each member of the collection. Its the same as


foreach($workflow->statuses as $status) {
    $status->delete();
}

The eloquent relationships are fundamental laravel concepts so I highly recommend this introduction series to get you up and running in no time

https://laracasts.com/series/laravel-from-scratch-2018

boldstar's avatar

@D9705996 - Okay I'll check it out, I guess I was wondering why it is not

$workflow->statuses()

??

D9705996's avatar

The function version will return the relationship object e.g. hasMany() and the property returns the result.

$model->relation() returns the relationship object
$model->relation returns the result of the relationship

See below for the detailed version

https://stackoverflow.com/a/28224427

1 like
D9705996's avatar

@sergiu17 - You have a typo in your answer firtOrFail(); should be >firstOrFail();. Would it be possible to update to prevent anyone else looking at the solution and having problems.

Thanks

1 like

Please or to participate in this conversation.