laravel 间接修改Illuminate\Support\Collection的重载元素没有效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44539395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indirect modification of overloaded element of Illuminate\Support\Collection has no effect
提问by Kelvin
im quite new in laravel framework, and im from codeigniter.
我在 Laravel 框架中很新,我来自 codeigniter。
I would like to add new key and value from database
我想从数据库中添加新的键和值
static function m_get_promotion_banner(){
$query = DB::table("promotion_banner")
->select('promotion_banner_id','promotion_link','about_promotion')
->where('promotion_active','1')
->get();
if($query != null){
foreach ($query as $key => $row){
$query[$key]['promotion_image'] = URL::to('home/image/banner/'.$row['promotion_banner_id']);
}
}
return $query;
}
that code was just changed from codeigniter to laravel, since in codeigniter there are no problem in passing a new key and value in foreach
statement
该代码刚刚从 codeigniter 更改为 laravel,因为在 codeigniter 中,在foreach
语句中传递新的键和值没有问题
but when i tried it in laravel i got this following error :
但是当我在 Laravel 中尝试时,出现以下错误:
Indirect modification of overloaded element of
Illuminate\Support\Collection
has no effectat HandleExceptions->handleError(8, 'Indirect modification of overloaded element of Illuminate\Support\Collection has no effect', 'C:\xampp\htdocs\laravel-site\application\app\models\main\Main_home_m.php', 653, array('query' => object(Collection), 'row' => array('promotion_banner_id' => 1, 'promotion_link' => 'http://localhost/deal/home/voucher', 'about_promotion' => ''), 'key' => 0))
间接修改重载元素的
Illuminate\Support\Collection
无效在 HandleExceptions->handleError(8, 'Illuminate\Support\Collection 的重载元素的间接修改无效', 'C:\xampp\htdocs\laravel-site\application\app\models\main\Main_home_m.php', 653,数组('查询' => 对象(集合),'行' => 数组('promotion_banner_id' => 1,'promotion_link' => ' http://localhost/deal/home/voucher','about_promotion' => ''), '键' => 0))
please guide me how to fix this
请指导我如何解决这个问题
thank you (:
谢谢你 (:
采纳答案by Jerodev
The result of a Laravel query will always be a Collection. To add a property to all the objects in this collection, you can use the map
function.
Laravel 查询的结果将始终是一个 Collection。要将属性添加到此集合中的所有对象,您可以使用该map
函数。
$query = $query->map(function ($object) {
// Add the new property
$object->promotion_image = URL::to('home/image/banner/' . $object->promotion_banner_id);
// Return the new object
return $object;
});
Also, you can get and set the properties using actual object properties and not array keys. This makes the code much more readable in my opinion.
此外,您可以使用实际对象属性而不是数组键来获取和设置属性。在我看来,这使代码更具可读性。
回答by Amarnasan
The problem is the get
is returning a collection
of stdObject
问题是get
返回一个collection
的stdObject
Instead of adding the new field to the result of your query, modify the model of what you are returning.
不要将新字段添加到查询结果中,而是修改您返回的内容的模型。
So, assuming you have a PromotionBanner.php model file in your app
directory, edit it and then add these 2 blocks of code:
因此,假设您的app
目录中有一个 PromotionBanner.php 模型文件,请对其进行编辑,然后添加以下 2 个代码块:
protected $appends = array('promotionImage');
here you just added the custom field. Now you tell the model how to fill it:
在这里,您刚刚添加了自定义字段。现在你告诉模型如何填充它:
public function getPromotionImageAttribute() {
return (url('home/image/banner/'.$this->promotion_banner_id));
}
Now, you get your banners through your model:
现在,您可以通过模型获取横幅:
static function m_get_promotion_banner(){
return \App\PromotionBanner::where('promotion_active','1')->get();
}
Now you can access your promotionImage
propierty in your result
现在您可以promotionImage
在结果中访问您的属性
P.D: In the case you are NOT using a model... Well, just create the file app\PromotionImage.php:
PD:如果你没有使用模型......好吧,只需创建文件 app\PromotionImage.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PromotionImage extends Model
{
protected $appends = array('imageAttribute');
protected $table = 'promotion_banner';
public function getPromotionImageAttribute() {
return (url('home/image/banner/'.$this->promotion_banner_id));
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'promotion_banner_id','promotion_link','about_promotion','promotion_active'
];