下面的代码用于显示一个页面中有15个结果的表。为此,我使用了两个不同的数据库,一个是wordpress数据库,另一个是我创建的个人数据库。
第一个Query用于从wordpress数据库中获取表值,但它不获取用户名,因为wordpress只在该表中存储用户ID。我唯一有正确用户名的地方是我的第二个个人数据库。
为此,我使用foreach循环来替换用户名的ID。因此,在循环中,有一个新的查询用于获取用户名。
我的问题是,每次加载页面时,我都会同时运行16个DB查询,因此它会发出16个数据库请求,这会使我的页面速度变慢
public function index() {
$posts = DB::connection('mysql2')
->table('wp_rocketsciencebrposts')
->select('ID', 'post_title', 'post_status', 'post_author', 'post_date')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
$posts = compact('posts');
foreach($posts['posts'] as &$value){
//this DB Query is making my page slow, since it's inside this foreach loop, therefore, making 16 database requests
$value->post_author = DB::connection('mysql')
->table('users')
->select('name')
->where('rsbwordpressid', $value->post_author)
->value('name');
}
return view('posts/posts', $posts);
}
我相信这个解决方案非常简单,但我无法想出如何将第二个DB查询放在循环之外的策略,从而避免所有不必要的数据库请求。
请帮帮我。
因此,您可能会同时使用Laravel&Wordpress。我可以从Laravel POV那里给你一些建议,告诉你如何让这件事变得更好。
Eloquent模型
如果您还没有,请制作两个模型(Post
&User
(并设置它们之间的关系。
User.php
class User extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'wp_rocketsciencebrposts';
public function posts()
{
return $this->hasMany(Post::class, 'post_author', 'name');
}
}
Post.php
class User extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users';
public function user()
{
return $this->belongsTo(User::class, 'name', 'post_author');
}
}
控制器
在你的控制器里,你现在应该可以做一些类似的事情了
$post = Post::with('user')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
现在,$posts
中的每个Post
对象都应该加载一个->user
实体。
您现在可以进入$post->user->name
检索后的作者的姓名
您应该使用一个带有联接的查询:
$posts = DB::connection('mysql2')
->table('wp_rocketsciencebrposts')
->join('users', 'users.rsbwordpressid', '=', 'wp_rocketsciencebrposts.post_author')
->select('ID', 'post_title', 'post_status', 'post_author', 'post_date', 'users.name')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
这是我期望看到的答案:
public function index() {
$posts = DB::connection('mysql2')
->table('wp_rocketsciencebrposts')
->select('ID', 'post_title', 'post_status', 'post_author', 'post_date')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
$user_ids = $posts->pluck('post_author')->toArray();
$users = DB::connection('mysql')
->table('users')
->whereIn('rsbwordpressid', $user_ids)
->pluck('rsbwordpressid', 'name')
->toArray();
$posts = compact('posts');
$users = array_flip($users);
foreach($posts['posts'] as $value) {
$value->post_author = $users[$value->post_author];
}
return view('posts/posts', $posts)->with('mensagemSucesso', print_r($users) );
}