gpt4 book ai didi

angularjs - laravel 请求未定义

转载 作者:行者123 更新时间:2023-12-05 04:11:21 28 4
gpt4 key购买 nike

我是 Laravel 的新手,所以一切都在探索期。我使用 angular http post 将数据发送到 laravel 并且在 laravel Controller 中我能够

dd($request)

Request {#40
#json: ParameterBag {#32
#parameters: array:4 [
"GPTour_id" => 1
"customer_id" => 1
"status" => "Confirmed"
"note" => "asdfasdf"
]
}
#userResolver: Closure {#300
class: "Illuminate\Auth\AuthServiceProvider"
this: AuthServiceProvider {#22 …}
use: array:1 [
"$app" => Application {#3
#basePath: "/Users/haophung/Dropbox/server/websites/nglaravelyep/laravel-backend"
#hasBeenBootstrapped: true
#booted: true
#bootingCallbacks: []

但是,如果我使用

$request->input('key')

我得到 $request 未定义。请指教!!!

public function addGospelCustomer(Request $request)
{
if ($request) {
$customer_id = $request->get('customer_id');
$tour_id = $request->get('GPTour_id');
$validator = Validator::make($request->all(), [
'customer_id' =>'required'
]);

if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 406);
}
$gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) {
$query->where('id', $customer_id);
}])->first();

if ($gospel_customer === 'null') {
return response()->json(['error' => "The Customer is already on the list"], 406);
}

return 'success';//response()->json(['success' => $request], 200);
}else {
return response()->json(['error' =>'can not add customer'], 401);
}
}

GospelController.php 第 60 行中的 ErrorException: undefined variable :customer_id

我觉得问题是

    $gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) {
$query->where('id', $customer_id);
}])->first();

我可以 echo $customer_id 出来,但是在这个 eloquent 没有定义

最佳答案

您需要在函数定义中输入提示请求

public function name(Request $request) {}

像这样使用它

$key = $request->key;
$key = $request->get('key');

或者使用全局函数

$key = request('key');

更新

哪里有错误异常做

$gospel_customer = Gospel_tour::find($tour_id)->with(['customers' => function($query) use ($customer_id) {
$query->where('id', $customer_id);
}]);

发生错误是因为您在闭包内,并且它无权访问外部变量。

关于angularjs - laravel 请求未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42823107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com