dongyou8368 2018-07-04 17:39
浏览 237

如何通过引用另一个模型来创建新模型对象

I have a function in which I take model as parameter to create slug value from that model values and after creating slug i want to check existence of that in same table.

public static function slug_it($model = null)
{
  if(!empty($model)) {
    #from model
    $keys = [/*priority => key to find*/
        1 => 'name', 2 => 'item', 4 => 'title', 3 => 'code', 5 => 'company', 6 => 'code_name', 7 => 'country', 8 => 'city', 9 => 'state', 10 => 'username', 11 => 'first_name', 12 => 'last_name',];
    /* suppose i have slug */
    $slug = 'item_erp5511'; 

    /*slug or sku*/
  } else {

  }
}

Suppose Model is of item table and I want to find slug in "slug" col of that table but i only have Model Object of that table.

  • 写回答

1条回答 默认 最新

  • donglin4636 2018-07-04 17:53
    关注

    You can try something like the following, presumption, $model = Item::class instance and you want to find something from the slugs table.

    // $model = Item::find(1);
    $model->getQuery()->from('slugs')->where('slug', $slug)->get();
    

    Alternatively if slug_it is inside a model you can try:

    self::getQuery()->from('slugs')->....->get();
    

    Update after your latest comment

    Inside the method to do a check you can do:

    if (self::where('slug', $slug)->exists()) {
        // Do what you need
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向