Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Three minutes takes you through the get find first usage of the Laravel model


Jun 01, 2021 Article blog


Table of contents


Let's first look at the Laravel model, which has two collections, BaseCollection and EloquentCollection Laravel T he latter inherits the former and overloads some methods. Clearly distinguishing the nuances between them will keep you from being confused in programming.

This article goes from a set of examples, from database building tables, writing models, writing controllers, to template rendering, from a small bug to the deep-seated reasons for collections.

Learning time

First create a database table, we don't use migration, directly on 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 3 columns, one is the primary key, one is the title, and one is the text content. Note the database fields, tables, all declared utf-8 encodings.

Then create the model About specify the table name, which we omitted here, and look directly at one of the controller's methods:

public function index()
{
    $about = About::where('title', 'about-me')->get(); //id = 3
    return view('about', compact('about'));
}

SQL query conditions return all entries based on the title, and then render the result set through view view.

Then the point comes, in the view so write everyone to see if there will be a problem!?

@section('title')
    {{$about->title}}
@stop


@section('content')
    {!! $about->content !!}
@stop 

If, unsurprisingly, when you turn on debug = true to access the page, you'll probably 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 how this failure happened. We'll give you an answer in the next section.

Write it correctly

The get() method of the Laravel model returns a collection EloquentCollection I f you need to use the properties of the collection, you first need to traverse them. Write in the view file like this:

@foreach ($collection as $object)
    {{ $object->title }}
@endforeach 

Every element within EloquentCollection is an About Model object. So you can use $object->title to get to title property.

If your needs are simple, it is the title of the first element, as follows:

{{ $collection[0]->title }}

If you want to get the first element within the collection, use the first method:

{{ $collection->first() }}

Go one step further

We know that the problem comes from the get() method, so which one should we use if you want to get the first piece of data for the query dataset? find() or first()

will return an About Model object that you can happily write in the view:

{{ $object->title }}

Using practical examples, this article explains the subtle differences in querying the data result set in find get first laravel model, hoping to help you. Students who want to know more can take a look at the tutorial

Laravel 5 Chinese documents: https://www.w3cschool.cn/qpmsiw/

Laravel getting started: https://www.w3cschool.cn/minicourse/play/laravelpre

Source: www.toutiao.com/a6855259233106002440/