我有一个业务表,它使用eloquent与表地址,位置和图库进行链接。 输出如下:
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 \ views \ 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('App\Address', 'business_address', 'business_id', 'address_id');
}
public function gallery()
{
return $this->hasOne('App\Gallery', 'business_id');
}
public function film()
{
return $this->hasOne('App\Film', 'business_id');
}
public function location()
{
return $this->hasOne('App\Location', 'business_id');
}
public function user()
{
return $this->hasMany('App\User', 'user_business', 'business_id', 'user_id');
}
另一个问题是,有时一个字段是空的,所以有时候说$ address-&gt; city = null如何告诉我忽略该字段并继续?
我可以做@if($ address-&gt; 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 \GuzzleHttp\Client();
$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 \ views \ business \ displayBusiness.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}}}
});
答案 0 :(得分:4)
看起来你需要改变
$business = Business::where('id', $id)
->with('addresses')
->with('location')
->with('gallery')
->get();
到
$business = Business::where('id', $id)
->with('addresses')
->with('location')
->with('gallery')
->first();
您的问题是,您的查询返回的是Collection
而不是Business
的单个实例,这是因为您的查询正在搜索多个商家,并且会生成Collection
个它发现的企业,即使只有一个。
答案 1 :(得分:0)
更好地使用has()
$business = Business::where('id', $id)
->has('addresses')
->has('location')
->has('gallery')
->get();
查询关系存在 https://laravel.com/docs/5.7/eloquent-relationships#querying-relations