8

Not sure what im doing wrong but im getting this error, and im not sure how to solve it.

i referenced this, but im still unsure, maybe its my schema migration

im using laravel 5.5

laravel 5.2 Call to undefined method Illuminate\Database\Query\Builder::associate()

Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

PostController.php

public function isLikedByMe($id)
{
    $post = Post::findOrFail($id)->first();
    if (Like::whereUserId(auth()->id())->wherePostId($post->id)->exists()){
        return 'true';
    }
    return 'false';
}

public function like(Post $post)
{
    $existing_like = Like::withTrashed()->wherePostId($post->id)->whereUserId(auth()->id())->first();

    if (is_null($existing_like)) {
        Like::create([
            'post_id' => $post->id,
            'user_id' => auth()->id()
        ]);
    } else {
        if (is_null($existing_like->deleted_at)) {
            $existing_like->delete();
        } else {
            $existing_like->restore();
        }
    }

Likes Model

class Like extends Model
{
    //
}

User model condensed to whats important

public function likes()
{
    return $this->belongsToMany('App\Post', 'likes', 'user_id', 'post_id');
}

Post model

public function likes()
{
    return $this->belongsToMany('App\User', 'likes');
}

Migration:

likes migration

public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts');
        $table->foreign('user_id')->references('id')->on('users');
        $table->softDeletes();
        $table->timestamps();

    });
}

Posts migration

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });
}
1
  • 3
    did you use this in your model use SoftDeletes; ? Commented Nov 27, 2017 at 21:46

2 Answers 2

36

Your Eloquent model should use the Illuminate\Database\Eloquent\SoftDeletes trait to make use of the withTrashed() method.

Like::class

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Like extends Model
{
    use SoftDeletes;
}
Sign up to request clarification or add additional context in comments.

6 Comments

i just updated let me know if you think im missing something
in the meanwhile im going to add that because i didn't have that
let me try it now
so the second edit is gets me closer to the objective.
would you have any idea what the mass assignment is pointing to
|
-1

Had the same issue, the problem was that I used DB::table($table_name) instead of calling the model, using the model helped.

Regards

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.