1
votes

I am developing a system that uses Maatwebsite to read and write data to the database from an excel sheet, which is working fine. Now before inserting the data, the system checks for the entries in parent table. And if there is any record that matches the record of the sheet, the system inserts foreign key to the child schema and if there's not, the system creates one first and then insert it's id as foreign key.
Here's the import 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
            ]);
        }
    }
}

It's saying:

Trying to get property 'author_name' of non-object

I know this error comes when you try to access an object's property from a non-object instance. get() is returning the collection object as usual and working fine outside the foreach() loop. what i can't figure out is why it's not working inside the loop. Any help would be appreciated!

1
A simple test would be to use something like var_dump($athr); before that line and see what it thinks the variable is.Nigel Ren
@NigelRen I have already done that it says: object(Illuminate\Database\Eloquent\Collection)#9051 (1) { ["items":protected]=> array(4).... I also check dd($athr) it's returning the collection object.Saud
I don't know why it's saying non-object when it's returning the collection object.Saud

1 Answers

0
votes

I still can't figure out why it's saying that and seems like no else also. So I think it's about time I post the solution I came up with. I found a way around it, So, basically what I did was I stored the whole collection to a global variable and accessed it in the loop.

Here's the code:

/**
 * 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 ;
    }

}

and in my importing controller's import method:

$quotes  = Excel::import(new QuotesImport(compact('author')));

Working fine till now. If there's some improving or anything that needs to be change, kindly feel free. I would appreciate it.