#SOLVED | Error Trying to Pass A Data Created by Logged In User with Return View

23 messages · Page 1 of 1 (latest)

sly drift
#

Context:

I was following a tutorial about creating a page to manage listings. I've created a function to show the manage listing page and passing the data of job listings that have been created by the logged in user (currently using a user called "Test User" which have a user_id of 2). When trying to access the page, it gives me "Function () does not exist" error.

Here's the function code:

// Manage Listings 
    public function manage() {

// Pass 'listings' 
        return view('listings.manage', ['listings' => auth()->user()->listings()->get()]);
    }

For some reason, in the video tutorial it worked with no issues. But in my end, the code above throws "undefined method 'listings' error. I've even copy pasted the code from github but it still gives me that error.

I've made a table called "listings" consisting of job listings created by multiple users that registered and saved in a table called "users" (See the screenshot for the structures)

"listings" table have foreign key called "user_id" which connects to the "id" from "users" table.

The use of the "user_id" foreign key is to mark the job listings so we can know the listing's creator.

Any idea how to fix this?

#

Screenshot of the listings table

radiant pike
#

Your user model does not have a relation to listings defined

sly drift
# radiant pike Your user model does not have a relation to listings defined

I've made relationship for user to listings model

from Listing Model

// Relationship To User Model
    public function user() {
        return $this -> belongsTo(User::class, 'user_id');
    }

from User Model

// Relationship with Listings Model
    public function listings() {
        return $this -> hasMany(Listing::class, 'user_id');
    }
radiant pike
#

Can you show the full error? Are you sure this is the line they causes the error?

sly drift
#

Idk i just assume this one is the error's cause because it's the only one who have that redline

radiant pike
#

Till you hit a file that you made

#

Probably in some route

#

You are calling something weird somewhere

#

And your IDE just doesn't understand the Laravel magic IMO

sly drift
radiant pike
#

Maybe idk

#

But we are fixing a real error now, not ur IDE

crimson aurora
#

Seems it is trying to call an invokable controller that does not have the __invoke method

sly drift
# crimson aurora What is your route definition for listings/manage?
// ****************************************************************************************
// Manage Listings
// ****************************************************************************************
Route::get('/listings/manage', [ListingController::class], 'manage') -> middleware('auth');
crimson aurora