Laravel with PostgreSQL and Postgis point - laravel

I develop an application which allows to post(show) a list of restaurants according to the location(localization) of the user.
For that purpose, I have a base(basis) of datum containing bars with their latitudes-longitudes.
My database is under postgresql and I would like to use, all my back-service(back-office) is under Laravel. At present to add a POINT, I make: ST_GeogFromText('SRID=4326;(110 30)'), but on Laravel, the request SQL ( db:seed ) includes the ST_ guillement enter.
DB::table('establishments')->insert(['location' =>ST_GeogFromText(\'SRID=4326;POINT(-110 30)\'),]), but ST_GeogFromText is not a fonction --
The error message:
parse error - invalid geometry HINT: "ST"
How may I make to manage PostgreSQL-Postgis and Laravel?

ST_GeogFromText is a PostGIS function not a PHP/Laravel function, and you cannot wrap it in quotes (i.e. ""), as Laravel will then consider it a string.
Instead, pass a raw query expression like below:
DB::table('establishments')->insert([
'location' => DB::raw("ST_GeogFromText('SRID=4326;POINT(-110 30)')"),
// your other columns & values
]);

Related

Why does laravel query problem with PLUS sign

I am fetching a simple record from database using following query:
$user = User::where('email','myemail+33#gmail.com')->first(); and its returning an empty result.
but when I tried to match other email for example:
$user = User::where('email','myemail#gmail.com')->first(); this one return the proper object.
It seems like plus sign is stripping from ORM but not sure anybody face and resolve problem like this before ?

JSON_CONTAINS doesn't recognize my column mysql v.8.0.17 laravel 5.6

I have to do a query similar to this:
SomeModel::whereRaw("JSON_CONTAINS(column, '', '$.a')")->get();
column contains JSON *{"a":null}*;
With mysql version 5.7 everything works fine, i get empty collection but on a database on digitalocean which is mysql v8.0.17 i get this error:
Illuminate/Database/QueryException with message 'SQLSTATE[22032]: <>: 3141 Invalid JSON text in argument 1 to function json_contains: "The document is empty." at position 0. (SQL: select * from 'table' where JSON_CONTAINS(column, '', '$.a'))'
I'm stuck on this for days, please help! :)
The problem is empty string that you are passing as argument.
Try:
SomeModel::whereRaw("JSON_CONTAINS(column, null, '$.a')")->get();
The problem is you want to search a json field inside a json column, so you need a json array for that an not just an empty value.
Try something like:
SomeModel::whereRaw("JSON_CONTAINS(`column`, cast('[]' as json), '$.a')")->get();
Also use `` for your column names.
Since you are using laravel 5.7, you could also use the eloquent functions that are shipped with laravel like:
SomeModel::whereJsonContains('your_json_field','your_value')->get();

How to show DBMS name and version in laravel view?

I want to show a status bar in a laravel website. Which will show the DBMS name and version number. The output I want is like
PostgreSQL 9.2.24
Or
MySQL 5.6
I can get the name of database by using env('DB_CONNECTION'). It is giving me the name(although, it is showing 'pgsql'; not 'PostgreSQL ').
However, I don't understand how can I get the version number in view. What is the way to get the version number?
You can use DB::raw() to create database raw expression.
$version = DB::select( DB::raw("select version()"));
pass the value to your view.

How can I use an OVER statement in spotfire when connected to an external database?

Usually, I have been using the following calculated column when importing the data from an excel file:
(Sum([Units]) - Sum([Units]) OVER (PreviousPeriod([Axis.Columns]))) / Sum([Units]) OVER (PreviousPeriod([Axis.Columns])) * 100 as [% Difference]
In this scenario, however, the data is coming directly from an Oracle database.
When I try to create the calculated column, I get the error message:
"Could not find function: 'PreviousPeriod' "
I have done some research and found that I should be using the THEN keyword, but I have the same problem when I try to insert it after the aggregated expression.
You need to import that date via an INFORMATION LINK or EMBED the data in your analysis in order to use the majority of the functions in SPOTFIRE. If you must keep your data EXTERNAL, that is not connected via in Information Link or Embedded, you will not be able to use all the functions within SPOTFIRE.

Execute Store Command Entity Framewrok

Hello I am using Entity Framework and I am using the ExecuteStoreCommand to do a query against the database. well I am basically calling a user defined function.
This is the call:
string result = m.ExecuteStoreQuery (SQL).FirstOrDefault();
I query the User Defined Function and I get the following result.
2.09,2.06,2.06,2.0098,2.04,2.04,2.04,2.04,2.04,2,2.1,2.04,2.04,2.04
The return type for the user defined function is
RETURNS Varchar(200). The result above is from the same cell.
When I execute the code from MVC controller I get the following error.The data reader has more than one field. Multiple fields are not valid for EDM primitive types.
What datatype I should be using instead of string.
Any ideas and suggestions.
Apparently, the return type is not only a string.
On the SQL Server side, try to surround the code of your query with:
SET NOCOUNT ON
<your current sql code>
SET NOCOUNT OFF
Maybe EF is getting the rows affected in the result and gets messed up.

Resources