A problem when using Laravel ORM?

WBOY
Release: 2016-08-15 18:01:48
Original
916 people have browsed it

Reply content:

That's not how Laravel works.

app/Models/User.php:

<code class="language-php"><span class="cp"><?php</span>

<span class="k">namespace</span> <span class="nx">App\Models</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Illuminate\Database\Eloquent\Model</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">User</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">cars</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">hasMany</span><span class="p">(</span><span class="nx">Car</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code>
Copy after login
Oh my God...
Those cars of mine, and those are my cars...are you still arguing about this difference? Are there too few projects?

You need to have both and use both. Is there any conflict between the two? There won't be any problem if "those cars of mine" and "those are my cars" appear separately in an article, right, as long as the context is appropriate. You are already using ORM, but you are still struggling with this issue? ORM allows you not to worry about what happens in the internal sql. Use the first method and wait until the project business is fully developed before considering optimization. Personally, I think the second one is better. According to active_record, it should be written in user.
Because cars is an attribute of user and should be obtained from user.
As for adding complex queries in the future, it cannot be expanded if placed in user? ? ?



I recommend your group to read this
A problem when using Laravel ORM?

A problem when using Laravel ORM?


There is also a third volume, but there is no Chinese version If you write both methods, they will definitely work. I think from an understanding point of view,
$user->myCars()
is more complex with human thinking than
$car->list($userId)
Are you using laravel? You can look at the example on the first floor. Is there really any point in arguing about this? It’s so low
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!