php laravel 5.2:获取查询字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38737019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:41:35  来源:igfitidea点击:

laravel 5.2 : Get query string

phplaravel

提问by Gammer

I have the following url http://project.su/?invitee=95

我有以下网址 http://project.su/?invitee=95

first i want to check the inviteein url, if the url have inviteethen get the value.

首先,我想检查输入invitee的 url,如果 url 有invitee然后获取值。

What i have tried (controller) :

我尝试过的(控制器):

if(!empty($request->get('invitee'))){
   $user->invitee = $request->get('invitee');
}

The following code is not working .

以下代码不起作用。

I want storing the inviteeresult(id) in database.

我想将invitee结果(id)存储在数据库中。

Thanks.

谢谢。

回答by Depzor

To determining if an input value is present:

要确定输入值是否存在:

if ($request->has('invitee')) {
       $user->invitee = $request->input('invitee');
}

The hasmethod returns true if the value is present and is not an empty string:

具有方法返回true,如果该值存在,并且是不是空字符串:

回答by Rohan Khude

In laravel 5.7, we can also use request()->invitee. By using this, we can get both, URL path parameters and query parameters. Say we have below URL

在 laravel 5.7 中,我们也可以使用request()->invitee. 通过使用它,我们可以获得 URL 路径参数和查询参数。假设我们有以下网址

http://localhost:8000/users/{id}?invitee=95

To read idand invitee, we can use

要阅读idinvitee,我们可以使用

$id  ?
$invitee ? - Invalid
request()->id ?
request()->invitee ?

回答by Vikram Bhaskaran

As far as Laravel 5.7 is concerned the preferred way to retrieve query params is

就 Laravel 5.7 而言,检索查询参数的首选方法是

if($request->has('invitee') {
    $request->query('invitee')
}

or using the helper function if you don't have access to $request

或者如果您无权访问 $request,则使用辅助函数

request()->query('invitee')

回答by Somnath Muluk

You can get input by:

您可以通过以下方式获得输入:

$invitee = Input::get('invitee');

For above 5.*

5.* 以上

$invitee = $request->input('invitee');

Or

或者

$invitee = Request::input('invitee');

回答by Alfonz

To check if inviteeparameter exists:

检查invitee参数是否存在:

if($request->has('invitee')) {
    // ...
}

To get the value of invitee:

获取 的值invitee

$invitee = $request->input('invitee');

回答by Alex Bouma

Are you calling $user->save()anywhere? You should call $user->save()to actually persist the data.

你在$user->save()任何地方打电话吗?您应该调用$user->save()以实际保留数据。

You can always check by calling dd($user);right after the second line in you example if you are worried it is not set correctly, this way you can see what attributes are set in the $userobject.

dd($user);如果您担心未正确设置,您可以随时通过在示例中的第二行之后立即调用来检查,这样您就可以查看$user对象中设置了哪些属性。

Also you can replace !empty($request->get('invitee'))with $request->has('invitee').

你也可以!empty($request->get('invitee'))$request->has('invitee').