如何获取您关注的用户的提要 [英] How to get the feeds of the user whom you are following

查看:80
本文介绍了如何获取您关注的用户的提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取您关注的用户的提要.现在,我可以获取您正在关注的用户的ID,但仍在努力获取以下用户的供稿. 关注控制器

How to get the feeds of the user whom you are following. Right now I'm able to get the id of the user whom you are following but struggling to get the feeds of the following user. Followscontroller

<?php

namespace App\Http\Controllers;
use Redirect;
use App\User;
use Laracasts\Commander\CommanderTrait;
use App\FollowUserCommand; 
use Sentinel;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class FollowsController extends Controller
{
use CommanderTrait;
/**
 * Follow a User
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store()
{
    $input = array_add(Input::all(), 'user_id', Sentinel::getuser()->id);
    $this->execute(FollowUserCommand::class, $input);
    return Redirect::back();
}


/**
 * Unfollow a User
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}
}

FollowUserCommand

<?php namespace App;
use App\User;

class FollowUserCommand {

public $user_id;
public $userIdToFollow;

function __construct($user_id, $userIdToFollow)
{
    $this->user_id = $user_id;
    $this->userIdToFollow = $userIdToFollow;
}
}

FollowUserCommandHandler

<?php namespace App;

use Laracasts\Commander\CommandHandler;

class FollowUserCommandHandler implements CommandHandler {

protected $userRepo;

function __construct(UserRepository $userRepo)
{
    $this->userRepo = $userRepo;
}




public function handle($command)
{
    $user = $this->userRepo->findById($command->user_id);

    $this->userRepo->follow($command->userIdToFollow, $user);

    return $user;
}

}

UserRepository

<?php namespace App;
 use App\User;

class UserRepository {

public function save(User $user) 
{
    return $user->save();
}

public function getPaginated($howMany = 4)
{
    return User::orderBy('first_name', 'asc')->paginate($howMany);
}

public function findByUsername($username)
{
    return User::with(['feeds' => function($query)
    {
        $query->latest();
    }

    ])->whereUsername($username)->first();
}

public function findById($id) 
{
    return User::findOrFail($id);
}

public function follow($userIdToFollow, User $user)
{
    return $user->follows()->attach($userIdToFollow);
}
}

User.php

<?php namespace App;
use Cartalyst\Sentinel\Users\EloquentUser;
use Illuminate\Database\Eloquent\SoftDeletes;


 class User extends EloquentUser {


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes to be fillable from the model.
 *
 * A dirty hack to allow fields to be fillable by calling empty fillable array
 *
 * @var array
 */
protected $fillable = [];
protected $guarded = ['id'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

/**
* To allow soft deletes
*/
use SoftDeletes;

protected $dates = ['deleted_at'];



 public function feeds() 
{
    return $this->hasMany('App\Feed');
}

public function comment()
 {
    return $this->hasMany('App\Comment');
 }


    // This function allows us to get a list of users following us
public function follows()
{
    return $this->belongsToMany(self::class, 'follows', 'follower_id', 'followed_id')->withTimestamps();
}

// Get all users we are following
public function following()
{
    return $this->belongsToMany('User', 'followers', 'user_id', 'follow_id')->withTimestamps();
}
}

推荐答案

应该很简单.

$user = User::with('following.feeds')->get();
foreach ($user->following as $followedUser) {
    foreach ($followedUser->feeds as $feed) {

    }
}

这篇关于如何获取您关注的用户的提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆