如何根据请求的条件雄辩地在laravel中添加或删除属性

时间:2019-05-30 07:25:10

标签: laravel eloquent

我有多种帖子类型的帖子模型。我需要通过帖子类型控制帖子属性。例如,我有2种帖子类型:

  • 歌曲
  • 歌手

仅一次建模一次发布

后期模型方案:

id
title
description
parent_id
type

请求song帖子类型时,如何自动将属性singer添加到响应数组中,或者当请求singer帖子类型添加属性songs时如何

我在使用__construct()方法的模型内部感到累了,但没有用:

public function __construct() {
    if($this->type == "song") {
        //$this->with = array_push($this->with, "singer");
        $this->appends = array_push($this->appends, 'singer');
    }
}

1 个答案:

答案 0 :(得分:0)

/*
 * ext_current () -- Defines the Xpath extension current(),
 * defined by Yang RFC.
 *
 * From Yang RFC 7950:
 *
 * The current() function takes no input parameters and returns a node
 * set with the initial context node as its only member.
 */
static void
ext_current (xmlXPathParserContextPtr ctxt, int nargs)
{
    /*
     * This function takes 0 args.
     */
    if (nargs != 0) {
        return;
    }

    /* Push the current context node onto Xpath Stack */
    valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
}

/*
 * register_yang_xpath_extensions () -- Registers extensions defined by Yang
 * RFC.
 */
static void
register_yang_xpath_extensions (xmlXPathContextPtr ctxt)
{
    int rc = 0;

    rc = xmlXPathRegisterFunc(ctxt, (const xmlChar *)"current",
                              ext_current);
    if (rc != 0)
        fprintf(stderr, "Error in registering current() func\n");
}