Laravel How to query the fields specified in a table when eloquent ORM

Source: Internet
Author: User
Tags mixed

Guide: In the use of the Laravel ORM model method Find, GET, the "the" the data object returned when the Attributes property array of Data objects will contain all the fields in the data table corresponding ... Original address: http://www.bcty365.com/content-153-5939-1.html

When using the Laravel ORM Model method Find, get, the Attributes property array of the data object returned when the method gets the data object contains the key value relationships for all the fields in the datasheet. So how do you only return data from a specified field in a datasheet when you orm a query? Many times, the document does not specify the use of the need to see the source code to explore, let's look at the implementation of these three methods.

Because ORM relies on QueryBuilder to implement the query, in QueryBuilder's source code, by looking at the implementation of the Get,first method, they can receive an array parameter to specify the fields to query:

The Find method is implemented in the \illuminate\database\eloquent\builder class, as follows:

/** 
 * Find a model by its primary key. 
 *www.bcty365.com
 * @param  mixed  $id 
 * @param  array  $columns 
 * @return \illuminate\ Database\eloquent\model|\illuminate\database\eloquent\collection|null 
 * 
/Public Function Find ($id, $ columns = [' * ']) 
{  if (Is_array ($id)) {return  $this->findmany ($id, $columns); 
 c17/>}  $this->query->where ($this->model->getqualifiedkeyname (), ' = ', $id);  return $this->first ($columns); 

Since the eloquent query Builder is dependent on the Query Builder \illuminate\database\query\builder, the source code for the A/a method is as follows in Query Builder:

/** 
 * Execute the query and get the. 
 * * 
 @param  array $columns 
 * @return mixed|static 
/Public Function ($columns = [' * ']) 
c9/>  $results = $this->take (1)->get ($columns);  return count ($results) > 0? Reset ($results): null; 
} 
 
/** 
 * Execute the query as a "select" statement. 
 * 
 * @param  array  $columns 
 * @return array|static[] 
 * 
/Public function get ($columns = [' * ']) 
{  if (is_null ($this->columns)) {  $this->columns = $columns;  } return  $this->processor->processselect ($this, $this->runselect ()); 

So using Laravel's Orm method query to return the specified field can be done in the following three ways

$data = Modela::find ($id, [' Column1 ', ' Column2 ']); 
 
$data = Modela::first ([' Column1 ', ' Column2 ']); 
 

In different scenarios, the three choose to meet the needs of the use can be.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.