简体   繁体   English

在 Laravel 中以相同的方法将变量从一个查询传递到另一个查询

[英]Passing a variable from one query to another in same method in Laravel

I am trying to pass a variable from one query to another (bellow is the code) If I use $data[0]->name this will give a fixed values which I need something variable like in $data[$i]->name or something.我正在尝试将变量从一个查询传递到另一个查询(下面是代码)如果我使用 $data[0]->name 这将给出一个固定值,我需要像 $data[$i]-> 这样的变量名字什么的。

Controller: Controller:

  public function accounts($country) {

    
    $data= DB::table("invoices")
        ->where("country", $country)
        ->select([ "name",
                    DB::raw("sum(case when type='income' then amount else 0 end) as income"),
                    DB::raw("sum(case when type='outcome' then amount else 0 end) as outcome")
                ])
        ->groupBy("name")
        ->orderBy("name", "DESC")
        ->get();


    $payments= DB::table("payments")
        ->where("name", $data[0]->name) // here is the variable I want to pass from the above $data query
        ->where("country", $country)
        ->select([  
                    DB::raw("sum(case when type='income' then amount else 0 end) as payment_income"),
                    DB::raw("sum(case when type='outcome' then amount else 0 end) as payment_outcome")
                ])
        ->groupBy("name")
        ->orderBy("name", "DESC")
        ->get();

    return view('accounting.accounts')
    ->with('accounts',$data)
    ->with('payments',$payments);

   }

View:看法:

 @foreach($accounts as $account)
   @foreach($payments as $payment)
     <tr>
        <th>{{$account->name}}</th>
        <td>{{$account->income}}</td>
        <td>{{$payment->payment_income}}</td>
        <td></td>
        <td>{{$account->outcome}}</td>
        <td>{{$payment->payment_outcome}}</td>
        <td></td>
     <tr>
  @endforeach
@endforeach

Thank you in advance先感谢您

Here is what you can do这是你可以做的

$names = $data->pluck('name');

$payments= DB::table("payments")
        ->where("country", $country)
        ->whereIn("name", $names) 
        ->select([  
                    DB::raw("sum(case when type='income' then amount else 0 end) as payment_income"),
                    DB::raw("sum(case when type='outcome' then amount else 0 end) as payment_outcome")
                ])
        ->groupBy("name")
        ->orderBy("name", "DESC")
        ->get();
return view('accounting.accounts')
->with('accounts',$data)
->with('payments',$payments);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将变量从一个函数传递到同一控制器中的另一个函数(laravel 5) - Passing variable from one function to another function in same controller (laravel 5) 将变量从一种控制器方法传递到另一种方法 - Passing a variable from one controller method to another 将变量从一个视图传递到另一个视图-Laravel 5.1 - Passing Variable From One View to Another View - Laravel 5.1 将变量值从一个控制器传递到另一个控制器 laravel - Passing variable value from one controller to another controller laravel 从一个资源的角度传递参数以使用Laravel创建另一个资源的方法 - Passing argument from view of one resource to create method of another with Laravel 将变量从一个函数传递到同一控制器中的另一个函数 - Passing variable from one function to another function in same controller 需要将具有相同值的变量从一个 function 传递到另一个 laravel - Need to pass the variable with same value from one function to another in laravel 如何在 Laravel 中将变量从一种方法传递到另一种方法 - How to pass variable from one method to another in Laravel 将变量从一种方法传递到同一控制器中的另一种方法 - pass variable from one method to another within the same controller 如何将变量从一个方法传递到同一类中的另一个方法? - How to pass a variable from one method to another in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM