Laravel'find'方法返回什么
一个简单的MySQL表,即centers
与Center
作为其模型,code
作为其中一列。Laravel'find'方法返回什么
$center_found=Center::find(6);
echo " code from method 1 = ".$center_found->code;
echo " code from method 1 = ".$center_found['code'];
echo " type = ".gettype($center_found);
使我惊奇的两种方式工作,以找出code
和类型显示为object
。
所以 1)什么是find
的退货类型?
2)返回类型是否有变化,除非没有匹配结果存在?
Model::find()
返回either an instance of Model
or a collection containing one or more instances of Model
。
OP中有2个问题。结果的数据类型是什么? –
@IstiaqueAhmed https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_find – linktoahref
@IstiaqueAhmed:无论是模型还是集合,就像答案所说。 –
从文档中检查此[检索单个模型/聚合](https://laravel.com/docs/5.4/eloquent#retrieving-single-models)。 –
如果'gettype($ center_found)'返回''object'',使用['get_class($ center_found)'](http://php.net/manual/en/function.get-class.php)来找到它类。 – axiac
@axiac,'class'显示为'App \ Customer'。这不回答问题1和2. –