Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
my laravel应用程序最喜爱的功能_Laravel - Fatal编程技术网

my laravel应用程序最喜爱的功能

my laravel应用程序最喜爱的功能,laravel,Laravel,我目前正在尝试为我的laravel应用程序创建一个最喜欢的功能。我试图用Elount访问post表,但它说property posts(最喜欢的模型中的函数)不存在 更新:我更新了查询。如果我转储$favorite,我会得到两个项,这是正确的,但现在我得到的是错误消息:在雄辩的生成器实例上不存在Property[posts]。(视图:C:\xampp\laravelprojects\Skakahand\resources\views\profile\index.blade.php) 最喜爱的型号

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

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

最喜爱的型号:

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);
    }


   
}

第一个正确的模型是这样的

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);
    }
}
 $favorite = Favorite::where('user_id', auth()->user()->id)->with('post')
        ->get();

        return view('profile.index',compact('favorite','user'));
在控制器中,更改如下代码以避免延迟加载

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>

米娜·福瑞特

    @foreach($favorites作为$favorite)
  • {{$favorite->post->title???'}
  • @endforeach
只需将关系的
->包含在()中即可,
您的查询应该如下所示

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);
    }
}
 $favorite = Favorite::where('user_id', auth()->user()->id)->with('post')
        ->get();

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

如果您转储
$favorite
,您会得到什么?Illumb\Database\Eloquent\Collection{620▼ #items:[]}基本上没有什么我将查询更改为$favorite=favorite::where('user_id',auth()->user()->id)现在我得到了两个项,但是这个错误消息属性[posts]在雄辩的生成器实例上仍然不存在。(视图:C:\xampp\laravelprojects\Skakahand\resources\views\profile\index.blade.php)此错误消息的根本原因是什么?所以我不再犯同样的错误了,是不是我忘记了在模型中填写post_id?post_id不是问题。我想你原来的问题是在then end->get()方法中使用的。