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').
Related
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
So, I have the following code:
$homepage = Homepage::first();
if (!$homepage) {
$homepage = new Homepage;
}
$homepage->first_presta_title = $request->first_presta_title;
$homepage->first_presta_content = $request->first_presta_content;
$homepage->second_presta_title = $request->second_presta_title;
$homepage->second_presta_content = $request->second_presta_content;
$homepage->third_presta_title = $request->third_presta_title;
$homepage->third_presta_content = $request->third_presta_content;
$homepage->shiatsu_text = $request->shiatsu_text;
$homepage->shiatsu_image = $request->shiatsu_image;
$homepage->doin_text = $request->doin_text;
$homepage->doin_image = $request->doin_image;
$homepage->save();
Everything works, but I wanted to see if there weren't any better way to save datas without asigning every single element to its column, then I found out someone answering to a question by using the following code:
$homepage->save($request->all());
So I tried it by myself, but nothing happened: no error, but also nothing saved in my database.
So, is there any fastest way to save datas ? Is it possible to use a loop to save everything?
Thank you in advance
When you use save(), you are actually using Mass assignment. So, either you explicitly define all the fields in your model to be mass assignable or you could use create() instead.
However, in your particular case, the whole method could be cleaned up to just one line:
return Homepage::updateOrCreate($request->all());
If you want the model to autofill based on a given array you need to create a new model entity Like this
$homepage = HomePage::create($request->all());
$homepage->save()
If you give an array to save() it expects the options for saving not for values to assign
Source:
laravel api docs for model/save()
laravel api docs for model::create()
I'm working on a old PHP project, that is running in legacy SQL query, which is good but I like to use query builders like Laravel Illuminate SQL package!
So i have added all required package dependencies to run Illuminate SQL, and this query builder seems to work fine with pagination!
$users = Capsule::table('users')->paginate(1)->toArray();
But, the paginator seems not to be able to listen the query string! For example, by running the above code it would give some properties like, next_page , previous_page etc...
And when I try to pass the query string in the URL it can't fetch the data from query string(From the GET request)!
Visiting this page http://app.app/?page=2 would give the same result set.
How i should configure the Illuminate sql package so it can also listen to the query strings?
EDIT
Also, i've tried to use the illuminate/http package, but the $request->all() method always returns an empty array! Here is the code:
<?php
require_once './vendor/autoload.php';
use \Illuminate\Http\Request;
$req = new Request();
echo '<pre>';
print_r($req->all());
echo '</pre>';
It returns empty input data array,
What i am missing to use the http package, any idea would be helpful.
You have to set a page resolver:
\Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') {
return (int) ($_GET[$pageName] ?? 1);
});
Laravel uses this resolver.
You need to notice, that paginator won't use other parameters for query string - it just uses page parameter to get valid results page. But for example if you use:
http://app.app/?page=2&price=2
it won't take from database results with price = 2 - it's your job to do this.
However the 2nd thing are urls generated by paginator.
If you do it like this:
$users = Capsule::table('users')->paginate(1);
You can also use in next line
$users->appends(request()->except('page'));
This will add all other parameters from query string (except page) to urls, so first_page_url will then contain all other parameters from request.
You can also wrap it using fluent syntax like this:
$users = Capsule::table('users')->paginate(1)->appends(request()->except('page'));
You need to fetch the query string in the controller to pass the get parameter to
paginate.
You can use the Illuminate\Http\Request class to receive and use the HTTP payload.
Your controller file:
<?php
use Illuminate\Http\Request;
class YourControllerClassName{
public function someFunction(Request $request){
echo "<pre>";
print_r($request->all());
print_r($request->input('page'));
$users = Capsule::table('users')->paginate($request->input('page'))->toArray();
}
}
I am using Symfony (version 2.5.0-DEV) and the doctrine mongodb cookbook on http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html.
I am stuck at the moment trying to pass the value of a defined parameter into findOneByIndex.
So if I do the following $script = $repository->findOneByIndex(1); it works perfectly.
But if I do the following $script = $repository->findOneByIndex($user); it fails to search with the value of user.
In routing.yml, I have this pattern: platform/designing/users/{user}/showuser and in the controller I simply do this under the showuserAction:
$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user);
Any help would be appreciated :).
In your last code example, what is the type of the $user variable? I assume it may be a string if it's a routing parameter and coming from the URI. You can use var_dump() to get the type and value in one shot.
Based on an earlier comment, you said that the Scripts document had the following fields:
_id
name (string)
description (string)
index (integer)
user_id (integer)
If the index field in your MongoDB document is an integer, you will need to use an integer in the query. For instance, the findOneByIndex('1') will not match a document with integer 1 in its field. A best practice here is to cast your values to the appropriate type before querying. It may also be best to stop relying on the magic DocumentRepository methods and explicitly define your own findBy methods, which do the casting internally. Then, your controller can pass a numeric string directly from a routing or request parameter and not have to worry about doing the integer cast on its own.
Also, to comment on your original code example:
$script = $repository->findOneByIndex($user);
This was for the routing pattern platform/designing/users/{user}/showuser. You said that this failed to find a result. I assume the $user argument to your controller is a user ID. If that is the case, why were you querying on the index field instead of user_id?
This value should be an integer, not object. Please pass the id, or index value - I don't know how it's stored in mongodb.
$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user->getId());
Do you pass $user to showuser action? like this (notice $user as parameter):
public function showuserAction($user)
{
$repository = $this->get('doctrine_mongodb')->getManager()->getRepository('MyMpBundle:Scripts');
$script = $repository->findOneByIndex($user);
}
PS. I'd recommend you to change var $user to $userId just not to mistake it with $user object
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.