Home > Backend Development > PHP Tutorial > How to write this related query in laravel?

How to write this related query in laravel?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:53:55
Original
964 people have browsed it

How to write this related query in laravel?

The relationship is as follows:

<code>users表->articles表 ,一对多关系。
categories表->articles表,一对多关系。
</code>
Copy after login
Copy after login

Query:

<code>    public function index()
    {
        $user=\Auth::user();

        //1、查询当前登录用户的文章.
        //2、文章对应的类别.
        //用的下面这样一条语句:
        $articles = $user->articles->with('category');

        return view('index',  compact('articles'));
    }</code>
Copy after login
Copy after login

Error report:
How to write this related query in laravel?

Question:
How should I write the query statement in the index() method?

Reply content:

How to write this related query in laravel?

The relationship is as follows:

<code>users表->articles表 ,一对多关系。
categories表->articles表,一对多关系。
</code>
Copy after login
Copy after login

Query:

<code>    public function index()
    {
        $user=\Auth::user();

        //1、查询当前登录用户的文章.
        //2、文章对应的类别.
        //用的下面这样一条语句:
        $articles = $user->articles->with('category');

        return view('index',  compact('articles'));
    }</code>
Copy after login
Copy after login

Error report:
How to write this related query in laravel?

Question:
How should I write the query statement in the index() method?

Change it to this, change article to a method, so that what is returned is not the result, but the query builder:
$articles = $user->articles()->with('category')->get();

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template