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.