简体   繁体   中英

Laravel 5 : database relation query using with

User -> Has many question Question -> Has many answer

I want to show one user questions information with latest answer for each question

$questions = Question::select('id','user_id',
            'title','video','status','created_at')
            ->where('user_id','=',$user->id)
            ->where('status','>=','2')
            ->with([
                'user' => function($query){
                    $query->select('id','name','picture');
                },
                'answers' => function($query){
                    $query->select('id','question_id','user_id','answer','created_at',DB::raw('count(*) as answer_count'))
                    ->orderBy('created_at','desc')->first();

                },
            ])->get();

here I did not get answer for each question. I got answer for all questions submitted by the user

can you help me please? how to put where questions id for answer

Expected output:

User [id, name, and so on ] -> 
question [
    ['id', 'title' ..]->Answer['id','question_id',etc ],
    ['id', 'title' ..]->Answer['id','question_id',etc ]
]

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

You need to start with fetching user with their questions and the answers to them, not with the questions themselves, to get the expected output:

$user = User::select('id','name','picture')->with([
            'questions' => function($query){
                $query->select('id','user_id','title','video','status','created_at');
                $query->where('status','>=','2');
            },
            'questions.answers' => function($query){
                $query->select('id','question_id','user_id','answer','created_at',DB::raw('count(*) as answer_count'));
                $query->orderBy('created_at','desc')->first();

            },
        ])->findOrFail($user_id);

Now you should be able to get all questions asked by given user and the latest answer to them with the following:

foreach($user->questions as $question) {
  $answer = $question->answers;
}
$questions = Question::select('id','user_id','title','video','status','created_at')->with([
            'user' => function($query){
                $query->select('id','name','picture');
            }
        ])->where('status','>=','2')->get();

        foreach($questions as $question)
        {
            $question_data[] = [
                'id' => $question->id,
                'title' => $question->title,
                'video' => $question->video,
                'user_id' => $question->user_id,
                'status' => $question->status,
                'created_at' => $question->created_at->toDateTimeString(),
                'thumbnail' => $question->thumbnail,
                'user' => [
                    'id' => $question->user->id,
                    'name' => $question->user->name,
                    'picture' => $question->user->picture
                ],
                'answers' => 
                $question->answers()->select('video','question_id','user_id','created_at','thumbnail')
                ->with([
                    'user' => function($query){
                        $query->select('id','name','picture');
                    }
                ])->orderBy('created_at','desc')->first()];
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM