简体   繁体   English

如何使用where子句从Laravel 4中的模型中选择数据

[英]How to select data from model in Laravel 4 with a where clause

I have been trying to figure out how to do a simple select where on a table for a few hours now. 我已经尝试了几个小时,现在想弄清楚如何在桌子上的一个简单位置进行选择。 I was trying to follow this page but nothing I seem to try is working. 我正在尝试关注页面,但似乎无法正常工作。 Here is my model code with nothing in it. 这是我的模型代码,其中没有任何内容。

<?php

namespace App\Models;

use BaseModel;

class Redemption extends BaseModel {


}

And here is the code I am trying to use in my controller. 这是我尝试在控制器中使用的代码。

$row = RedemptionModel::where('code', '=', Input::get('redemption_code'))->get();
var_dump($row);exit;

And here is the resulting data 这是结果数据

object(Illuminate\Database\Eloquent\Collection)#162 (1) { ["items":protected]=> array(0) { } }

I know that the db is working fine and the model is working fine because if I do RedemptionModel::find(1) that works. 我知道数据库工作正常,模型工作正常,因为如果我这样做的话,那么RedemptionModel::find(1)可以正常工作。 Also, if you know any good places to find ORM Laravel code with better examples please let me know. 另外,如果您找到找到具有更好示例的ORM Laravel代码的好地方,请告诉我。

get fetches a Collection object, which is essentially an array of items, even if there's only one result. get获取一个Collection对象,该对象本质上是一组项目,即使只有一个结果也是如此。 In other words, your $row is actually row* s *. 换句话说,您的$row实际上是row * s *。

If you want a single result, you can use first() instead of get() . 如果只需要一个结果,则可以使用first()代替get()

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

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