mercredi 1 avril 2020

Having problems in the store method Laravel-Eloquent

Been at this for a very long time and I can't get it right. Would be really thankful for some help. Thanks in advance! Im trying to build a little clone of reddit some learning purpose.

I think these models is correct for what I'm trying to do. Im able to save a Subreddit to DB with user_id. But my problem is that I can't post to the post table since it's telling me it cannot find the subreddits_id column. Im seems like the method im trying to call should be working but it doesnt.

protected $fillable = ['title', 'link','content', 'post_picture', 'user_id', 'subreddit_id'];


//Functions

public function user () {
    return $this->belongsTo(User::class);
}

public function subreddit() {
    return $this->belongsTo(Subreddit::class);
}

public function comments () {
    return $this->hasMany(Comment::class);
}

Above is my Post model

protected $guarded = [];

//Functions

public function user () {
    return $this->belongsTo(User::class);
}

public function posts () {
    return $this->hasMany(Post::class);
}

public function comments () {
    return $this->hasMany(Comment::class);
}

Above is my Subreddit Model

public function posts () {
    return $this->hasMany(Post::class);
}

public function subreddit() {
    return $this->hasMany(Subreddit::class);
}

public function commments () {
    return $this->hasMany(Comment::class);
}

Above is my User model

I think these models is correct for what I'm trying to do. Im able to save a Subreddit to DB with user_id. But my problem is that I can't post to the post table since it's telling me it cannot find the subreddits_id column. Im seems like the method im trying to call should be working but it doesnt.

The Store method looks like this:

public function store(Request $request)
{ $data = request()->validate([
        'title' => 'required',
        'link' => 'required',
        'content' => 'required',
    ]);


    $post = auth()->user()->posts()->create($data);
return redirect('/home');

}

I'm getting this error General error: 1364 Field 'subreddit_id' doesn't have a default value (SQL: insert into posts (title, link, content, user_id, updated_at, created_at) values (awe, ew, we, 2, 2020-04-01 17:41:29, 2020-04-01 17:41:29))



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire