First i do the DB request.
$user=DB::table('pupil')->select('accountName')->where('accountName', '6001')->get();
But it returns this data.
[{"accountName":"6001"}]
And i need this data.
6001
I want only to echo 6001 and not [{"accountName":"6001"}].
if you want to get only one value then used first method here..
$user = DB::table('pupil')->select('accountName')->where('accountName', '6001')->first()->accountName;
or used value method here..
you may extract a single value from a record using the value method.
DB::table('pupil')->where('accountName', '6001')->value('accountName')
Try this below
$value='{"accountName":"6001"}';
$return=json_decode($value);
print_r($return->{'accountName'});
use the value method by laravel
$user=DB::table('pupil')->where('accountName','=', '6001')->value('accountName');
return $user;
Output:
6001
Related
I use Laravel 5.8, and I want simply use a default value for description if it's empty, and take summary.
// summary variable request is equal to "test"
$summary = $request->get('summary', null);
$request->get('description', $summary)
But, the field is present, empty, and description give me null instead of summary value. Summary value is "test".
To get information from a request you should use get(), input() or the name directly. There is no documentation for the get method on requests for recent Laravel versions. For the input method on Laravel 5.8 the documentation says
You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request
It says it only works if it is not present so I would do it as simple as this
$description = $request->description ? $request->description : $request->summary
It really depends on what you want to achieve after all this and how you want your data.
Possible Solutions
My first impressions were that the data may not be being sent through correctly but upon looking over your code again, I realized you are using the more deprecated function ->get('description').
Try using ->input('description) instead. I personally have never used ->get(), so maybe this could be the problem.
https://laravel.com/docs/5.8/requests
I need to call a method from CLI which uses the Request::input so I had to manually create the Request object and the below code is not working in my case. can you please advice.
$request = \Illuminate\Support\Facades\Request::create('script/run', 'POST');
\Illuminate\Support\Facades\Route::dispatch($request);
$request->request->add(['adjustment' => '10']);
I am trying to call the value from another class
dd(\Illuminate\Support\Facades\Request::input('adjustment'));
output shows as null but it supposed to show the value 10
I also tried the below option and it did not work as well
$request = app('request');
$request->request->add(['adjustment' => '10']);
dd($request->all());
returns empty array. If I am able to add input here, it will work for me.
You're almost there, but when you create the request you are assigning it to a variable, however, when you go to die and dump the value of 'adjustment' you're calling a new request which doesn't have the value of 'adjustment'
To obtain this simply call it from the request you've assigned to $request.
dd($request->input('adjustment'));
Found the answer finally, I had to use merge instead of add. below code worked for me.
$request = app('request');
$request->merge(['adjustment' => '10']);
dd(\Illuminate\Support\Facades\Request::all());
back in the (real) days, we used to use $_GET, $_POST! and now we got Laravel's \Request::input(). Consequently here is whats happening:
if(\Request::isMethod('post'))
{
$POST = \Request::input();
}
if I have the a variable in $_GET, the value gets in the POST as well.
For example:
&x=1 //i.e. in the query string
$_POST['x'] = null; //as it was not posted with the form, but it could be as there is a field with same name
$POST['x'] = 1; //as its in the GET, but should be null as its not in the $_POST!
Any solution to get POSTed vars only? Or shall I just use $_POST?
Thanks
I believe the only way to get this from the Request instance is to access either the query (GET) or request (POST) property. These are both ParameterBag instances, so you'll probably use the ->get() method of them to access the parameter you want.
I have the following url
http://project.su/?invitee=95
first i want to check the invitee in url, if the url have invitee then get the value.
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 invitee result(id) in database.
Thanks.
To determine if an input value is present:
if ($request->has('invitee')) {
$user->invitee = $request->input('invitee');
}
The has method returns true if the value is present and is not an empty string:
As far as Laravel 5.7 is concerned, the preferred way to retrieve query params is
if( $request->has('invitee') ) {
$request->query('invitee');
}
or using the helper function if you don't have access to $request
request()->query('invitee');
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
http://localhost:8000/users/{id}?invitee=95
To read id and invitee, we can use
$id ✔
$invitee ✖ - Invalid
request()->id ✔
request()->invitee ✔
You can get input by:
$invitee = Input::get('invitee');
For above 5.*
$invitee = $request->input('invitee');
Or
$invitee = Request::input('invitee');
To check if invitee parameter exists:
if($request->has('invitee')) {
// ...
}
To get the value of invitee:
$invitee = $request->input('invitee');
Are you calling $user->save() anywhere? You should call $user->save() to actually persist the data.
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 $user object.
Also you can replace !empty($request->get('invitee')) with $request->has('invitee').
hii..I want to edit single row.i used
$this->data = $this->BuildingProperty->read(null,$id);
but it didnt fetch the values of this id.
so what can i do. Give me any suggestion.
Why did you pass null as the first parameter? It should be a string on array of fields that you want to retrieve.
Anyway, try this instead:
$this->BuildingProperty->id = $id;
$this->data = $this->BuildingProperty->read();
The syntax which you are using is correct. First check if the $id is integer. (echo $id). Then if so, check your database if it has such record in building_properties table check if this ID exist. Finally check if the variable $this->data is populated with the proper values.
All those checks return you proper values then the problem is not in Model->read() function.
Another hint, try to clear the cache /app/tmp/cache/modules and /app/tmp/cache/persistent
Are you calling the method from a controller that knows about BuildingProperty? i.e. BuildingPropertiesController. If not, have you included a
var $uses = array('BuildingProperty');
statement in the class definition or explicitly loaded the model with, for example,
loadModel('BuildingProperty')
Your syntax is correct and the only other explanation if there is no warning or error message is that the returned array is empty, i.e. the record does not exist.
Check that you have debug turned on:
Configure::write('debug', 1); // or higher.A 2 will output SQL queries as well
then try
debug($this->BuildingProperty->read(null,$id));
You should at least get some output telling you the line of the debug call.