我有一个业务表,该业务表使用与表地址,位置和画廊的雄辩链接。输出看起来像这样:
Collection {#266 ▼
#items: array:1 [▼
0 => Business {#260 ▼
#table: "businesses"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [▶]
#original: array:14 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:3 [▼
"addresses" => Collection {#261 ▼
#items: array:1 [▼
0 => Address {#263 ▼
#table: "addresses"
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [▼
"id" => 10
"firstline_address" => "96"
"secondline_address" => "Cambridge Street"
"town" => "p.wojtas26@gmail.com"
"city" => "p.wojtas26@gmail.com"
"postcode" => "SK15 1AU"
"telephone" => "7815522481"
"created_at" => "2017-06-26 08:05:32"
"updated_at" => "2017-06-26 08:05:32"
]
#original: array:11 [▶]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [▼
"pivot" => Pivot {#262 ▶}
]
#touches: []
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
"location" => Location {#279 ▶}
"gallery" => null
]
但是,问题是:
属性[地址]在此集合实例上不存在。(查看:c: xampp htdocs laravel resources view displaybusiness.blade.php(
这是我的观点:
@foreach($business->addresses as $address)
<p>{{$address->firstline_address}}</p>
<p>{{$address->secondline_address}}</p>
<p>{{$address->town}}</p>
<p>{{$address->city}}</p>
<p>{{$address->postcode}}</p>
<p>{{$address->telephone}}</p>
@endforeach
</div>
模型:
class Business extends Model
{
protected $table = 'businesses';
public $timestamps = true;
use Searchable;
public function addresses()
{
return $this->belongsToMany('AppAddress', 'business_address', 'business_id', 'address_id');
}
public function gallery()
{
return $this->hasOne('AppGallery', 'business_id');
}
public function film()
{
return $this->hasOne('AppFilm', 'business_id');
}
public function location()
{
return $this->hasOne('AppLocation', 'business_id');
}
public function user()
{
return $this->hasMany('AppUser', 'user_business', 'business_id', 'user_id');
}
另一个问题是有时候一个字段是空的,所以假设有时候$ address-> city = null我该如何忽略该领域并继续进行?
我可以做@if($ addressif-> city == null(,但我不想为每个人做,因为我无法预测会空的。
//编辑
控制器:
public function displayBusiness($id) {
$business = Business::where('id', $id)
->with('addresses')
->with('location')
->with('gallery')
->get();
//dd($business);
/*
$instagram = $business->instagram_business;
$twitter = $business->twitter_business;
$instagramItems=[];
$twitterItems=[];
if(!empty ($instagram)){
$client = new GuzzleHttpClient();
$url = sprintf('https://www.instagram.com/'.$instagram.'/media');
$response = $client->get($url);
$instagramItems = json_decode((string) $response->getBody(), true)['items'];
$twitterItems = Twitter::getUserTimeline(['screen_name' => $twitter, 'count' => 10, 'format' => 'array']);
*/
//$session = session()->put('key', $id);
//$gallery = Gallery::where('business_id', $id)->get();
//$location = Location::where('business_id', $id)->get();
//$review = Review::where('business_id', $id)->get();
return view('business.displayBusiness', compact('business', 'address', 'gallery', 'location', 'review', 'instagramItems', 'twitterItems'));
/*}
//edit2
现在说
尝试获取非对象的属性(查看:c: xampp htdocs laravel resources resources view business business displaybusiness.blade.blade.php(
是:
@foreach ($business->location as $marker)
<script>
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 19,
center: {lat: {{$marker->latitude}}, lng: {{$marker->longitude}}}
});
看起来您需要更改
$business = Business::where('id', $id)
->with('addresses')
->with('location')
->with('gallery')
->get();
to
$business = Business::where('id', $id)
->with('addresses')
->with('location')
->with('gallery')
->first();
您的问题是您的查询是返回Collection
而不是Business
的一个实例,这是因为您的查询正在搜索多个业务,并且将生成它所发现的任何业务的Collection
,即使只有一个。
最好使用((
$business = Business::where('id', $id)
->has('addresses')
->has('location')
->has('gallery')
->get();
查询关系存在https://laravel.com/docs/5.7/eloquent-relationships#querying-reasations