Route Slug Always Being Queried - Laravel 5.4 - laravel-5.4

Here's my web.php file:
Route::get('/items', 'ItemsController#index');
Route::get('/items/create', 'ItemsController#create');
Route::get('/items/{item}', 'ItemsController#show');
Route::post('/items', 'ItemsController#store');
Route::delete('/items/{item}', 'ItemsController#destroy');
Route::get('/items/deleted', 'DeletedItemsController#index');
How do I prevent the /items/deleted slug from being considered a wildcard?
NotFoundHttpException No query results for model [App\Item].

Put /items/deleted route definition above wildcard:
Route::get('/items', 'ItemsController#index');
Route::get('/items/create', 'ItemsController#create');
Route::get('/items/deleted', 'DeletedItemsController#index');
Route::get('/items/{item}', 'ItemsController#show');
Route::post('/items', 'ItemsController#store');
Route::delete('/items/{item}', 'ItemsController#destroy');

Related

Alias Sequelize

I have this query in sequelize:
Domicilio.hasMany(User, {foreignKey: 'UserId'});
const abonados = await Domicilio.findAll({include: User});
Which result is the following:
SELECT domicilio.DomicilioId, _users.UserId AS _users.UserId, _users.UserName AS _users.UserName, _users.FullName AS _users.FullName, _users.Password AS _users.Password, _users.Documento AS _users.Documento, _users.Cuit AS _users.Cuit, _users.Email AS _users.Email, _users.FechaBajada AS _users.FechaBajada, _users.FechaContrato AS _users.FechaContrato, _users.FechaNacimiento AS _users.FechaNacimiento, _users.Phone AS _users.Phone, _users.FailedPasswordCount AS _users.FailedPasswordCount, _users.IsActive AS _users.IsActive, _users.IsLocked AS
_users.IsLocked, _users.IsTestUser AS _users.IsTestUser, _users.LastLoginDate AS _users.LastLoginDate, _users.createdAt AS _users.createdAt, _users.createdBy AS _users.createdBy, _users.deletedAt AS _users.deletedAt, _users.deletedBy AS _users.deletedBy, _users.updatedAt AS _users.updatedAt, _users.updatedBy AS _users.updatedBy, _users.CondicionIVAId AS _users.CondicionIVAId, _users.OnuId AS _users.OnuId, _users.ServicioId AS _users.ServicioId FROM domicilio AS domicilio LEFT OUTER JOIN _user AS _users ON domicilio.DomicilioId = _users.UserId;
But I don't need the ALIAS of each of the columns of table _user. E.g _user.UserId I need only the name of each column without alias. Is there any way to aproach this?
There are some ways to change the value of the returning column
First I would suggest that you fetch only the columns really needed
by you at the model endpoint that way the response is concise.
Please refer the code below in which you can see how you can provide
alias in an association, the alias you provide in there is the one
which is used during the include you can fiddle around to get the
desired behavior. Also, you can tweak your findAll with option
params like {raw: true}
Official Docs:
https://sequelize.org/master/manual/assocs.html
https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findAll
Ship.belongsTo(Captain, { as: 'leader' }); // This creates the `leaderId` foreign key in Ship.
// Eager Loading no longer works by passing the model to `include`:
console.log((await Ship.findAll({ include: Captain })).toJSON()); // Throws an error
// Instead, you have to pass the alias:
console.log((await Ship.findAll({ include: 'leader' })).toJSON());
// Or you can pass an object specifying the model and alias:
console.log((await Ship.findAll({
include: {
model: Captain,
as: 'leader'
}
})).toJSON());
Second but not very recommended way is to provide a raw query with
the parameter [Do not use javascript template string to pass the
args] here you have control over the params and conditions
Docs for raw query: https://sequelize.org/master/manual/raw-queries.html
Lastly, you can use alias in attribute to customize the value's key
to anything you like
const abonados = await Domicilio.findAll({
include: User,
attributes: [['UserId', 'newUserId'], ['UserName 'newUserName']]
});
P.S: literal might also come in handy at times.

Rails dynamically add condition to mysql query

How to add condition dynamically to sql query
for example if i have one element than it will look like
query=['one_element']
User.where('name LIKE ?, %"#{query[0]}"%')
but if it more than one
User.where('name LIKE ? and LIKE ? and Like... , %"#{query}"%', ..so on)
Im use myslq
so my main goal to split search query if it contains more than 2 words and search by them separately in one sql query
not where(name:'john dou') but where(name:'john' and name:'dou')
If you have a version of MySQL that supports RLIKE or REGEXP_LIKE:
User.where("RLIKE(name, :name_query)", name_query: query.join("|"))
Otherwise, you'll have to manually build with the ActiveRecord or operator:
# In the User model
scope :name_like, ->(name_part) {
return self.all if name_part.blank?
where("name LIKE :name_query", name_query: "%#{name_part}%")
}
scope :names_like, ->(names) {
relation = User.name_like(names.shift)
names.each { |name| relation = relation.or(name_like(name)) }
relation
}
Then you can pass it an array of any name partials you want:
query = ["john", "dou"]
User.names_like(query)
First split the word by it's separator like this query.split(' ') this will give you array of words. Then you can use like below in rails.
User.where(name: ['John', 'dou']

Rails i18n API Triple Dashes/Hyphens, Ellipsises and Newlines

I am using i18n API for a purpose. I seed a MySQL database with:
Translation.find_or_create_by(locale: 'en', key:'key1', value: 'value1')
However, after seed, the data is saved on database as:
locale: en
key: key1
value: --- value1\n...\n
All columns are varchar(255) and 'utf8_unicode_ci'.
On Rails i18n documentation, I could not find an explanation for this.
Because of that problem, I can not use find_or_create_by() method. It do/can not check the value column and adds duplicate entries.
Is there any solution for that?
Translate model:
Translation = I18n::Backend::ActiveRecord::Translation
if Translation.table_exists?
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend)
end
What you're seing in your value column is the value serialized to YAML (that's done by the I18n::Backend::ActiveRecord::Translation); which is required, among other things, for pluralization.
#find_or_create_by doesn't work nicely when the value stored in the database needs serialization
To do a simple seed try:
Translation.create_with(value: 'value1').find_or_create_by(locale: 'en', key: 'key1')

Vuejs changes order of json_encoded array, when decodes it back from props in vuejs component

Php:
$json_string = "{
"26":{"blabla":123,"group_id":1,"from":"27.08.2018","to":"02.09.2018"},
"25":{"blabla":124,"group_id":1,"from":"20.08.2018","to":"26.08.2018"},
"24":{"blabla":125,"group_id":1,"from":"20.08.2018","to":"26.08.2018"}
}"
my.blade.php template:
<my-component :records={{ $json_string }}></my-component>
MyComponent.vue:
export default {
props: ['records'],
...
mounted: function () {
console.log(this.records);
}
Output is:
{__ob__: Observer}
24:(...)
25:(...)
26:(...)
And when I use v-for, records in my table in wrong order (like in console.log output).
What I am doing wrong?
EDIT:
I figured out 1 thing:
When I do json_encode on collection where indexes are from 0 till x, than json string is: [{some data}, {some data}]
But if I do ->get()->keyBy('id') (laravel) and than json_encode, json string is:
{ "26":{some data}, "25":{some data}, "24":{some data} }
Then how I understood, issue is in different outer brackets.
In Javascript keys of objects have no order. If you need a specific order then use arrays.
Here is documentation for keyBy Laravel method: https://laravel.com/docs/5.6/collections#method-keyby
I wanted to have ids for rows data to fast and easy access without iterating over all rows and check if there is somewhere key Id which is equals with my particular Id.
Solution: not to use keyBy method in Laravel and pass json string to Vue component like following [{some data}, {some data}] (as I described in my Question Edit section) - this will remain array order as it used to be.
I found this short and elegant way how to do this, instead of writing js function by myself:
Its find() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Example:
let row = records.find( record => record.id === idToFind );

tRestClient Talend with dynamic URL and paramters from DB returns null

I'm trying to use Talend from a REST web service and by passing parameters that come from a DB.
I̶ ̶a̶m̶ ̶t̶r̶y̶i̶n̶g̶ ̶t̶o̶ ̶l̶i̶n̶k̶ ̶t̶P̶o̶s̶g̶r̶e̶s̶q̶l̶I̶n̶p̶u̶t̶ ̶c̶o̶m̶p̶o̶n̶e̶n̶e̶t̶ ̶t̶o̶ ̶t̶h̶e̶ ̶t̶R̶e̶s̶t̶ ̶c̶o̶m̶p̶o̶n̶e̶n̶t̶ ̶a̶n̶d̶ ̶t̶o̶ ̶s̶e̶e̶ ̶h̶o̶w̶ ̶t̶o̶ ̶p̶a̶s̶s̶ ̶D̶B̶ ̶r̶o̶w̶s̶ ̶v̶a̶l̶u̶e̶ ̶i̶n̶ ̶t̶h̶e̶ ̶U̶R̶L̶,̶ ̶b̶u̶t̶ ̶i̶t̶s̶ ̶s̶e̶e̶m̶s̶ ̶t̶h̶a̶t̶ ̶t̶R̶E̶S̶T̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶a̶c̶c̶e̶p̶t̶ ̶t̶h̶i̶n̶g̶s̶ ̶l̶i̶k̶e̶ ̶t̶h̶i̶s̶.̶
T̶h̶i̶s̶ ̶i̶s̶ ̶w̶a̶h̶t̶ ̶I̶ ̶d̶i̶d̶ ̶u̶n̶t̶i̶l̶ ̶t̶h̶i̶s̶ ̶t̶i̶m̶e̶ ̶:̶
̶t̶P̶o̶s̶g̶r̶e̶s̶q̶l̶I̶n̶p̶u̶t̶ ̶x̶x̶x̶x̶x̶x̶ ̶t̶R̶E̶S̶T̶ ̶-̶-̶-̶>̶ ̶t̶E̶x̶t̶r̶a̶c̶t̶J̶S̶O̶N̶F̶i̶e̶l̶d̶s̶ ̶-̶-̶-̶>̶ ̶t̶M̶a̶p̶ ̶-̶-̶-̶>̶ ̶t̶P̶o̶s̶g̶r̶e̶s̶q̶l̶O̶u̶t̶p̶u̶t̶
I verified that the DB component returns data below:
I updated the job as this:
The Schema of tRESTClient is :
And I used globalMap to pass the values from the database:
The used URL is : "URL/search/"+ (String)globalMap.get("row1.hashtag")
But when I see the results, I found it used the "null" value to request the server.
All I needed is to use a tFlowToIterate component to iterate each row, so that we will able to access the input data extracted from DB and set a dynamic URL.
For example as I have before, the job must look like this :
BBin --main(row1)--> tFlowToITerate --iterate--> tREST ---> tExtractJSONFields ---> tMap ---> DBout
On tRest, we can set a dynamic URL like:
"SOME_URL/otherpath/"+(String)globalMap.get("row1.columnName")