如何解决尝试获取非对象错误的属性“出价”

时间:2018-12-03 03:12:41

标签: javascript laravel datatables

我想以数据表的形式显示出价表中的数据。但是我得到这个错误  如果没有出价,则“尝试获取非对象的属性'出价'”。出价模型连接到拍卖模型,拍卖模型连接到媒体站点模型。如果没有数据,如何使其显示空白记录。

这是我的控制人:

<?php

namespace App\Http\Controllers;

use App\Auction;
use App\Bid;
use App\User;
use App\Media;
use App\MediaSite;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MediaSiteController extends Controller
{
public function show(MediaSite $mediaSite)
{
$auction = $mediaSite->auction;
$bids = $auction->bids;


return view('admin.media-site.show', ['mediaSite' => $mediaSite,'auction' => $auction], compact('auction'));
    }

我的观点:

<body>
<div id="datatable-bid"></div>
</body>

<script>
$(document).ready(function () {
var datatableBid = $('#datatable-bid').mDatatable({

// datasource definition
data: {
type: 'local',
source: {!! json_encode($auction->bids) !!},
pageSize: 10
},

// layout definition
layout: {
theme: 'default', // datatable theme
class: '', // custom wrapper class
scroll: false, 

footer: false // display/hide footer
},

// column sorting
sortable: true,

pagination: true,

search: {
input: $('#panel-search')
},

// columns definition
columns: [
{

field: "price",
title: "Price",
}, {

field: "month",
title: "Month",
},{

field: "user_id",
title: "User Id",
}
]
});

</script>

这是我的错误: Trying to get property 'bids' of non-object

2 个答案:

答案 0 :(得分:0)

跟随$auction = $mediaSite->auction;

if($auction){
$bids = $auction->bids;
}else{
//put following line or whatever you need to do if there is no data comes
$auction = [];
}

答案 1 :(得分:0)

show()函数中进行这些更改

$auction = $mediaSite->auction;
if($auction) {
  $bids = $auction->bids;
} else {
  $bids = [];
}
// now send $bids to view along with $auction
// may be like this
// return view(..., compact($auction, $bids));

然后,您认为需要进行更改

// datasource definition
data: {
type: 'local',
source: {!! json_encode($bids) !!},
pageSize: 10
},

看看是否有帮助。