Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use get find first of Laravel Model

Shulou Source: shulou.com Published: 2022-06-01 11:23:56 10月04日 Update

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.

Tags: Model data method view element content article title object attribute database problem learning query proper pleasant no accident writing only field Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology OPPO Reno Shulou Tech Info Microsoft