Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0
Solution

First, you forgot the return before $this->hasMany('Listing'); in User model.

Then is the DB.

in users and categories table you must have a row "id".

In listings you need to have two rows, user_id and category_id

These are the default names for the row. If you want a different row name you must add id in belongsTo like this: return $this->belongsTo('User', 'row_name');

And when you insert a listing you need to insert the user_id and the category_id.


// User model

public function listings() 
{       
    return $this->hasMany('Listing', 'user_id');
}

// Category model

public function listings() 
{
    return $this->hasMany('Listing', 'category_id');
}  


// Listing model

public function listings() 
{
    return $this->belongsTo('Category', 'category_id');
}

public function users() 
{
    return $this->belongsTo('User', 'user_id');
}


// DB
Users - must have row "id"
Categories - must have row "id"
Listings - must have two rows "user_id", "category_id"

If you want a listing to have more categories you must use belongsToMany and create a new table for relationship. Let me know if you want that and i will make a version for that too :)

Last updated 1 year ago.
0

Hey Tavicu!

Thanks for your help, it's really appreciated :).

I simply forgot to add the return as you mentioned, as well as inserting the user_id and category_id.

I will test if it works as intended, then I will return here.

Last updated 1 year ago.
0

I am a little bit concerned about

public function listings() 
{       
    return $this->hasMany('Listing', 'user_id');
}

if it does not confuse, because the method name is singular, even though it returns hasMany results

Last updated 1 year ago.
0

The function name must be plural so listings it's ok ($user->listings)

The hasMany just call a Model, Listing, which needs to be singular :) ($listing->user)

But yes, in Listing model functions needs to be singular since is belongsTo (listing istead of listings and user instead of users)

But this is just aesthetic.

delmadord said:

I am a little bit concerned about

public function listings() 
{       
   return $this->hasMany('Listing', 'user_id');
}

if it does not confuse, because the method name is singular, even though it returns hasMany results

Last updated 1 year ago.
0

I managed to fix it with your help!

So to any other people with similar issues, always double-check your models as well as your controllers.

My issue was that I didn't save the user_id to the database along with the other values when I submitted my form.

So now I am simply doing:

$listing->user_id = Auth::user()->id;

However as a side question, is there a better way to achieve this?

Last updated 1 year ago.
0

Just to modify the default model function you are using to create a new listing and add by default the current user id.

Reached said:

I managed to fix it with your help!

So to any other people with similar issues, always double-check your models as well as your controllers.

My issue was that I didn't save the user_id to the database along with the other values when I submitted my form.

So now I am simply doing:

$listing->user_id = Auth::user()->id;

However as a side question, is there a better way to achieve this?

Last updated 1 year ago.
0

Can you be a bit more precise? :)

Tavicu said:

Just to modify the default model function you are using to create a new listing and add by default the current user id.

Reached said:

I managed to fix it with your help!

So to any other people with similar issues, always double-check your models as well as your controllers.

My issue was that I didn't save the user_id to the database along with the other values when I submitted my form.

So now I am simply doing:

$listing->user_id = Auth::user()->id;

However as a side question, is there a better way to achieve this?

Last updated 1 year ago.
0

As i said you need to overwrite model functions.

For example:

// Default model function
/**
 * Save a new model and return the instance.
 *
 * @param  array  $attributes
 * @return \Illuminate\Database\Eloquent\Model|static
 */
public static function create(array $attributes)
{
	$model = new static($attributes);

	$model->save();

	return $model;
}

// How your function need to be:
/**
 * Save a new model and return the instance.
 *
 * @param  array  $attributes
 * @return \Illuminate\Database\Eloquent\Model|static
 */
public static function create(array $attributes)
{
	$attributes['user_id'] = Auth::user()->id;

	$model = new static($attributes);

	$model->save();

	return $model;
}

Just add this function in Listing model and it will overwrite the default function. Now when you will use create funtion ( Example: $user = User::create(array('name' => 'John')); ) it will add user_id by default.

I'm not at pc right now but i will look tonight and maybe we can add this in save function so it will works with every method you are using.

Reached said:

Can you be a bit more precise? :)

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.