gpt4 book ai didi

php - Laravel 5.2 - Eloquent 模型查询

转载 作者:行者123 更新时间:2023-11-29 21:37:47 25 4
gpt4 key购买 nike

我在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com