I dont know if this is the whole problem, but using with will always result in call_actions existing, but being empty
if (!$call->call_actions) {
try
if (empty($call->call_actions)) {
Hi all,
Thanks in advance for any help & advice.
I'm building an API and one endpoint is to return information about a "call" (job) to a user with the relationships "appointments" and "call_actions".
If "call_actions" doesn't exist, I want to attempt to find the previous "call" and get it's call_actions relationship so the consumer can use it to pre-fill a form.
Unfortunately whenever I try I get "InvalidArgumentException".
Could anyone please advise/point me in the right direction?
Thank you!
Model (SCCall.php)
<?php
namespace App\Models\Tesseract;
use App\Enums\CallStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SCCall extends Model
{
use HasFactory;
protected $connection = 'sqlsrv';
protected $table = 'SCCall';
protected $primaryKey = 'Call_Num';
public $timestamps = false;
public function appointment()
{
return $this->hasOneThrough(
SCDiaryEntry::class,
SCDiaryEntryLinkToCall::class,
'Call_Num', // Foreign key on SCDiaryEntryLinkToCall
'DiaryEntry_ID', // Foreign key on SCDiaryEntry
'Call_Num', // Local key on SCCall
'DiaryEntry_ID' // Local key on SCDiaryEntryLinkToCall
)
->orderBy('Appointment_Date', 'desc');
}
public function latestPreviousPPMCall()
{
return $this->belongsTo(SCCall::class)
->where('Call_CalT_Code', 'PPM')
->where('Call_Status', 'COMP')
->where('Call_Prod_Num', $this->Call_Prod_Num)
->where('Call_Site_Num', $this->Call_Site_Num)
->where('Call_Prob_Code', $this->Call_Prob_Code)
->where('Call_Num', '<', $this->Call_Num)
->orderBy('Call_Num', 'desc');
}
Controller
<?php
namespace App\Http\Controllers\API;
use App\Models\Tesseract\SCCall;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class CallsController extends Controller
{
public function show($call_num)
{
$call = SCCall::with([
'appointment',
'call_actions'
])
->where('Call_Num', $call_num)
->first();
if (!$call) {
return new JsonResponse(['message' => 'Call not found'], 404);
}
// If no call_actions exist, try to get them from the previous completed call
if (!$call->call_actions) {
$call->load('latestPreviousPPMCall.call_actions');
if ($call->latestPreviousPPMCall && $call->latestPreviousPPMCall->call_actions) {
$call->call_actions = $call->latestPreviousPPMCall->call_actions;
}
}
// Set default values if still no call_actions found
if (!$call->call_actions) {
$call->call_actions = new \stdClass();
$call->call_actions->Email_To_Customer = false;
$call->call_actions->Remedial_Required = false;
$call->call_actions->Assets_On_Site = false;
$call->call_actions->FSR = '';
$call->call_actions->Remedial_Works = '';
$call->call_actions->System_Info = [];
}
return new JsonResponse($call, 200);
}
Please or to participate in this conversation.