试图在 foreach 循环中获取 属性 个非对象
Trying to get property of non-object in foreach loop
我正在开发一个系统,该系统使用 Maatwebsite 从 excel sheet 读取数据并将数据写入数据库,工作正常。现在,在插入数据之前,系统会检查父 table 中的条目。如果有任何记录与sheet的记录匹配,系统将外键插入子模式,如果没有,系统会先创建一个,然后将其id作为外键插入。
这是导入 class:
public function collection(Collection $rows){
$sub_chap = SubChapter::where(['chap_id' => $this->chap_id])->get();
$chapter = Chapter::where(['chap_id' => $this->chap_id])->first();
$author = Author::where(['author_status' => 1])->get();
$book = $chapter->book_id;
$author_id = 0;
$sub_chap_id = 0;
/* Working perfectly fine here...
foreach($author as $a){
echo $a->a_name."\r";
}
*/
foreach ($rows as $row){
if($row['quote_english'] != ""){
foreach($sub_chap as $sub){
if(trim($sub->sub_chap_english) == trim($row['sub_chap_english'])){
$sub_chap_id = $sub->sub_chap_id;
break;
} else{
$sub_chap_id = 0;
}
}
if($author->count() > 0){
foreach($author as $athr){
$author_id = (trim($athr->author_name) == trim($row['author_name']))? $athr->author_id : $author_id = 0;
}
}
if($author_id == 0){
$author = Author::create([
'author_name' => $row['author_name'],
...
...
'author_status' => 1,
]);
$author_id = $author->author_id;
}
$quote = Quote::create([
'quote_english' => $row['quote_english'],
'author_id' => $author_id,
'sub_chap_id' => $sub_chap_id,
'chap_id' => $this->chap_id,
'book_id' => $book
]);
}
}
}
说的是:
Trying to get property 'author_name' of non-object
我知道当您尝试从非对象实例访问对象的 属性 时会出现此错误。 get() 像往常一样返回集合对象,并且在 foreach() 循环之外工作正常。我想不通的是为什么它在循环内不起作用。如有任何帮助,我们将不胜感激!
我仍然不明白为什么会这样说,而且似乎也没有别的。所以我认为是时候 post 提出我想出的解决方案了。我找到了解决方法,所以,基本上我所做的就是将整个集合存储到一个全局变量中并在循环中访问它。
代码如下:
/**
* Global variable for raavi's data.
*/
public $author;
/**
* Construction function.
*
* @param int $id
* @param Collection $arr
*/
function __construct($arr) {
$this->author= $arr;
}
/**
* This method is responsible for inserting the excel
* sheet's rows data to the database schema.
*
* @param Collection $rows
*/
public function collection(Collection $rows){
// other code as it is...
foreach($this->author['author'] as $athr){
$author_id = (trim($athr->a_name) == trim($row['author_name']))? $athr->a_id : 0 ;
}
}
并且在我的导入控制器的导入方法中:
$quotes = Excel::import(new QuotesImport(compact('author')));
到目前为止工作正常。如果有一些改进或任何需要更改的地方,请随意。我将不胜感激。
我正在开发一个系统,该系统使用 Maatwebsite 从 excel sheet 读取数据并将数据写入数据库,工作正常。现在,在插入数据之前,系统会检查父 table 中的条目。如果有任何记录与sheet的记录匹配,系统将外键插入子模式,如果没有,系统会先创建一个,然后将其id作为外键插入。
这是导入 class:
public function collection(Collection $rows){
$sub_chap = SubChapter::where(['chap_id' => $this->chap_id])->get();
$chapter = Chapter::where(['chap_id' => $this->chap_id])->first();
$author = Author::where(['author_status' => 1])->get();
$book = $chapter->book_id;
$author_id = 0;
$sub_chap_id = 0;
/* Working perfectly fine here...
foreach($author as $a){
echo $a->a_name."\r";
}
*/
foreach ($rows as $row){
if($row['quote_english'] != ""){
foreach($sub_chap as $sub){
if(trim($sub->sub_chap_english) == trim($row['sub_chap_english'])){
$sub_chap_id = $sub->sub_chap_id;
break;
} else{
$sub_chap_id = 0;
}
}
if($author->count() > 0){
foreach($author as $athr){
$author_id = (trim($athr->author_name) == trim($row['author_name']))? $athr->author_id : $author_id = 0;
}
}
if($author_id == 0){
$author = Author::create([
'author_name' => $row['author_name'],
...
...
'author_status' => 1,
]);
$author_id = $author->author_id;
}
$quote = Quote::create([
'quote_english' => $row['quote_english'],
'author_id' => $author_id,
'sub_chap_id' => $sub_chap_id,
'chap_id' => $this->chap_id,
'book_id' => $book
]);
}
}
}
说的是:
Trying to get property 'author_name' of non-object
我知道当您尝试从非对象实例访问对象的 属性 时会出现此错误。 get() 像往常一样返回集合对象,并且在 foreach() 循环之外工作正常。我想不通的是为什么它在循环内不起作用。如有任何帮助,我们将不胜感激!
我仍然不明白为什么会这样说,而且似乎也没有别的。所以我认为是时候 post 提出我想出的解决方案了。我找到了解决方法,所以,基本上我所做的就是将整个集合存储到一个全局变量中并在循环中访问它。
代码如下:
/**
* Global variable for raavi's data.
*/
public $author;
/**
* Construction function.
*
* @param int $id
* @param Collection $arr
*/
function __construct($arr) {
$this->author= $arr;
}
/**
* This method is responsible for inserting the excel
* sheet's rows data to the database schema.
*
* @param Collection $rows
*/
public function collection(Collection $rows){
// other code as it is...
foreach($this->author['author'] as $athr){
$author_id = (trim($athr->a_name) == trim($row['author_name']))? $athr->a_id : 0 ;
}
}
并且在我的导入控制器的导入方法中:
$quotes = Excel::import(new QuotesImport(compact('author')));
到目前为止工作正常。如果有一些改进或任何需要更改的地方,请随意。我将不胜感激。