简体   繁体   English

我想从控制器向Laravel的Trait发送变量

[英]I want to send a variable from a Controller to a Trait on Laravel

I have a controller that store the data. 我有一个存储数据的控制器。 This use a form Request and after Show the messages on a trait. 这将使用“请求”表单,然后在“显示特征”消息之后。 I am using the same trait on 3 API but i want to know if is posible send/add to the json/array a Custom Message for each API. 我在3个API上使用相同的特征,但我想知道是否可以为每个API发送/向json / array添加一条自定义消息。 For example if Category created (Category created successfull o Product Created Successfull) according the api. 例如,如果根据api创建了类别(类别创建成功或产品创建成功)。 This is my Store on my controller 这是我在控制器上的商店

    public function store(StoreCategory $request)
{
    $data = request()->all();
    $newCategory = Category::create($data);    
    return $this->respondCreated(new CategoryResource($newCategory)); 
}

I am using CategoryResource like Resource And this is my trait 我正在像资源一样使用CategoryResource这是我的特质

    public function respondCreated($data)
{
    return $this->setStatusCode(IlluminateResponse::HTTP_CREATED)->respond($data
    ->response()      
    );
}
    public function respond($data, $headers = [])
{
    $response = $data;
    return $response->setStatusCode($this->statusCode);        
}

CategoryResource code: CategoryResource代码:

public function toArray($request) {
    return [ 'id' => $this->id,
             'name' => $this->name,
             'parent_id' => $this->parent_id,
             'created_at' => $this->created_at,
             'updated_at' => $this->updated_at,
            ];
} 

Is Possible add a custom message per Api Request? 是否可以为每个Api请求添加自定义消息? May be can i pass a Variable from my controller and after add the custom message add the variable to the array? 可能是我可以从控制器传递变量,并且在添加自定义消息后将变量添加到数组吗?

Best Regards Sorry my english is not good 最好的问候对不起,我的英语不好

You can achieve this by adding attribute to the model directly : link 您可以通过添加属性可以直接在模型实现这一目标: 链接

Occasionally, when casting models to an array or JSON, you may wish to add attributes that do not have a corresponding column in your database. 有时,将模型转换为数组或JSON时,您可能希望添加数据库中没有对应列的属性。 To do so, first define an accessor for the value: 为此,首先为该值定义一个访问器:

public function getHasMessageAttribute()
{
    return "the message that you want to pass";
}

After creating the accessor, add the attribute name to the appends property on the model. 创建访问器后,将属性名称添加到模型的appends属性中。 Note that attribute names are typically referenced in "snake case", even though the accessor is defined using "camel case": 请注意,即使访问器是使用“驼峰大小写”定义的,属性名称也通常在“蛇形大小写”中引用:

{
/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['has_message'];
}

There are many ways to go from here but; 从这里可以走很多路,但是;

and finally pass the message through CategoryResource 最后通过分类资源传递消息

'message'=> $this->has_message,

if you want to separate that from the DB entries you can always use with . 如果要将其与数据库条目分开,则始终可以与一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM