Laravel 五 基礎(chǔ)(七)- Eloquent (laravel 的ORM)
Jun 13, 2016 pm 12:17 PM
Laravel 5 基礎(chǔ)(七)- Eloquent (laravel 的ORM)
- 我們來(lái)生成第一個(gè)模型
<code>php artisan make:model Article#輸出Model created successfully.Created Migration: 2015_03_28_062517_create_articles_table</code>
查看一下生成的文件 app/Article.php
<code><?php namespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model { //}</code>
沒(méi)什么特別的,除了繼承自 Model 以外,但是具有強(qiáng)大的功能,這些都封裝在laravel的Model中。模型自動(dòng)具有了 save() update() findXXX()
等強(qiáng)大的功能。
- tinker 是 laravel提供的命令行工具,可以和項(xiàng)目進(jìn)行交互。
<code>php artisan tinker#以下是在tinker中的交互輸入Psy Shell v0.4.1 (PHP 5.4.16 — cli) by Justin Hileman>>> $name = 'zhang jinglin';=> "zhang jinglin">>> $name=> "zhang jinglin">>> $article = new App\Article;=> <App\Article #000000005c4b7ee400000000ab91a676> {}>>> $article->title = 'My First Article';=> "My First Article">>> $article->body = 'Some content...';=> "Some content...">>> $article->published_at = Carbon\Carbon::now();=> <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" }>>> $article;=> <App\Article #000000005c4b7ee400000000ab91a676> { title: "My First Article", body: "Some content...", published_at: <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } }>>> $article->toArray();=> [ "title" => "My First Article", "body" => "Some content...", "published_at" => <Carbon\Carbon #000000005c4b7ee600000000ab91dcb6> { date: "2015-03-28 06:37:22", timezone_type: 3, timezone: "UTC" } ]>>> $article->save();=> true#查看數(shù)據(jù)結(jié)果,添加了一條記錄>>> App\Article::all()->toArray();=> [ [ "id" => "1", "title" => "My First Article", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:38:53" ] ]>>> $article->title = 'My First Update Title';=> "My First Update Title">>> $article->save();=> true>>> App\Article::all()->toArray();=> [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ] ] >>> $article = App\Article::find(1);=> <App\Article #000000005c4b7e1600000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" }>>> $article = App\Article::where('body', 'Some content...')->get();=> <Illuminate\Database\Eloquent\Collection #000000005c4b7e1800000000ab91a676> [ <App\Article #000000005c4b7e1b00000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" } ]>>> $article = App\Article::where('body', 'Some content...')->first();=> <App\Article #000000005c4b7e1900000000ab91a676> { id: "1", title: "My First Update Title", body: "Some content...", published_at: "2015-03-28 06:37:22", created_at: "2015-03-28 06:38:53", updated_at: "2015-03-28 06:42:03" }>>> >>> $article = App\Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);Illuminate\Database\Eloquent\MassAssignmentException with message 'title'</code>
MassAssignmentException,laravel保護(hù)我們不能直接插入記錄。比如,在一些特殊情況下我們需要直接利用表單的信息填充數(shù)據(jù)庫(kù)記錄,但是如果我們并沒(méi)有在表單中添加密碼字段,而黑客產(chǎn)生了密碼字段連同我們的其他字段一起送回服務(wù)器,這將產(chǎn)生修改密碼的危險(xiǎn),所以我們必須明確的告訴laravel我們的模型那些字段是可以直接填充的。
修改我們的模型文件 Article.php
<code><?php namespace App;use Illuminate\Database\Eloquent\Model;class Article extends Model { protected $fillable = [ 'title', 'body', 'published_at' ];}</code>
表示,title, body, published_at 是可以直接填充的。
退出 tinker,重新進(jìn)入
<code>>>> $article = App\Article::create(['title' => 'New Article', 'body' => 'New body', 'published_at' => Carbon\Carbon::now()]);=> <App\Article #000000005051b2c7000000007ec432dd> { title: "New Article", body: "New body", published_at: <Carbon\Carbon #000000005051b2c6000000007ec4081d> { date: "2015-03-28 06:55:19", timezone_type: 3, timezone: "UTC" }, updated_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", id: 2 } # It's ok>>> App\Article::all()->toArray();=> [ [ "id" => "1", "title" => "My First Update Title", "body" => "Some content...", "published_at" => "2015-03-28 06:37:22", "created_at" => "2015-03-28 06:38:53", "updated_at" => "2015-03-28 06:42:03" ], [ "id" => "2", "title" => "New Article", "body" => "New body", "published_at" => "2015-03-28 06:55:19", "created_at" => "2015-03-28 06:55:19", "updated_at" => "2015-03-28 06:55:19" ] ]>>> $article = App\Article::find(2);=> <App\Article #000000005051b22b000000007ec432dd> { id: "2", title: "New Article", body: "New body", published_at: "2015-03-28 06:55:19", created_at: "2015-03-28 06:55:19", updated_at: "2015-03-28 06:55:19" }>>> $article->update(['body' => 'New Updaet Body']);=> true#update自動(dòng)調(diào)用save()</code>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Title is the meaning that defines the title of the web page. It is located within the tag and is the text displayed in the title bar of the browser. Title is very important for the search engine optimization and user experience of the web page. When writing HTML web pages, you should pay attention to using relevant keywords and attractive descriptions to define the title element to attract more users to click and browse.

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出php提交表單通過(guò)后,彈出的對(duì)話框怎樣在當(dāng)前頁(yè)彈出而不是在空白頁(yè)彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁(yè)彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

The title in HTML displays the title tag of the web page, which allows the viewer to know what the current page is about, so each web page should have a separate title.

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers
