简体   繁体   English

未定义的变量问题Laravel 5.4

[英]Undefined variable issue Laravel 5.4

I've got stuck with this error so if ever pls forgive me because I'm still new at laravel. 我一直陷在这个错误中,所以请原谅我,因为我在Laravel还很新。 I got this error 我得到这个错误

Undefined variable: clientTransactions (View: C:\\xampp\\htdocs\\dcgwapo\\resources\\views\\service_details\\create.blade.php) 未定义的变量:clientTransactions(视图:C:\\ xampp \\ htdocs \\ dcgwapo \\ resources \\ views \\ service_details \\ create.blade.php)

but I have a right code but I still wondering why it is still undefined variable given I define it in my controller. 但是我有一个正确的代码,但是我仍然想知道为什么它仍然是未定义的变量,因为我在控制器中定义了它。

create.blade.php in service details code 服务详细信息代码中的create.blade.php

<div class="form-group">
    <label for="client_transaction_id">Client Trans ID: </label>
    <select class="form-control" name="client_transaction_id">
        @foreach ($clientTransactions as $clientTransaction)
            <option value= "{{ $clientTransaction->id }}">
              {{ $clientTransaction->id }}
            </option>
        @endforeach
    </select>
  </div>

ServiceDetailsController code ServiceDetailsController代码

public function create()
{
    $users = User::pluck('fname', 'lname', 'id');
    $services = Service::pluck('name', 'id');
    $clientTransactions = ClientTransaction::all();
    return view('service_details.create', ['users' => User::all()], ['services' => Service::all()], ['clientTransactions' => ClientTransaction::all()]);
}

ServiceDetail.php model code ServiceDetail.php模型代码

public function clientTransaction()
{
  return $this->belongsTo(ClientTransaction::class);
}

I hope you can help me. 我希望你能帮助我。 Thanks! 谢谢!

You're sending variables to your view the wrong way. 您以错误的方式将变量发送到视图。 The seconds argument should be an array with all your variables. seconds参数应该是包含所有变量的数组。 As of now your adding a new parameter to the view function for each variable. 到目前为止,您已经为每个变量的view函数添加了一个新参数。

view('view', [...], [...], [...])

It should be like this: 应该是这样的:

view('view', [...1, ...2, ...3])

So what you need to change is the return statement to this: 因此,您需要更改的是return语句:

return view('service_details.create', ['users' => User::all(), 'services' => Service::all(), 'clientTransactions' => ClientTransaction::all()]);
You can use compact to pass data from controller to view:



        public function create()
        {
            $users = User::pluck('fname', 'lname', 'id');
            $services = Service::pluck('name', 'id');
            $clientTransactions = ClientTransaction::all();
            return view('service_details.create',compact('users','services','clientTransactions');
        } 

Second parameter to view function accepts an associative array of data, you are passing an indexedArray of arrays, Just use this return statement and you are good to go. view函数的第二个参数接受关联的数据数组,您正在传递数组的indexedArray,只需使用此return语句,您就可以进行了。 ;) ;)

    return view('service_details.create', [
        'users' => User::all(),
        'services' => Service::all(),
        'clientTransactions' => ClientTransaction::all()
    ]); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM