How to find a particular object in a array - rethinkdb

How to find a particular object in a array.
Below is my code
r.table("tablename").filter(
function(doc){
return r.expr(["value1","value2"]);
});

You have forgotten to add .contains in return statement. Try the rectified one -
r.table("tablename").filter(
function(doc){
return r.expr(["value1","value2"]).contains(doc("someKey"));
});

Related

Dump within map function in Laravel

Within a map function i would like to use dd(); dump but i only get the first object.
When i use print_r(); within a map function i get all objects
$valeurCategorieCible = $CategoriesItineraires->map(function ($item) {
return $item->ciblesParCategorie->map(function ($ciblesParCategorie) {
return $ciblesParCategorie->cibles->map(function ($items) {
print_r($items); // Return all objects
dd($items); // Return only the first object then stop !
});
});
});
According to the documentation for dd() it "dumps the given variables and ends execution of the script" - the important part is "ends execution of the script"!
If you don't want to end execution use dump() instead of dd()

Laravel Factory ->each() add iterator

I have a factory:
factory(Survey::class, 3)->create()->each(function ($survey)
{
factory(Question::class, 1)
->create()
->each(function ($question)
{
factory(Option::class, rand(2,3))->create();
});
});
The problem is - I need to add an index to every option. It should be 1,2,3...
How can I add iterator to ->each() function?
I've tried to add variables inside body of the loop, but in not works as for/foreach one.
ANy ideas?
If I get your question right.
The each method iterates over the items in the collection and passes each item to a callback. If you will add second argument to each it will be an index of current element, which you can use later inside.
$collection->each(function ($item, $key) {
//
});
https://laravel.com/docs/5.7/collections#method-each

Pass a property of the model to the template in ItemView

I want to pass a property of my model to the template, so I presume I need a serializeData function, I tried this
serializeData:function(){
return this.model.toJSON().extend({_schema:this.model.schema});
}
but it complained about not being able to extend the output of toJSON. This must be a standard trick, stick some value from the prototype into the serialised form so the template can get it's hands on it.
Use templateHelpers for this use case – serializeData works better for replacing the model attributes entirely, or scoping them down.
templateHelpers: function()
{
return { _schema: this.model.schema };
}
Harladson's answers is the best, but in case someone else comes looking different approach you can do this:
serializeData:function(){
var data = this.model.toJSON();
data._schema = this.model.schema;
return data;
}

Route to two methods of controller with one row in Laravel 4

It is simple to route this way:
Route::get('user/profile', 'PaymentsController#profile');
Route::get('user/delete', 'PaymentsController#delete');
I want to do this with one row:
Route::get('user/{subsection}', 'PaymentsController#'.$subsection);
but my syntax seems to be wrong. Is it possible to be done with one row? It would be nice if it is possible.
No, you can't do that, but you can make proxy method
Route::get('user/{subsection}', 'PaymentsController#profileDelete');
And method will looks like
public function profileDelete($subsection) {
return $this->$subsection();
}
public function profile(){}
public function delete(){}
Also, you can bind {subsection}
Route::bind('subsection', function ($subsection) {
if (!in_array($subsection, ['profile', 'delete'])) {
throw new Exception;
}
return $subsection;
});
Not exactly one row, but you can do this:
Route::get('user/{subsection}', function($subsection){
if(!method_exists('PaymentsController', $subsection)){
App::abort(404);
}
return App::make('PaymentsController')->callAction($subsection);
});
Alternatively to the method_exists you could also use a route condition to only allow a predefined set of subsections:
Route::get('user/{subsection}', function($subsection){
return App::make('PaymentsController')->callAction($subsection);
})->where('subsection', '(profile|delete)');

getting the value of the single field output using the codeigniter active record

the following function is supposed to read the name of the given asset code from the database. but it triggers the error: "Trying to get property of non-object"
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get()->result()->row('name');
}
All I want is to have the name of the asset returned back to the controller! Your help is highly appreciated!
Use row() like,
return $this->db->get()->row()->name;
Use row() for a single row, and result() for multiple rows.
do like this, asset_types is your table name
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
return $this->db->get('asset_types');
}
And in your controller acess it like
$result=$this->modelname->sban_name('$asset')->row();
$name=$result->name;
I think it's important to check if the record that satisfies the conditions even exists in the database. Code for the model:
function sban_name($asset){
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code',$asset);
$row = $this->db->get()->row();
if (isset($row)) {
return $row->name;
} else {
return false;
}
}
Simply call the function from the controller like so:
$response = $this->model_name->sban_name($asset)
Try this code of block , I already checked and works fine:
function sban_name($asset)
{
$this->db->select('name');
$this->db->from('asset_types');
$this->db->where('code', $asset);
return $this->db->get()->row()->name;
}

Resources