【问题标题】:how to select a single row from table, the model of which has function如何从表中选择单行,其模型具有功能
【发布时间】:2016-06-12 03:31:08
【问题描述】:
class CreditRule extends Model
{
    protected $fillable = array(
        'department_id',
        'created_by',
        'updated_by'
    );

    public function department()
    {
        return $this->belongsTo('App\Department');
    }

我的控制器是

 $creditRules = CreditRule::get();

【问题讨论】:

  • 嗨,Eliza,您能否提供更多信息,说明您正在尝试完成什么、您遇到的问题以及您已经尝试过哪些没有奏效?

标签: html laravel


【解决方案1】:

通过id获取模型:

$element = Model::find(id)

通过匹配一个字段获取模型的所有行:

$element = Model::where('field','=',$value)->get()

或者只获取第一个:

$element = Model::where('field','=',$value)->first()

【讨论】:

    【解决方案2】:

    @Eliza,您可以在控制器中使用where,同时访问模型中的数据。

    where的简单结构是:

    $creditRules = CreditRule::where('id', 1)->first();
    

    【讨论】:

      【解决方案3】:

      你的意思是这样的

      CreditRule::find(1);
      

      这将检索模型 CreditRule 中 id=1 的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-18
        • 2021-04-18
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 2015-07-06
        • 2012-02-06
        • 1970-01-01
        相关资源
        最近更新 更多