I am using Laravel 5.1
And I need to convert array to diffrent array
So I am having
$wells = Well::get(array('well_id' => 'url','well' => 'name','iso3'))->toArray();
$users= User::get(array('id' => 'url', 'name' => 'name'))->toArray();
So from bouth of the arrays the output should be converted from well_id to url from wells and id to url from users. So even if they are different from the database the output will have the same 'name' because after I am having a function for both but it uses this 'name'.
So the examle I have given is not working but it should be something like this.
Thank you.
You can use select function of Laravel
$wells = Well::select('well_id as url','well as name','iso3')->get()->toArray();
and
$users= User::select(array('id' => 'url', 'name' => 'name'))->get()->toArray();
i haven't try it but it should work.
Related
I'm trying to create a custom id like this:
YYMM-increment.
example: 2101-0001, 2101-0002
the increment will reset to 0001 each year.
How to create this id format.. Im try to use haruncpi laravel id generator but its not working
Thank You.. I already found the solution. All code is setup at the controller
Note for myself in the future:-
First create a variable = find all row in table using count() function.
Then use a substr() function to get year and last increment
Do a conditioning and edit increment.
Lastly, store updated value to variable.
if you use package laravel idgenerator , just add 'reset_on_prefix_change' => true
$config = [
'table' => 'sales',
'field' => 'id',
'length' => '12',
'prefix' => 'RS-'.date('ym'),
'reset_on_prefix_change' => true
];
output => RS-221100001, RS-221100002, RS-221100003
output => RS-221200001, RS-221200002, RS-221200003
I have an array in the post request as an example below:
$data = array(
array('title'=>'1st title', 'desc'=>'desc'),
array('title'=>'2nd title', 'desc'=>'desc'),
array('title'=>'3rd title', 'desc'=>'desc'),
)
Is there a way in Laravel using Eloquent I can save above data without using foreach? Note that the array keys which I am getting in the request is not same as column names of the table.
I hope this would help you
$data = [
['title' => '1st title', 'desc' => 'desc'],
['title' => '2nd title', 'desc' => 'desc']
.....
];
DB::table('users')->insert($data);
Put all the values you want to insert in to an array and then pass it to the insert function.
Source: https://laravel.com/docs/5.1/queries#inserts
try this:
DB::table('table_name')->insert($data);
Using eloquent: just as mentioned by Sethu, but a few lines will be:
Model::insert($data); // eg: Posts::insert($your_request_array);
Just pass in the array directly here: above will return true on success.
I have this code.
$this->mergeWhen($request->stats=='partner_count',
['partner_count' => $this->partner_count])
Instead of $request->stats=='partner_count', I want to pass an array of request values. Is it possible or not?
Yes you can. As per the Laravel Manual you can pass an array like this
$this->mergeWhen($this->isAdmin(), [
'first-secret' => 'value',
'second-secret' => 'value',
]),
I have been succesful in uploading my data to algolia using laravel. My create method looks like this:
Beer::firstOrCreate([
'name' => $beer['title'],
'description' => $beer['description'],
'path' => '/uploads/gall/' . $filename,
'brewery_id' => '',
'abv' => $abv
]);
It gathers data from an api and works perfectly, except for the abv value. It needs to be a numeric type in algolia but whenever I check it in algolia it is a string.
Even when I force it to a float or int by using:
(float)$abv
I still end up with a string in my Algolia database. Funny enough the primary id for this record that is being auto incremented is not a string and looks fine in the algolia database.
To implement algolia in laravel I used the laravel helper in my model:
// Send new records to the algolia database
use AlgoliaEloquentTrait;
I hope somebody can maybe give me slight hint of what I could be doing wrong.
I am assuming that the data type of 'abv' inside your database is float and not string, if this is the case then try using floatval($abv). Your code would be something like:
Beer::firstOrCreate([
'name' => $beer['title'],
'description' => $beer['description'],
'path' => '/uploads/gall/' . $filename,
'brewery_id' => '',
'abv' => floatval($abv)
]);
Cheers,,
When I'm trying to select custom attributes from record
$data = Emotion::find()
->select('em_id, em_name, tbl_post.post_id, tbl_post.post_header')
->where(['em_id' => $id])
->joinWith('posts')
->one();
I've got an answer model, where related field have full set of table fields.
Trace message looks like this:
app\models\Emotion#1
( [yii\db\BaseActiveRecord:_attributes] =>
[ 'em_id' => 4
'em_name' => 'test_em']
[yii\db\BaseActiveRecord:_related] =>
[ 'posts' =>
[ 0 => app\models\Post#2 ( [yii\db\BaseActiveRecord:_attributes] =>
[ 'post_id' => 10
'post_header' => 'some header'
'post_date' => '2015-06-24 13:40:25',
'post_descr' => 'Rocco, continue the joke'
'post_content' => 'test content'...
So, maybe i did something wrong or this is a framework issue.
Found a workaround here: Yii2 GitHub issue
Which for my case looks like this:
$data = Emotion::find()
->select('em_id, em_name, tbl_post.post_id, tbl_post.post_header')
->where(['em_id' => $id])
->joinWith('posts')
->createCommand()
->queryAll();
And the output of this request will be an raw array.
Is there an another way to have an ActiveRecord object in a response data?
You should simply modify the relation query, e.g. :
$data = Emotion::find()
->select('em_id, em_name')
->where(['em_id' => $id])
->with([
'posts' => function($query) {
$query->select('post_id, post_header');
}
])->one();
And you don't need joinWith since you don't use posts in the main query.