Laravel like query exact match - laravel

I'm uploading a CSV file to my database and I want to automatically add a projecttype_id to a project, given this two project types:
Roads, new construction, widening
Roads, new construction
when I tried to upload "Roads, new construction" it always gives me the first id.
Tried removing % but still the same:
$projecttypeid = ProjectType::where('name', 'LIKE', "%{$projecttype}%")->first('id');

No need for LIKE if you want an exact match. where('name', $projecttype)

Related

MailChimp: Dynamic merge tag when only initial know for first name

We want to use the first name merge tag in our template. However for some small percentage of the audience we only have an initial and not the actual first name. Therefore the campaign would look like "Dear X" instead of "Dear Xavier" for example.
We want these to appear as "Dear Supporter". I know I could do this with an import and update the fields for the ones I have identified. However I do not want to run this every day for new signups.
Is there a way to create a conditional merge tag that results in the first name being used when the length is more than 1 and if the length of the first name is 0 or 1 then "Supporter" is used? Is this possible in MailChimp?
Thanks in advance!

Automate downloading of multiple xml files from web service with power query

I want to download multiple xml files from web service API. I have a query that gets a JSON document:
= Json.Document(Web.Contents("http://reports.sem-o.com/api/v1/documents/static-reports?DPuG_ID=BM-086&page_size=100"))
and manipulates it to get list of file names such as: PUB_DailyMeterDataD1_201812041627.xml in a column on an excel spreadsheet.
I hoped to get a function to run against this list of names to get all the data, so first I worked on one file: PUB_DailyMeterDataD1_201812041627
= Xml.Tables(Web.Contents("https://reports.sem-o.com/documents/PUB_DailyMeterDataD1_201812041627.xml"))
This gets an xml table which I manipulate to get the data I want (the half hourly metered MWh for generator GU_401970
Now I want to change the query into a function to automate the process across all xml files avaiable from the service. The function requires a variable to be substituted for the filename. I try this as preparation for the function:
let
Filename="PUB_DailyMeterDataD1_201812041627.xml",
Source = (Web.Contents("https://reports.sem-o.com/documents/Filename")),
(followed by the manipulating Mcode)
This doesnt work.
then this:
let
Filename="PUB_DailyMeterDataD1_201812041627.xml",
Source = Xml.Tables(Web.Contents("https://reports.sem-o.com/documents/[Filename]")),
I get:
DataFormat.Error: Xml processing failed. Either the input is invalid or it isn't supported. (Internal error: Data at the root level is invalid. Line 1, position 1.)
Details:
Binary
So stuck here. Can you help.
thanks
Conor
You append strings with the "&" symbol in Power Query. [Somename] is the format for referencing a field within a table, a normal variable is just referenced with it's name. So in your example
let Filename="PUB_DailyMeterDataD1_201812041627.xml",
Source = Xml.Tables(Web.Contents("https://reports.sem-o.com/documents/" & Filename)),
Would work.
It sounds like you have an existing query that drills down to a list of filenames and you are trying to use that to import them from the url though, so assuming that the column you have gotten the filenames from is called "Filename" then you could add a custom column with this in it
Xml.Tables(Web.Contents("https://reports.sem-o.com/documents/" & [Filename]))
And it will load the table onto the row of each of the filenames.

Searching for multiple prefixes inside a column through Eloquent using RegEx

I'm trying to build a simple filtering tool, which allows users to filter profiles through different US states (Arizona, Alabama, and so on)
So I'm using the state's 2-character name in order to identify them internally.
A profile has a field called "linear_coverage", which is an imploded string of State + County where the profile is enabled.
For example, a profile could have the following "linear_coverage"
NY_Cattaraugus,NY_Chautauqua,NY_Erie,NY_Genesee,NY_Niagara,NY_Wyoming,NY_Cattaraugus,NY_Chautauqua,NY_Erie,NY_Genesee,NY_Niagara,NY_Wyoming,NY_Erie,NY_Niagara,NY_Erie,NY_Niagara,AK_Kodiak Island,AK_Skagway
You can see this person has New York (NY) and multiple counties selected, but it also has Alaska (AK).
So I've been trying to use the following:
foreach($states as $state)
$query->orWhere('linear_coverages', 'LIKE', '%' . strtoupper($state) . '_%');
//$query->orWhere('linear_coverages', 'regexp', strtoupper($state) . "_[A-z]+");
The second comment line was a test I ran, without having success.
I'm thinking, is there anyway for a RegEx to search for MULTIPLE prefixes inside a string?
So I could be search for [NY_,AK_] and this would return me the profile I just showed, and the other which meet the criteria.
Thanks a lot.
M.
Finally found out to how to achieve this
I replaced my Regexp by the following
/(NY|AZ)_[A-z]+/
By enclosing the different states and separating them with | symbol, the Regex will try to find any of these prefixes, and then the remaining string is valid.
I was able to filter down all my results properly. :D

strip/gsub revert in ruby

I'm currently creating a title slugged version with the following.
title_slug = self.title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
In some cases I receive the slug and I should convert it back to the original name.
Is there any easy way to do the inverse of the code above (revert a slug name back to its original name.) in ruby ?
Well, no. You're removing information from the string (deleting all letters that aren't alphanumerics, for example), so you can't reconstruct them afterwards.
You can't even safely convert dashes back into spaces - they might have been dashes in the original string. (Thanks #XavierHolt!)
Your slugification function:
.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
is clearly a non-invertible function so you can't go back simply. If you need to go back, you have a couple options:
Store the slug in your database so that you can query it.
Implement your slugification function in your database language (SQL, JavaScript, ...) so that, again, you can query on it.
In either case, you'll get duplicates when you query by slug so either you prevent duplicates in the first place (by modifying the slugs to force uniqueness) or you figure out some way to pick which of the results you want.

Query multiple elements without specifying the element name

This may be a silly question, but is it possible to make a query using XPath without specifying the element name?
Normally I would write something like
//ElementName[#id = "some_id"]
But the thing is I have many (about 40) different element types with an id attribute and I want to be able to return any of them if the id fits. But I don't want to make this call for each type individually. Is it possible to search all of them at once, regardless of the name?
I am using this in an XQuery script, if that offers any help.
use * instead of name //*[#id = "some_id"]
It might be more efficient to look directly at the #id elements - //* will work, but will initially return every node in the document and then filter!
That may not matter in a small document, of course. but here's an alternative:
//#id[.="some_id"]/..

Resources