laravel查找匹配的id并为变量分配特定的字段值?

时间:2017-06-20 10:24:07

标签: laravel laravel-5 eloquent

所以我正在寻找一个使用雄辩的匹配业务。我得到一个匹配的结果,现在我想使用特定字段,将其分配给变量,以便我可以检索用户twitter。

这是我的代码:

    $business = Business::with('addresses')
                    ->find($id);
    $variable = $business->business_twitter;
    $test = Twitter::getUserTimeline(['screen_name' => $variable, 'count' => 20, 'format' => 'json']);
    dd($variable);

然而,dd($ variable)显然是空的。如何解决这个问题?

//修改

DD($业务);

Business {#232 ▼
  #table: "businesses"
  +timestamps: true
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:14 [▼
    "id" => 97
    "name" => "telephone"
    "type" => "4"
    "email" => "p.wojtas26@gmail.com"
    "logo" => null
    "twitter_business" => "live_oldham"
    "facebook_business" => "liveoldham"
    "instagram_business" => "live_oldham"
    "image" => null
    "gallery_id" => null
    "user_id" => 2
    "api_key" => null
    "created_at" => "2017-06-20 10:10:38"
    "updated_at" => "2017-06-20 10:10:38"

1 个答案:

答案 0 :(得分:1)

获取密钥时出错了。您应该从twitter_business集合中提取$business密钥。

$variable = $business->twitter_business;