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

问题描述 投票:1回答:2

我有一个用口才与表的地址,位置和画廊链接的业务表。输出看起来是这样的:

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" => "[email protected]"
                "city" => "[email protected]"
                "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 \资源\意见\ 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');
    }

另一个问题是,有时一个字段是空的,所以我们说有时候地址 - $>城市= NULL我怎么会告诉忽略,即场和进行?

我可以做@if($地址 - >城市== 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'));
        /*}

// Aaditi

现在说

试图让非对象属性(查看:C:\ XAMPP \ htdocs中\ laravel \资源\意见\企业\ 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}}}
                         });
laravel laravel-5 eloquent blade laravel-blade
2个回答
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,即使只有一个。


0
投票

最好使用具有()

$business = Business::where('id', $id) ->has('addresses') ->has('location') ->has('gallery') ->get();

查询关系存在https://laravel.com/docs/5.7/eloquent-relationships#querying-relations

© www.soinside.com 2019 - 2024. All rights reserved.