Laravel 5.2 - Eloquent 模型查询
Laravel 5.2 - Eloquent Model Query
我在尝试使用 Eloquent 模型执行非常基本的查询时遇到问题,其中 create 和 find两者都工作正常,但 where returns nothing.
型号:
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'];
}
控制器:
创建 工作正常:
\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);
在数据库上创建一个新行:
(id, name, ticker)
1, Apple Inc, AAPL
Find 工作正常:
$company = \App\Company::find(1);
print_r($company);
returns
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 以外的任何字段进行搜索时,它 return 什么都没有:
$company = \DB::table('companies')->select('ticker', 'name')->where('ticker', '=', 'AAPL')->get();
print_r($company);
returns 一个空数组:
Array ( )
并且两者:
$company = \App\Company::find(['ticker', 'AAPL'])->first();
print_r($company);
和:
$company = \App\Company::where('ticker', '=', 'AAPL')->first();
print_r($company);
他们只是return什么都没有。
---更新---
迁移:
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,感谢您的帮助)
手动创建 Company
效果很好,因为我是 'hard coding' 代码:
\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);
至于我从其他网站上删除的公司,在存储之前这样做就可以了:
str_replace(array('.', ' ', "\n", "\t", "\r"), '', $ticker);
显然,如果自动收报机有一个进位 and/or zero-length 空格,并且我正在使用经过修剪的自动收报机查询结果为空。更新代码后,Model
适用于:
$company = \App\Company::where('ticker', '=', $ticker)->first();
我在尝试使用 Eloquent 模型执行非常基本的查询时遇到问题,其中 create 和 find两者都工作正常,但 where returns nothing.
型号:
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'];
}
控制器:
创建 工作正常:
\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);
在数据库上创建一个新行:
(id, name, ticker)
1, Apple Inc, AAPL
Find 工作正常:
$company = \App\Company::find(1);
print_r($company);
returns
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 以外的任何字段进行搜索时,它 return 什么都没有:
$company = \DB::table('companies')->select('ticker', 'name')->where('ticker', '=', 'AAPL')->get();
print_r($company);
returns 一个空数组:
Array ( )
并且两者:
$company = \App\Company::find(['ticker', 'AAPL'])->first();
print_r($company);
和:
$company = \App\Company::where('ticker', '=', 'AAPL')->first();
print_r($company);
他们只是return什么都没有。
---更新---
迁移:
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,感谢您的帮助)
手动创建 Company
效果很好,因为我是 'hard coding' 代码:
\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);
至于我从其他网站上删除的公司,在存储之前这样做就可以了:
str_replace(array('.', ' ', "\n", "\t", "\r"), '', $ticker);
显然,如果自动收报机有一个进位 and/or zero-length 空格,并且我正在使用经过修剪的自动收报机查询结果为空。更新代码后,Model
适用于:
$company = \App\Company::where('ticker', '=', $ticker)->first();