小编Ila*_*kel的帖子

Laravel Eloquent - 如何加入一个表

我正在为我的应用程序开发 API。

我正在尝试从数据库中提取项目并在 JSON 对象中返回它们,我的项目表如下所示:

Items
-id
-name
-description
-price
-currency_id
-company_id
Run Code Online (Sandbox Code Playgroud)

这就是我获取物品的方式:

$rows = Company::where('guid',$guid)
                ->first()
                ->items()
                ->orderBy('name', $sort_order);
Run Code Online (Sandbox Code Playgroud)

我想用包含货币表所有列的货币对象替换货币 ID

所以我的结果会是这样的:

[
  {
    'id':'1',
    'name':'name',
    'description': 'example',
    'price':'100',
    'currency':{
     'id':'1',
     'name':'usd',
     'symbol': '$'
     }
  }
]
Run Code Online (Sandbox Code Playgroud)

更新:这是我的货币表:

id
name
symbol
code
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5 laravel-eloquent

5
推荐指数
1
解决办法
6338
查看次数

Laravel雄辩的问题:方法不存在

我有2个表,“客户和公司”,每个公司有很多客户,每个客户有一个公司

这是我的模型:

class Client extends Model
{
    public function company(){
        return $this->hasOne('App\Company');
    }
}

class Company extends Model
{
    public function clients(){
        return $this->hasMany('App\Client');
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取公司所有客户的列表,这就是我试图做的:

$clients = Company::where('guid',$guid)->clients()->all();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

BadMethodCallException in Macroable.php line 74:
Method clients does not exist.
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

laravel eloquent laravel-5 laravel-eloquent

3
推荐指数
1
解决办法
9652
查看次数

Laravel Eloquent - (where) and (where or where or where)

我正在对我的数据进行搜索查询,我只想获取列中值为 0 的可用行,is_temp如果用户向我发送搜索查询,我想在其他列中搜索此值

这就是我现在所拥有的:

$clients = Company::where('guid',$guid)->first()
        ->clients()
        ->where('is_temp',0)
        ->orderBy('name', $sort_order)
        ->skip($per_page*($page-1))
        ->take($per_page);

if($search_query != ""){
     $clients = $clients->orWhere('name','LIKE','%'.$search_query.'%')
                        ->orWhere('email','LIKE','%'.$search_query.'%')
                        ->orWhere('vat_number','LIKE','%'.$search_query.'%')
                        ->orWhere('contact_name','LIKE','%'.$search_query.'%');
}

$response = [
    'clients' => $clients->get() 
];
return response()->json($response, 200);
Run Code Online (Sandbox Code Playgroud)

但是当我以这种方式使用它时,我也会得到字段is_temp等于 1的行,所以我需要使用 (where) 和 (where 或 where 或 where...)

我怎么做?

php laravel eloquent laravel-5 laravel-eloquent

1
推荐指数
1
解决办法
7052
查看次数

标签 统计

eloquent ×3

laravel ×3

laravel-5 ×3

laravel-eloquent ×3

php ×2