【问题标题】:Favorite functionality for my laravel application我的 laravel 应用程序最喜欢的功能
【发布时间】:2021-05-13 11:24:06
【问题描述】:

我目前正在尝试为我的 laravel 应用程序添加一个最喜欢的功能。我正在尝试用 eloquent 访问 post 表,但它说属性 posts(最喜欢的模型中的函数)不存在。

更新:我更新了查询。如果我转储 $favorite,我会得到两个项目,这是正确的,但现在我收到此错误消息:Property [posts] 在 Eloquent 构建器实例上不存在。 (查看:C:\xampp\laravelprojects\Skakahand\resources\views\profile\index.blade.php)

<div class="favorite-section">
    <p>Mina favoriter</p>

    {{$favorite->posts->title}}

</div>

这是我的控制器:

public function index(User $user)
    {

        $favorite = Favorite::where('user_id', auth()->user()->id)
        ->get();

        return view('profile.index',[
            'user' => $user,
            'favorite' => $favorite

        ]);
    }

最喜欢的型号:

class Favorite extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id'
    ];

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

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

并发布模型:

class Post extends Model
{
    use HasFactory;
    /* use Sluggable; */
    
    protected $fillable = [
        'title',
        'body',
        'category',
        'decision',
        'number',
        'place',
        'image_path',
        'slug',
        'price',
        'user_id',
    ];


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

    
    public function favorites(){
        return $this->hasMany(Favorite::class);
    }


   
}

【问题讨论】:

  • 转储$favorite会得到什么?
  • Illuminate\Database\Eloquent\Collection {#620 ▼ #items: [] }
  • 基本没有
  • 我将查询更改为 $favorite = Favorite::where('user_id', auth()->user()->id) 现在我得到两个项目,但仍然出现此错误消息 Property [posts ] 在 Eloquent 构建器实例上不存在。 (查看:C:\xampp\laravelprojects\Skakahand\resources\views\profile\index.blade.php)

标签: laravel


【解决方案1】:

第一个这样的正确模型

class Favorite extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'post_id',
    ];


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


    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

在控制器中更改这样的代码以避免延迟加载

public function index(User $user)
{

    $favorites = Favorite::where('user_id', auth()->user()->id)
        ->with('post')
        ->get();

    return view('profile.index',[
        'user' => $user,
        'favorites' => $favorites

    ]);
}

在刀片中使用类似代码

<div class="favorite-section">
    <p>Mina favoriter</p>
    
    <ul>
        
        @foreach($favorites as $favorite)
                
             <li>{{ $favorite->post->title ?? ''  }} </li>
           
        @endforeach
    
    </ul>

</div>

【讨论】:

  • 此错误消息的根本原因是什么?这样我就不会再犯同样的错误了,是不是我忘记post_id可以在模型中填写了?
  • post_id 没有问题。我认为您最初的问题是忘记在 then end ->get() 方法中使用。
【解决方案2】:

只包括-&gt;with() 的关系, 您的查询应如下所示

 $favorite = Favorite::where('user_id', auth()->user()->id)->with('post')
        ->get();

        return view('profile.index',compact('favorite','user'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多