简体   繁体   中英

How to return value as json in Laravel?

In my controller, Im returning a row like;

 $alarm= \DB::table('vwAlarmActionSummaryYFUserSite')
            ->where([['vwAlarmActionSummaryYFUserSite.yfUserID', '=', $yfUserID], ['vwAlarmActionSummaryYFUserSite.AlarmId', '=', $id]])->first();

When i do;

 var_dump( $alarm);

It gives;

object(stdClass)#249 (59) { ["AlarmId"]=> string(6) "246733" ["AlertTypeId"]=> string(2) "23" ["AlertTypeName"]=> string(20) "Missing Data - Sales" ["AlertTypeDescription"]=> string(20) "Brownout Sales Alarm" ["AlertSourceSystemId"]=> string(1) "1" ["AlertSourceSystemName"]=> string(7) "HD SIRA" ["AlertId"]=> string(7) "9957057" [
...........

I pass that to my View page like;

return view('alarms.ack', ['alarm' => $alarm, 'yfSessionId' => $yfSessionId]);

In the View I access properties like;

$alarm->SiteCode 
$alarm->AlarmId

Now, I get this exception;

 ErrorException
Cannot use object of type stdClass as array (View: /app/resources/views/alarms/ack.blade.php)
at CompilerEngine->handleViewException(object(FatalThrowableError), 0)
in PhpEngine.php (line 46)
at PhpEngine->evaluatePath('/app/storage/framework/views/c593810096345df8c818851d1e61ae4bc523a3d4.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'alarm' => object(stdClass), 'yfSessionId' => 'abc'))
in CompilerEngine.php (line 59)

How can I overcome this?

//Controller

 $data = return response()->json($alarm);
return view('alarms.ack', compact('data'));

//View

{!! $data->SiteCode !!}
 $value = return response()->json( array('result'=>array('data'=>$alarm)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM