Laravel 5.2 - 雄辩的模型查询(Laravel 5.2 - Eloquent Model Query)
我在使用Eloquent模型尝试执行一个非常基本的查询时遇到了麻烦,其中创建和查找都工作正常,但没有返回任何内容。
模型:
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']);
在DB上创建一个新行:
(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 [递增] => 1 [时间戳] => 1 [属性:受保护] =>数组([id] => 1 [名称] => Apple Inc [ticker] => AAPL [行业] => [部门] => [描述] => [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)[关系:保护] =>数组()[隐藏:保护] =>数组()[追加:保护] => 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);
返回一个空数组:
数组()
两者:
$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'); } }
I'm having trouble trying to perform a really basic query using an Eloquent Model, where create and find both work fine but where returns nothing.
Model:
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:
Create works fine:
\App\Company::create(['ticker'=>'AAPL', 'name' => 'Apple Inc']);
creates a new row on the DB:
(id, name, ticker)
1, Apple Inc, AAPL
Find works fine:
$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] => )
Now when I try to search by any field other than the id it simply doesn't return anything:
$company = \DB::table('companies')->select('ticker', 'name')->where('ticker', '=', 'AAPL')->get(); print_r($company);
returns an empty array:
Array ( )
And both:
$company = \App\Company::find(['ticker', 'AAPL'])->first(); print_r($company);
and:
$company = \App\Company::where('ticker', '=', 'AAPL')->first(); print_r($company);
they simply return nothing at all.
---Update---
Migration:
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'); } }
原文:https://stackoverflow.com/questions/34779734
最满意答案
全局方法也是
window
属性,因此您可以执行以下操作:if (window.Uint8Array && typeof window.Uint8Array === 'function')
Global methods are also properties of the
window
so you can do things like:if (window.Uint8Array && typeof window.Uint8Array === 'function')
相关问答
更多-
我正在努力解决完全相同的问题。 当我使用ArrayBuffer备份Uint8Array时, 它在Safari和Chrome中都能正常工作 (尚未在其他浏览器中测试过),但Chrome会打印一条警告消息。 Chrome说我必须在将DataBuffer传递给Blob()构造函数之前将其包装在DataView中。 // write the bytes of the string to an ArrayBuffer var ab = new ArrayBuffer(byteString.length); var i ...
-
看看Uint8ClampedArray和Uint8Array的例子,看起来区别是如何处理分配值。 如果您尝试将一个元素设置为一个钳位数组,超出范围0-255值,则它将默认为0或255(取决于该值是否较小)。 一个正常的Uint8Array数组只是取值的前8位。 例子: var x = new Uint8ClampedArray([17, -45.3]); console.log(x[0]); // 17 console.log(x[1]); // 0 console.log(x.length); // 2 ...
-
来自编码标准的 TextEncoder和TextDecoder由stringencoding库进行 TextDecoder ,可以在字符串和ArrayBuffers之间进行转换: var uint8array = new TextEncoder("utf-8").encode("¢"); var string = new TextDecoder("utf-8").decode(uint8array); TextEncoder and TextDecoder from the Encoding standa ...
-
AFAIK需要将条目数作为参数传递给构造函数。 const ar = new Uint8Array(2); ar[0] = 'G'; ar[1] = 0x123; console.log(ar[1]); if(ar[1] > 0){ console.log("OK"); } else{ console.log("NOP") } .as-console-wrapper { max-height: 100% !important; top: 0; } 编辑:再次阅读文档,您可以 ...
-
参看 BLOB构造函数的文档: https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob [第一个参数]是一个ArrayBuffer,ArrayBufferView,Blob,DOMString对象的数组,或任何这些对象的混合,将放在Blob中。 DOMStrings编码为UTF-8。 我不确定它是如何工作的,但基本上构造函数需要一些它将打包到BLOB中的东西。 因此,在第一种情况下,您构建单个部件的BLOB(即您的ArrayBuffer),而在第 ...
-
Uint *** Arrays构造非类型数组(由@zfor注释 ,因此,例如, push将是undefined )仅包含数字(仍为字节)。 不同之处在于每个构造函数数组在内存中具有不同的字节范围。 Uint8Array只有1个字节,那么数字的限制是255 。 Uint16Array长2个字节,然后限制为65535 。 Uint32Array长4个字节,因此限制为4294967295 。 构造Uint * Array时,将数组长度声明为第一个参数: var arr = new Uint8Array(1); ...
-
该函数将ArrayBuffer转换为Float32Array var convertBlock(buffer) { // incoming data is an ArrayBuffer var incomingData = new Uint8Array(buffer); // create a uint8 view on the ArrayBuffer var i, l = incomingData.length; // length, we need this for the loop ...
-
这是因为Bson.serialize和Bson.deserialize使用像Buffer这样的Bson.deserialize 您可以通过使用底层ArrayBuffer来创建Uint8Array ( 在线测试 )并使用然后使用Buffer.from : var personJSON = { 'name': 'sarah' } // JSON var personBuffer = new Uint8Array(Bson.serialize(personJSON).buffer) // Uint8Array v ...
-
全局方法也是window属性,因此您可以执行以下操作: if (window.Uint8Array && typeof window.Uint8Array === 'function') Global methods are also properties of the window so you can do things like: if (window.Uint8Array && typeof window.Uint8Array === 'function')
-
我有答案。 你必须创建一个js文件。 在这一个贴这个代码: (function() { try { var a = new Uint8Array(1); return; //no need } catch(e) { } function subarray(start, end) { return this.slice(start, end); } function set_(array, offset) { if (arguments.length < 2) offset = 0; for (var i ...