【问题标题】:Query data laravel查询数据 laravel
【发布时间】:2020-04-01 15:00:25
【问题描述】:

我想知道如何让它在 laravel 中工作:

我有两个表:“orders”和“delivery_boys”。

创建订单后,我想在“delivery_boys”表中获取与“order”表中的“store_id”相对应的“id”。

在mysql中看起来像这样 SELECT `id` FROM `delivery_boys` WHERE store_id = '3'

感谢您的帮助。

我使用这个功能:

 protected $table = 'orders';

   public function addNew($data)
   {
      $user                = AppUser::find($data['user_id']);


      if(isset($data['address']) && $data['address'] > 0)
      {
          $address = Address::find($data['address']);
      }
      else
      {
          $address = new Address;

      }

      $add                 = new Order;
      $add->user_id        = $data['user_id'];
      $add->store_id       = $this->getStore($data['cart_no']);
      $add->d_boy          = $add->?????;  <---- here i want to add from "delivery_boys" table the "id" that correspond to the "store_id" added just above from the "order" table ---->

      $add->name           = $user->name;
      $add->email          = $user->email;
      $add->phone          = $user->phone;
      $add->address        = $address->address;
      $add->lat            = $address->lat;
      $add->lng            = $address->lng;
      $add->address        = $address->address;
      $add->d_charges      = $this->getTotal($data['cart_no'])['d_charges'];
      $add->discount       = $this->getTotal($data['cart_no'])['discount'];
      $add->total          = $this->getTotal($data['cart_no'])['total'];
      $add->payment_method = $data['payment'];
      $add->payment_id     = isset($data['payment_id']) ? $data['payment_id'] : 0;
      $add->type           = isset($data['otype']) ? $data['otype'] : 1;
      $add->notes          = isset($data['notes']) ? $data['notes'] : null;
      $add->save();

【问题讨论】:

  • 你需要创建一个关系函数来使用 $this->delivery_boy->store_id 一个函数比如: public function delivery_boy(){ return $this->hasOne('App\DeliveryBoyModel','id ','id_delivery_boy'); }
  • SELECT id FROM delivery_boys WHERE store_id = '3' 你想在 laravel 中使用这个吗?

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

在 $add = new Order 之前运行它;

$d_boy = DB::table('delivery_boys')
->select('id')
->where('store_id', $this->getStore($data['cart_no']))
->first();

然后在需要的地方添加:

$add->d_boy = $d_boy->id;

【讨论】:

  • 非常感谢您的帮助
【解决方案2】:
$id= DB::table('delivery_boys')->select('id')->where('store_id', '=', $this->getStore($data['cart_no']))->get();

【讨论】:

    猜你喜欢
    • 2014-06-04
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2021-11-24
    • 2017-08-30
    • 2019-05-12
    相关资源
    最近更新 更多