Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Eloquent in Laravel 5 framework

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Eloquent in Laravel 5 framework". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Eloquent in Laravel 5 framework".

A user may have more than one article, and one article is written by a user. This is the relationship. The same article may contain multiple TAG, while a TAG may be associated with multiple articles.

In the project, we already have User.php, the user model, which is quite simple to take a look at. We want to get all the articles directly in the form of $user- > articles (), so let's modify the user model:

Public function articles () {return $this- > hasMany ('App\ Article');}

But we have only completed one end of the relationship, so let's deal with the other end. The form we want is $article- > user (), let's modify the article model.

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

In our database, the article model does not have the user's foreign key, we need to set and modify the create_article_table.php

Schema::create ('articles', function (Blueprint $table) {$table- > increments (' id'); / / specify foreign key column $table- > integer ('user_id')-> unsigned (); $table- > string (' title'); $table- > text ('body'); $table- > timestamp (' published_at'); $table- > timestamps () / / generate foreign key $table- > foreign ('user_id')-> references (' id')-> on ('users')-> onDelete (' cascade');})

Because we are only in the development phase and have not yet been run online, we can directly modify the database migration file, roll back and then migrate, but if we run it online, we should create a new migration.

Php artisan migrate:refresh# output information Rolled back: 2015_03_28_050138_create_article_tableRolled back: 2014_10_12_100000_create_password_resets_tableRolled back: 2014_10_12_000000_create_users_tableNothing to rollback.Migrated: 2014_10_12_000000_create_users_tableMigrated: 2014_10_12_100000_create_password_resets_tableMigrated: 2015_03_28_050138_create_article_tableMigrated: 2015_03_28_051200_add_excerpt_to_articels_table

Now let's use tinker to create a user.

Php artisan tinkerPsy Shell v0.4.1 (PHP 5.4.16-cli) by Justin Hileman# is the execution process > > $user = new App\ User;= > {} > > $user- > name = 'zhang jinglin';= > "zhang jinglin" > $user- > email =' zjl@example.com';= > "zjl@example.com" > $user- > password = bcrypt ('pass'); = > "$2y$10 $Nbl2b9wqd.rXqKEsd3pRSOoIyFAFIhbqf71BufwDfS3Guv21SlEx2" > $user- > save (); = > true > > App\ User::first ()-> toArray () = > ["id" = > "1", "name" = > "zhang jinglin", "email" = > "zjl@example.com", "created_at" = > "2015-03-31 03:24:55", "updated_at" = > "2015-03-31 03:24:55"] >

Now we need to associate the newly published articles with users. For the time being, we modify form_partial.blade.php to hide a user id, which is only temporary:

The copy code is as follows:

{{--temporary processing--}}

{!! Form::hidden ('user_id', 1)!!}

At the same time, we need to modify the $fillabel property of the model so that our Mass Assisment.

Protected $fillable = ['title',' body', 'published_at',' user_id' / / temporary setting]

OK, add an article. Let's use tinker to check it out.

Php artisan tinkerPsy Shell v0.4.1 (PHP 5.4.16-cli) by Justin Hileman > > App\ Article::first ()-> toArray () = > ["id" = > "1", "user_id" = > "1", "title" = > "User 1 Article", "body" = > "User 1 Body", "published_at" = > "2015-03-31 08:00:00", "created_at" = > "2015-03-31 04:17:58", "updated_at" = > "2015-03-31 04:17:58" "excerpt" = > null] # get user > $user = App\ User::first () = > {id: "1", name: "zhang jinglin", email: "zjl@example.com", created_at: "2015-03-31 03:24:55", updated_at: "2015-03-31 03:24:55"} # get user articles > $user- > articles ()-> toArray (); BadMethodCallException with message 'Call to undefined method Illuminate\ Database\ Query\ Builder::toArray () > > $user- > articles- > toArray () = > ["id" = > "1", "user_id" = > "1", "title" = > "User 1 Article", "body" = > "User 1 Body", "published_at" = > "2015-03-31 08:00:00", "created_at" = > "2015-03-31 04:17:58", "updated_at" = > "2015-03-31 04:17:58" "excerpt" = > null]] # Why use $user- > articles instead of # user- > articles ()? # in fact $user- > articles () returns relationships. If you want to use articles (), you need to use > $user- > articles ()-> get ()-> toArray () = > ["id" = > "1", "user_id" = > "1", "title" = > "User 1 Article", "body" = > "User 1 Body", "published_at" = > "2015-03-31 08:00:00", "created_at" = > "2015-03-31 04:17:58", "updated_at" = > "2015-03-31 04:17:58" "excerpt" = > null]] # you can only use articles () to proceed to the next step For example, the following query $user- > articles ()-> where ('title',' User 1 Article')-> get () # We can also get user through article > $article = App\ Article::first () = > {id: "1", user_id: "1", title: "User 1 Article", body: "User 1 Body", published_at: "2015-03-31 08:00:00", created_at: "2015-03-31 04:17:58", updated_at: "2015-03-31 04:17:58", excerpt: null} > > $article- > user = > {id: "1", name: "zhang jinglin", email: "zjl@example.com", created_at: "2015-03-31 03:24:55", updated_at: "2015-03-31 03:24:55"} > Thank you for reading. This is the content of "how to use Eloquent in Laravel 5 Framework". After the study of this article I believe you have a deeper understanding of how to use Eloquent in the Laravel 5 framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report