【问题标题】:Laravel 5.4: Storing the 'Where' clause in a variableLaravel 5.4:将“Where”子句存储在变量中
【发布时间】:2017-06-04 03:02:08
【问题描述】:

我想在 Laravel 中编写一个动态更新查询,它接受参数并且可以在整个项目中使用。

以下是我的控制器功能:

public function editquery(Request $request)
    {

    $city_id   = $request->input('city_id');    
    $city_name = $request->input('city_name');   

    $tbl  = 'city';    
    $data = ['city_name'=>$city_name];
    $wher = ('city_id',1);

    General_model::editrecord($data,$wher,$tbl);

    return redirect()->action('Admin_controller@cities_page')->with('status','Record Updated Successfully!');;

    }

下面是我的模型函数:

public static function editrecord($data,$wher,$tbl)
    {
      return DB::table($tbl)->where($wher)->update($data);
    }

这里唯一的问题是我无法将值 ('city_id',1) 存储在 $wher 变量中。这是错误的屏幕截图: link to the image file

有没有其他方法可以做到这一点。请帮忙。

【问题讨论】:

    标签: php laravel laravel-5 where-in


    【解决方案1】:

    where 方法接受一组条件。

    $table  = 'city';
    $conditions = [
        ['city_id', '=', '1']
    ];
    $data = ['city_name' => $city_name];
    
    General_model::editRecord($table, $conditions, $data);
    
    // In your model
    
    public static function editRecord($table, $conditions, $data)
    {
        return DB::table($table)->where($conditions)->update($data);
    }
    

    您还可以设置多个条件。

    $conditions = [
        ['city_id', '=', '1'],
        ['test', '=', 'test'],
    ];
    

    编辑

    这是默认的where 方法

    where($column, $operator = null, $value = null, $boolean = 'and')
    

    将第四个参数设置为or 将使条件为orWhere

    例子

    $conditions = [
        ['city_id', '=', '1'],
        ['test', '=', 'test', 'or'],
    ];
    

    【讨论】:

    • 谢谢。有用。你能告诉我更多关于条件的信息吗?如果我如何在 $conditions 数组中使用 AND & OR 子句?
    • 感谢您的回答。 :)
    【解决方案2】:

    你不能这样做

    public static function editrecord($data,$wher,$tbl)
    {
      return DB::table($tbl)->where($wher)->update($data);
    }
    

    因为,where 是一个函数;它需要 2 或 3 个参数,而不仅仅是 1 个参数。

    你必须像这样传递两个参数

    public static function editrecord($data, $where_column, $where_val, $tbl)
    {
      return DB::table($tbl)->where($where_column, $where_val)
                            ->update($data);
    }
    

    然后,在你的控制器函数中

    $where_column = 'city_id';
    $where_val = 1;
    
    General_model::editrecord($data,$where_column,$where_val,$tbl);
    

    【讨论】:

    • 我应该更改控制器功能中的任何内容吗?
    • 如果我需要在 where 子句中使用 AND OR 语句怎么办?例如,其中 city_id = 1 AND city_name = london AND city_country = uk。
    • 您需要为此添加单独的 where 子句。另外,请针对此问题提出另一个问题。我已经回答了你原来的问题。如果它解决了您的问题,请点赞并接受答案。
    • @linuxartisan where 也接受闭包和数组。
    • @Sandeesh 可能是 OP 从一开始就应该清楚这一点。由于OP在原始问题中没有提到这些事情,因此我相应地给出了答案。
    【解决方案3】:

    您的代码并不完全符合 Laravel 的风格,如果 Eloquent / Query Builder 的标准功能可以轻松解决此类任务,为什么还要创建一个单独的静态函数?

    雄辩的例子:

    app/City.php

    <?php
    class City extends Model {
        protected $table = 'city';
        protected $primaryKey = 'city_id';
        protected $fillable = ['city_name'];
    }
    

    在你的控制器中:

    City::findOrFail($city_id)->update([
        'city_name' => $city_name
    ]);
    

    查询生成器示例:

    DB::table('city')->where(['city_id' => $city_id])->update([
        'city_name' => $city_name
    ]);
    

    这比以难以理解的方式做类似事情的函数更容易阅读、理解和支持。

    【讨论】:

    • 我是 Laravel 的新手。假设我的数据库中有超过 100 个表,那么我需要为每个表制作 100 个不同的 Model 文件吗?您提供的示例适用于一个表,但我想编写一个可用于所有表的函数。我曾经在 Codeigniter 中这样做过。
    • 一般来说,如果项目需要这么多的表,创建100个类也没有问题。这不会影响加载,因为类仅在您需要使用它们时才加载。这样,您将获得 Eloquent 提供的大量优势:关系、突变体、观察者。代码将保持干净且易于理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多