我看到很多人在使用预加载和 Laravel 时遇到问题,我也是其中之一。 我收到错误:尝试获取非对象的属性
解决方案:我需要在我的模型中包含 public $incrementing = false;。请参阅底部的扩展答案
我查看了其他堆栈溢出答案,但没有一个解决了这个问题:这是所有我的代码
我的 2 个迁移文件
创建作者表
<?php
# database/migrations/1970_01_01_000001_create_authors_table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->string('author_id');
$table->string('first_name');
$table->string('last_name');
$table->timestamps();
$table->primary('author_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('authors');
}
}
create_books_table
<?php
# database/migrations/1970_01_01_000002_create_books_table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->string('book_id');
$table->string('title');
$table->string('author_id');
$table->timestamps();
$table->foreign('author_id')->references('author_id')->on('authors');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
接下来,这是我的路线
routes/web.php
<?php
# routes/web.php
Route::get('/books', 'BookController@index');
我有两个模型
模型/Author.php
<?php
# app/Models/Author.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
protected $primaryKey = 'author_id';
public function book()
{
return $this->hasMany('App\Models\Book');
}
}
模型/Book.php
<?php
# app/Models/Book.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $primaryKey = 'book_id';
public function author()
{
return $this->belongsTo('App\Models\Author', 'author_id');
}
}
这是我的BookController
<?php
# app/Http/Controllers/BookController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Book;
class BookController extends Controller
{
public function index()
{
$books = Book::with('author')->get();
$results = [];
foreach ($books as $book) {
$result = new \stdClass();
$result->title = $book->title;
$result->author_first_name = $book->author->first_name;
$result->author_last_name = $book->author->last_name;
$results[] = $result;
}
dd($results);
}
}
这里有一些测试数据来填充数据库:
insert into authors (author_id, first_name, last_name, created_at)
values
(
'ceefb81f-48bb-ff07-1002-27fd8b7272b4', 'Charles', 'Coal', '2018-02-11 00:00:00'
);
INSERT INTO books (book_id, title, author_id, created_at)
VALUES
(
'a4e58f2a-36f5-f2fb-960c-c60e2689b337', 'Killing Coal', 'ceefb81f-48bb-ff07-1002-27fd8b7272b4',
'2018-02-11 00:00:00'
),
(
'a4e58f2a-36f5-f2fb-960c-c60e2689b338', 'Cleaning Coal', 'ceefb81f-48bb-ff07-1002-27fd8b7272b4',
'2018-02-11 00:00:01'
),
(
'a4e58f2a-36f5-f2fb-960c-c60e2689b339', 'Using Coal', 'ceefb81f-48bb-ff07-1002-27fd8b7272b4',
'2018-02-11 00:00:02'
);
我在 MySQL 中打开了日志记录,可以看到正在运行 2 个查询
# turn on logging by running the following in MySQL
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "/tmp/mysql.logfile.log";
SET GLOBAL general_log = 'ON';
这是 MySQL 日志文件的输出
2018-02-11T18:52:21.426064Z 262 Prepare select * from `books`
2018-02-11T18:52:21.426348Z 262 Execute select * from `books`
2018-02-11T18:52:21.426539Z 262 Close stmt
2018-02-11T18:52:21.430560Z 262 Prepare select * from `authors` where `authors`.`author_id` in (?)
2018-02-11T18:52:21.430689Z 262 Execute select * from `authors` where `authors`.`author_id` in ('ceefb81f-48bb-ff07-1002-27fd8b7272b4')
但是,由于某种原因,这些关系没有关联
解决方案 因为,我使用的是 GUID 而不是自动递增的整数,所以我不得不告诉 Eloquent 主键不是自动递增的。 Laravel 在幕后试图将我的 GUID 转换为整数。
<?php
# app/Models/Book.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $primaryKey = 'book_id';
# HAD TO ADD THIS
public $incrementing = false;
public function author()
{
return $this->belongsTo('App\Models\Author', 'author_id');
}
}
应用程序/模型/作者
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
protected $primaryKey = 'author_id';
# HAD TO ADD THIS
public $incrementing = false;
public function book()
{
return $this->hasMany('App\Models\Book');
}
}
最佳答案
对于Book模型你需要定义第三个参数book_id
public function author()
{
return $this->belongsTo('App\Models\Author', 'book_id', 'author_id');
}
If your parent model does not use id as its primary key, or you wish to join the child model to a different column, you may pass a third argument to the belongsTo method specifying your parent table's custom key
关于php - Laravel 5.5 急切加载抛出错误 - 试图获取非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48735289/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调