laravel 调用未定义的方法 Illuminate\Database\Query\Builder::withTrashed()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47520188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:01:06  来源:igfitidea点击:

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

phplaravel

提问by BARNOWL

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.5

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

laravel 5.2 调用未定义的方法 Illuminate\Database\Query\Builder::associate()

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

调用未定义的方法 Illuminate\Database\Query\Builder::withTrashed()

PostController.php

后控制器.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 modelcondensed 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();
    });
}

回答by

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

您的 Eloquent 模型应该使用Illuminate\Database\Eloquent\SoftDeletestrait 来使用该withTrashed()方法。

Like::class

喜欢::类

namespace App\Models;

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

class Like extends Model
{
    use SoftDeletes;
}