此集合实例上不存在 laravel 属性 [地址]

Prz*_*tas 1 laravel eloquent blade laravel-5 laravel-blade

我有一个业务表,它使用 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
      ]
Run Code Online (Sandbox Code Playgroud)

然而问题是说:

此集合实例上不存在属性 [addresses]。(查看: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>
Run Code Online (Sandbox Code Playgroud)

模型:

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');
    }
Run Code Online (Sandbox Code Playgroud)

另一个问题是有时一个字段是空的,所以让我们说有时 $address->city = null 我如何告诉忽略该字段并继续?

我可以做 @if($address->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'));
        /*}
Run Code Online (Sandbox Code Playgroud)

//编辑2

现在说

试图获取非对象的属性(视图: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}}}
                         });
Run Code Online (Sandbox Code Playgroud)

小智 5

看起来你需要改变

$business = Business::where('id', $id)
                    ->with('addresses')
                    ->with('location')
                    ->with('gallery')
                    ->get();
Run Code Online (Sandbox Code Playgroud)

$business = Business::where('id', $id)
                    ->with('addresses')
                    ->with('location')
                    ->with('gallery')
                    ->first();
Run Code Online (Sandbox Code Playgroud)

您的问题是您的查询返回的是 的一个Collection而不是单个实例Business,这是因为您的查询正在搜索多个企业,并且会生成Collection它找到的任何企业的一个,即使只有一个。