In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use the get find first of the Laravel model", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the get find first of the Laravel model" can help you solve your doubts.
Study time
First of all, create the database table, we do not use migration, go directly to SQL.
CREATE TABLE `about` (`id` int (10) UNSIGNED NOT NULL, `title` varchar (500) COLLATE utf8_unicode_ci NOT NULL, `content` text COLLATE utf8_unicode_ci,) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
There are only three columns, one is the primary key, one is the title, and the other is the text content. Note that the database fields, tables, and utf-8 codes are declared.
Then create the model About and specify the table name. Let's skip it here and look directly at a method of the controller:
Public function index () {$about = About::where ('title',' about-me')-> get (); / / id = 3 return view ('about', compact (' about'));}
The SQL query condition returns all entries based on the title, and then renders the result set through the view view.
Then the point comes, if you write like this in the view, will there be a problem?
@ section ('title') {{$about- > title}} @ stop@section (' content') {! $about- > content!} @ stop
If nothing happens, when you open debug = true to visit the page, you will most likely get the following error prompt:
Property [title] does not exist on this collection instance. (View: e:\ laragon\ www\ newsite\ resources\ views\ about.blade.php)
Let's think about it, how did this failure happen? We will give you an answer in the next section.
Correct writing method
The get () method of the Laravel model returns a collection (EloquentCollection). If you need to use the properties of the collection, you need to traverse it first. Write in the view file as follows:
@ foreach ($collection as $object) {{$object- > title}} @ endforeach
Every element in the EloquentCollection is an About Model object. So you can use $object- > title to get the title attribute.
If your requirements are simple, ask for the title of the first element, abbreviated as follows:
{{$collection [0]-> title}}
If you want to get the first element in the collection, use the first method:
{{$collection- > first ()}} one step further
We know that the problem comes from the get () method, so if you want to get the first piece of data in the query dataset, which one should you use? Find () or first ()!
Will return an About Model object, which can be happily written in the view:
{{$object- > title}} read here, this article "how to use the get find first of the Laravel model" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.