drbouzlxb92333332 2014-04-03 23:58
浏览 25
已采纳

Laravel 4 - 获取模型属性的子集

I have this laravel model method call:

$entry = self::firstOrCreate(array('url' => $url, 'version' => $version));

But it has a problem: One of the columns is a TEXT column which could hold really large data I don't need (in this context).

What do I need: A way to load-or-create this model without loading the content (TEXT) data.

Question: Is there a model method I could use in place of this, or a parameter I'm missing?

  • 写回答

1条回答 默认 最新

  • douyaju4259 2014-04-04 00:54
    关注

    Not using firstOrCreate as you'll need to issue some custom selects. I'd suggest using firstOrFail, catch the exception and then create a new model.

    $payload = compact('url', 'version'); // Build an associative array out of the variables.
    
    try
    {
        $entry = Model::where('url', $url)->where('version', $version)->firstOrFail(array_keys($payload));
    }
    catch (Illuminate\Database\Eloquent\ModelNotFoundException $e)
    {
        $entry = Model::create($payload);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制