php Laravel 的 Response 内容必须是字符串或对象,实现了 __toString(),给定的“对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31771276/
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
Laravel The Response content must be a string or object implementing __toString(), "object" given
提问by Alameddin ?elik
I want to run Skills function but I cant it.
我想运行技能功能,但我不能。
Route.php
路由.php
Route::get('setting',function(){
return \App\User::first()->skills();
});
User.php
用户名
protected $casts = [
'skills' => 'json'
];
public function skills(){
return new Skills($this , $this->skills);
}
Skills.php
技能.php
namespace App;
use App\User;
use Mockery\Exception;
class Skills
{
protected $user;
protected $skills = [];
public function __construct(User $user,array $skills){
$this->user=$user;
$this->skills=$skills;
}
}
I want to enter /settings page I have "The Response content must be a string or object implementing __toString(), "object" given.
" error.
我想进入 /settings 页面我有“ The Response content must be a string or object implementing __toString(), "object" given.
”错误。
I tried to add dd()
function's return in route, I see all JSON data but $skills->get()
, $skill->set()
didn't working at the time.
我尝试添加dd()
函数的返回路线中,我看到所有的JSON数据,但是$skills->get()
,$skill->set()
在当时没有工作。
Edit:
编辑:
Skills.php
技能.php
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 01.08.2015
* Time: 11:45
*/
namespace App;
use App\User;
use Mockery\Exception;
class Skills
{
protected $user;
protected $skills = [];
public function __construct(User $user,array $skills){
$this->user=$user;
$this->skills=$skills;
}
public function get($key){
return array_get($this->skills,$key);
}
public function set($key,$value){
$this->skills[$key]=$value;
return $this->duration();
}
public function has($key){
return array_key_exists($key,$this->skills);
}
public function all(){
return $this->skills;
}
public function merge(array $attributes){
$this->skills = array_merge(
$this->skills,
array_only(
$attributes,
array_keys($this->skills)
)
);
return $this->duration();
}
public function duration(){
return $this->user->update(['skills' => $this->skills]);
}
public function __get($key){
if ($this->has($key)) {
return $this->get($key);
}
throw new Exception("{$key} adl? Yetenek bulunamad?");
}
}
采纳答案by jedrzej.kurylo
When you do
当你做
return \App\User::first()->skills();
you are returning the Relation definition object, which doesn't implement __toString()method. What you need in order to return the related object is
您正在返回未实现__toString()方法的 Relation 定义对象。为了返回相关对象,您需要的是
return \App\User::first()->skills;
This will return a Collectionobject cotaining related skills - this will be properly serialized.
这将返回一个包含相关技能的Collection对象 - 这将被正确序列化。
回答by itsazzad
May be you could go with get_object_vars($skills)
and later loop
through the object variables.
Example:
可能您可以使用get_object_vars($skills)
并稍后loop
通过对象变量。例子:
foreach(get_object_vars($skills) as $prop => $val){
}
回答by nensamuel
in the Route.php, return a collection like so
在Route.php 中,像这样返回一个集合
Route::get('setting',function(){
return \App\User::first()->skills;
});
OR
或者
Route::get('setting',function(){
$skills = \App\User::first(['id','skills']);
return $skills->skills;
});
And on you User.php model, you can factor your code say so
在你的 User.php 模型上,你可以考虑你的代码这样说
protected $casts = [
'skills' => 'json'
];
public function skills()
{
return $this->skills;
}
I hope this helps someone.
我希望这可以帮助别人。