php - Laravel 5.2 - Eloquent 模型查询

标签 php mysql eloquent laravel-5

我在尝试使用 Eloquent 模型执行真正基本的查询时遇到了麻烦,其中 createfind 都可以正常工作,但是 where不返回任何内容。

型号:

namespace App;
use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
protected $table = 'companies';
protected $fillable = ['name', 'ticker', 'industry', 'sector', 'description'];
protected $visible = ['name', 'ticker', 'industry', 'sector', 'description'];
}

Controller :

创建工作正常:

\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);

在数据库上创建一个新行:

(id, name, ticker)

1, Apple Inc, AAPL

查找工作正常:

$company = \App\Company::find(1);
print_r($company);

返回

App\Company Object ( [table:protected] => companies [fillable:protected] => Array ( [0] => name [1] => ticker [2] => industry [3] => sector [4] => description ) [visible:protected] => Array ( [0] => name [1] => ticker [2] => industry [3] => sector [4] => description ) [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [id] => 1 [name] => Apple Inc [ticker] => AAPL [industry] => [sector] => [description] => [created_at] => 2016-01-14 00:01:35 [updated_at] => 2016-01-14 00:01:35 ) [original:protected] => Array ( [id] => 1 [name] => Apple Inc [ticker] => AAPL [industry] => [sector] => [description] => [created_at] => 2016-01-14 00:01:35 [updated_at] => 2016-01-14 00:01:35 ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [appends:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [dateFormat:protected] => [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] => )

<小时/>

现在,当我尝试按 id 以外的任何字段进行搜索时,它根本不会返回任何内容:

$company = \DB::table('companies')->select('ticker', 'name')->where('ticker', '=', 'AAPL')->get();
print_r($company);

返回一个空数组:

Array ( )

两者:

$company = \App\Company::find(['ticker', 'AAPL'])->first();
print_r($company);

和:

$company = \App\Company::where('ticker', '=', 'AAPL')->first();
print_r($company);

它们根本不返回任何内容。

---更新---

迁移:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCompaniesTable extends Migration
{   
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('ticker');
            $table->string('industry')->nullable();
            $table->string('sector')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('companies');
    }
}

最佳答案

已修复:

我从其他网站上阅读的代码带有奇怪的字符(感谢@ceejayoz,感谢您的帮助)

手动创建一个公司工作得很好,因为我对股票代码进行了“硬编码”:

\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);

对于我从其他网站上删除的公司,在存储之前执行此操作可以达到目的:

str_replace(array('.', ' ', "\n", "\t", "\r"), '', $ticker);

显然,如果代码有进位和/或零长度空格,并且我使用修剪后的代码进行查询,结果将为空。一旦更新了代码,模型就可以正常工作:

$company = \App\Company::where('ticker', '=', $ticker)->first();

关于php - Laravel 5.2 - Eloquent 模型查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779734/

相关文章:

php - 使用上传的 CSV 中的值创建/填写表单

php - 如何通过为所有条目添加自定义名称来更新所有记录

MySQL group_concat 从两个表返回值(包括 NULL)

laravel - Eloquent (Laravel 5) - 如何在关系查询 (whereHas) 中包含 SoftDeleted 记录?

php - 尝试在 null 上读取属性 "name"

php - 基于列的唯一行

php - 如何从 Yii 中的模型访问 Controller 功能?

mysql - 根据输入参数查询表

mysql - Laravel 4 - 如何设置多个外键

php - 将目录路径转换为javascript对象