D
D
Dmitry Kuznetsov2017-10-24 11:35:47
MySQL
Dmitry Kuznetsov, 2017-10-24 11:35:47

How to get authors (users) using eager lazy loading?

I do something like social. networks. Profile page. I get user posts. It is necessary to get more authors of posts, if any. But I don't know how to do(
User model:

/* Получаем данные по пользователю */
  public static function getUser(string $login = null){
    return User::where('login_users', $login)->first();
  }

  /* Получаем все зависимости */
  public static function withUser(string $login){
    $user = User::getUser($login);   // Получаем данные о пользователе

    // "Делаем ленивую нетерпеливую загрузку"
    if($user){
      $user->load(['posts']);
    }

    return $user;    // Отдаём эти данные
  }

  /* Получаем все посты от пользователя */
  public function posts(){
    return $this->hasMany(Posts::class, 'user_posts', 'id_users');
  }

  /* Получаем всех авторов постов (не так получается) */
  public function authorPosts(){
    return $this->hasMany(Posts::class, 'userAdd_posts', 'id_users');
  }

From this code, the data of the user himself and posts from his page are displayed. But now we need to get more authors by userAdd_posts (posts table). How to do it?
Post table
59eefabd4ca6a341873049.png
User table
59eefafa8d9eb204485690.png

I don't know what to do anymore. Thanks in advance.
PS : Maybe such an example would be better: Sots. network. User page. The user has posts. They may be from different users. I need to get all the posts (with data about the authors) that this user has (taken by condition, let's say 'author_posts' == $id_user). Further, as soon as all this data is received, it will still be necessary to load “likes” and “reposts” (I have not yet reached the implementation of this due to the incomprehensibility of how to do this), or rather, the quantity.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
ellrion, 2017-10-24
@dima9595

why are you adding "noise" to the model? than your static getUser method which will be called as
User::getUser($login) is better than User::whereLogin($login)->first()? I would also understand if this getByLogin method is in UsersRepository. the static method withUser is generally nonsense - User::withUser($login) - wat?
The naming of fields made me cry.
What for to do greedy lazy loading for one object?
Well and the main thing in your question is not a question.

A
Alexander Drozdov, 2017-10-24
@bagzon

When such a number of data and connections are involved, it is better to convert to pure mysql queries, otherwise it will flow and, in general, will slow down on big data

A
anlamas, 2017-10-24
@anlamas

It seems you wanted to create a la "bookmarked posts" for the user?
To do this, create a third table. As a result, you have three tables.
users_posts: user_id post_id // bookmarked posts
RouteServiceProvider.php

public function boot()
{
    parent::boot();

    Route::model('user', App\User::class);
    Route::model('post', App\Post::class);
}

web.php
Route::get('profile/{user}', '[email protected]')
Route::get('posts/{post}', '[email protected]')

user.php
public function getRouteKeyName()
{
    return 'login';
}

public function posts_feed()
{
    return $this->belongsToMany('App\Post');
}

public function posts()
{
    return $this->hasMany('App\Post', 'author_id');
}

post.php
public function getRouteKeyName()
{
    return 'slug';
}

public function author()
{
    return $this->belongsTo('App\User');
}

class ProfileController extends Controller
{
    public function show(User $user)
    {
        $user->load('posts_feed.author')
        return view('profile', compact('user'))
    }
}

In show.blade.php
{{ $user->name }}
@foreach(user->posts_feed as $post)
    {{ $post->title }}
    {{ $post->author->name }}
@endforeach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question