Hello I would like to know is it possible to get column value as string. Instead of array in array:
Current query: Number::limit('1000')->get(['number'])->toArray()
The result at the moment is this:
Preferable result:
Before your toArray() call, add pluck('number'):
$result = Number::limit('1000')->get(['number'])->pluck('number')->toArray();
That's it! This will pluck just the number attributes from your result collection, and give you a single-level array.
The reason this works, is because you are getting a Collection back from get():
All multi-result sets returned by Eloquent are an instance of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship.
And the pluck method:
https://laravel.com/docs/5.1/collections#method-pluck
Update
Another, even more succinct method provided by #wunch in the comments:
$result = Number::limit('1000')->lists('number')->toArray();
Related
Database table SITE has many columns. One of them is site_id. I need all the site_ids as an array since it has to be fed to a method which accepts only a string array.
What I tried so far is:
$sites = DB::select('select site_id from site_tab');
$sites_arr = $sites->toArray();
But this doesn't produce the result I want. I need $sites_arr to be like ['A','B','C',...]
Please suggest a way to get this done. A solution based on Eloquent is also OK for me.
Thanks
Try this:
DB::table('site_tab')->pluck('site_id')->toArray();
reference pluck
referen toArray
If you open a manual, you will see that
The select method will always return an array of results
So, there's no need to use ->toArray(), as result is already an array.
To get values as array of names you can do:
$site_ids = DB::table('site_tab')->pluck('site_id');
Using ->toArray() here is optional, as you can iterate over $site_ids (which is a Collection) with a foreach too.
The following query returns every Quote's id-s, instead of returning just one:
Quote::find(1)->pluck('id'); // --> [1, 2, 3, 4, ...]
Why is this happening?
Update
Strangely, the query:
Quote::where('id', 1)->pluck('id'); // --> [1]
works as expected.
There are (basically) two kinds of pluck() - one that works on collections, and one that works on Builder objects. When you do
Quote::where('id', 1)->pluck('id');
then you have a Builder object, since prior to the plucking you haven't returned anything from the database - you're still building the query. So that works, because the plucking is added to the query and executed.
However, when you do Quote::find(1), you get a Quote object back - which is neither a Builder nor a Collection. Instead, Laravel creates a new query where it tries to pluck values from the database for you, based on which model you're on. In this case, that's the Quote.
This behaviour was changed in 5.2, so in 5.1 you'd get just the column value back - 1 in your case, as expected. But pluck() is taking over what used to be called lists() in earlier versions of Laravel, starting from 5.2. So your results are correct - however confusing.
Quote::find(1) is returning a Quote object, not a Collection of Quotes. You are then calling pluck('id') on that quote object. Since the Quote object doesn't actually have a pluck() method, what this is actually doing is creating a new query builder instance for quotes, and calling pluck on that. It is the same as if you did Quote::pluck('id'). That is why you are getting all of the ids.
Since Quote::find(1) is going to return the Quote object, you don't need to call pluck() on it. You can just access the property.
$quote = Quote::find(1);
echo $quote->id;
How can I do multiple wherein in laravel?
$devices = DB::table('foo')
->select('foo.*')
->whereIn('bar1', $request->bar1)
->whereIn('bar2', $request->bar2)
->get();
Above is my sample code but it is returning me an empty array.
It is ok to use multiple WHERE IN constraints in your query. The code you provided is also ok.
If you're getting no results, make sure that values of $request->bar1 and $request->bar2 are what you expect - they should be arrays of values that contain what you want your bar1/bar2 columns to be.
You can always get the generated SQL by calling toSql() instead of get(), you can also inspect the values of parameters by calling getBindings().
I'm using laravel FindOrNew() to get an entry with two parameters, or create a new one:
$option = \App\Option::findOrNew(['user_id' => $this->id , 'option_name' => $optionName]);
I want to get an option for a user that has the name in $optionName. The problem is that it just checks for the user_id, and does not create a new one when option_name does not exist.. instead it "finds" one which does not match the $optionName value..
Can someone say what I'm doing wrong? How can I achieve this?
TL;DR:
You're using the wrong method. You're looking for the firstOrNew() method, not findOrNew().
Explanation:
The findOrNew() is an extension of the find() method, which works on ids only. It takes two parameters, the first being the id (or array of ids) to find, and the second being the columns to retrieve. It's treating the array you've passed in as an array of ids to find.
The firstOrNew() method takes one parameter: an array of attributes to search for. It will turn the array into a where clause, and then call first() on the query builder. If no results are returned, it returns a new instance with those attributes filled in.
I was reading the page https://www.codeigniter.com/user_guide/database/results.html
and I read if $row = $query->first_row() this gives object
and if we want this as array then we have to use
$row = $query->first_row('array') and this is ok.
But, can we replace these functions?
$row = $query->row(); //for object
and
$row = $query->row_array(); //for array
to a single function with different parameter.
Also if these functions have parameters like index,class then we can use the above functions like
$row = $query->row(array('index'=>5,'type'=>'array','class'=>'Users'));
//for array mentioned type here
$row = $query->row(array('index'=>5,'type'=>'object','class'=>'Users'));
//for object mentioned type here
Need some Guidance.
Thanks.
to a single function with different parameter. i think you are asking for a core system file modification here.. which is not good at all...why??? because what if you need to update your codeigniter version later on...you need to update your core files for that which in turn will replace your modified code....
anyways..the only difference between these two function (row and row_array) is.. row returns the datas as object whereas row_array() returns in array.....
what row() does is it gets single result in object...
This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.
example
$query=$this->db->query('select * from table where id= "1"');
$row=$query->row() //these returns i object
$row=$query->row_array() //this returns in array..
however you can pass two optional parameters to row() function.. one being the row number of specific rows return... if not mentioned it returns the first row.. and second which is , name of a class to instantiate the row with.. you can go through the guide..for more information