Fetch php incomplete class from wordpress wp-pro-quiz plugin
Hello All, i am using corcel plugin in laravel to access wordpress tables and data now i have a wp-pro-quiz plugin used for quiz platform in wordpress now i am trying to fetch quiz and questions using quiz id in laravel which is returning fine but answer_data is serialized in database which has data like below
a:4:{i:0;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:12:"(a) Only one";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:1;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:12:"(b) Only two";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:2;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:13:"(c) All three";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:1;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}i:3;O:27:"WpProQuiz_Model_AnswerTypes":7:{s:10:"*_answer";s:8:"(d) None";s:8:"*_html";b:0;s:10:"*_points";i:1;s:11:"*_correct";b:0;s:14:"*_sortString";s:0:"";s:18:"*_sortStringHtml";b:0;s:10:"*_mapper";N;}}
now i am trying to fetch this data in laravel but getting below error, i cant autoload wordpress file becuase its in different server is there any other way to fetch answer_data column ?
i have code like below
WpProQuiz.php model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class WpProQuiz extends Model
{
use HasFactory;
protected $connection = 'corcelword';
protected $primaryKey = 'id';
protected $table = "wp_pro_quiz_master";
public function questions()
{
return $this->hasMany('App\Models\WpProQuizQuestion', 'quiz_id');
}
}
WpProQuizQuestion.php model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class WpProQuizQuestion extends Model
{
use HasFactory;
protected $connection = 'corcelword';
protected $primaryKey = 'id';
protected $table = "wp_pro_quiz_question";
}
now in cotnroller
public function getQuizData($quizid)
{
$quizData = WpProQuiz::with('questions')->where('id', $quizid)->first();
$quizModels = [];
if ($quizData) {
$allquestions = $quizData->questions;
foreach ($allquestions as $question) {
$serializedAnswerData = rtrim($question->answer_data);
$unserializedAnswerData = unserialize($serializedAnswerData);
$answers = [];
// Iterate through incomplete objects and treat them as arrays
foreach ($unserializedAnswerData as $answerObject) {
// Convert the __PHP_Incomplete_Class objects to arrays
$answerArray = [
'_answer' => property_exists($answerObject, '_answer') ? $answerObject->_answer : null,
'_html' => property_exists($answerObject, '_html') ? $answerObject->_html : null,
'_points' => property_exists($answerObject, '_points') ? $answerObject->_points : null,
'_correct' => property_exists($answerObject, '_correct') ? $answerObject->_correct : null,
'_sortString' => property_exists($answerObject, '_sortString') ? $answerObject->_sortString : null,
'_sortStringHtml' => property_exists($answerObject, '_sortStringHtml') ? $answerObject->_sortStringHtml : null,
'_mapper' => property_exists($answerObject, '_mapper') ? $answerObject->_mapper : null,
];
$answers[] = $answerArray;
}
// Now $answers is an array of associative arrays
$quizModels[] = $answers;
}
}
return $quizModels;
}
if i dd($unserializedAnswerData ) i am getting below response,
array:4 [
0 => __PHP_Incomplete_Class(WpProQuiz_Model_AnswerTypes) {#461
#_answer: "(a)\tOnly one"
#_html: false
#_points: 1
#_correct: false
#_sortString: ""
#_sortStringHtml: false
#_mapper: null
}
1 => __PHP_Incomplete_Class(WpProQuiz_Model_AnswerTypes) {#462
#_answer: "(b)\tOnly two"
#_html: false
#_points: 1
#_correct: false
#_sortString: ""
#_sortStringHtml: false
#_mapper: null
}
2 => __PHP_Incomplete_Class(WpProQuiz_Model_AnswerTypes) {#483
#_answer: "(c)\tAll three"
#_html: false
#_points: 1
#_correct: true
#_sortString: ""
#_sortStringHtml: false
#_mapper: null
}
3 => __PHP_Incomplete_Class(WpProQuiz_Model_AnswerTypes) {#476
#_answer: "(d)\tNone"
#_html: false
#_points: 1
#_correct: false
#_sortString: ""
#_sortStringHtml: false
#_mapper: null
}
]
but i am getting below errro while fetching
he script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "WpProQuiz_Model_AnswerTypes" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition {"exception":"[object] (ErrorException(code: 0): property_exists(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "WpProQuiz_Model_AnswerTypes" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide an autoloader to load the class definition at //app/Http/Controllers/API/MiscellaneousController.php:307
Please help me with this.
Thank you.
Please or to participate in this conversation.