加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel Response内容必须是实现__toString()的字符串或

发布时间:2020-12-14 19:48:15 所属栏目:大数据 来源:网络整理
导读:我想运行技能功能,但我不能. Route.php Route::get('setting',function(){ return AppUser::first()-skills();}); user.php的 protected $casts = [ 'skills' = 'json'];public function skills(){ return new Skills($this,$this-skills);} Skills.php na
我想运行技能功能,但我不能.

Route.php

Route::get('setting',function(){
    return AppUser::first()->skills();
});

user.php的

protected $casts = [
    'skills' => 'json'
];

public function skills(){
    return new Skills($this,$this->skills);
}

Skills.php

namespace App;
use AppUser;
use MockeryException;

class Skills
{
    protected $user;
    protected $skills = [];

    public function __construct(User $user,array $skills){

        $this->user=$user;
        $this->skills=$skills;
    }
}

我想进入/设置页面我有“响应内容必须是实现__toString()的字符串或对象,”对象“给出.”错误.

我尝试在路由中添加dd()函数的返回,我看到所有JSON数据但是$skills-> get(),$skill-> set()当时没有工作.

编辑:

Skills.php

<?php
    /**
     * Created by PhpStorm.
     * User: root
     * Date: 01.08.2015
     * Time: 11:45
     */

    namespace App;
    use AppUser;
    use MockeryException;

    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?");
        }
    }
当你这样做
return AppUser::first()->skills();

您正在返回Relation定义对象,该对象未实现__toString()方法.返回相关对象需要的是

return AppUser::first()->skills;

这将返回一个包含相关技能的Collection对象 – 这将被正确序列化.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读