I have a business table which is linked using eloquent with table addresses, location and gallery. Output looks like this:
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
]
However the problem is that is says:
Property [addresses] does not exist on this collection instance. (View: C:\xampp\htdocs\laravel\resources\views\displayBusiness.blade.php)
here's my view:
@foreach($business->addresses as $address)
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
@endforeach
</div>
Model:
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');
}
Another problem is that sometimes a field is empty so let's say sometimes $address->city = null how would I tell to ignore that field and carry on?
I can do @if($address->city == NULL) but I don't want to do it for each one as I cannot predict what will be empty.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire