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 ?
Related
I'm using spring version 5.0.8.RELEASE and I am trying to make a search in the database with an email address using NamedParameterJdbcTemplate. If I search with the whole address I'm getting no results
getJdbcTemplate().queryForList("SELECT p.* FROM users p WHERE p.email = :email", new MapSqlParameterSource(){{addValue("email", "a.alexandrakis#company.com")}})
if I change the code to this I'm getting one result, as expected
getJdbcTemplate().queryForList("SELECT p.* FROM users p WHERE p.email like :email", new MapSqlParameterSource(){{addValue("email", "a.alexandrakis#%")}})
and if change the code to this I'm getting no results again
getJdbcTemplate().queryForList("SELECT p.* FROM users p WHERE p.email like :email", new MapSqlParameterSource(){{addValue("email", "a.alexandrakis#c%")}})
It seems that something is happening if the # character is on the string. Do I have to escape it somehow?
Thanks in advance.
After all there is no issue... I was looking to another database with similar data.
I'm trying to add a new feature to an existing Laravel codebase and in that codebase there's this:
$hasGAP = (new \App\Models\Policy)->where('leadID', $leadId)
->where('policystatus', '!=', 'Canceled')
->get()->where('product.name', 'GAP Insurance')->count() > 1;
So this is doing an SQL query on the table referenced by the \App\Models\Policy model. It's doing WHERE policystatus != 'Canceled' and then it's getting the result. And then it's doing a WHERE on the result? That doesn't make sense to me.
Also, product.name isn't a column in the table. Indeed, it seems like the period (.) operator would be an illegal character..
Does this code actually work and if so what is it actually doing?
The ->get() ends the query and returns the results in a collection.
The subsequent ->where(..) and ->count() are then calls on the collection.
The dot notation is widely used in Laravel for getting sub fields of arrays, objects and similar data structures (example: array_get()) and works in ->where() (on a collection) as well.
So the posted code should work. I assume the Policy belongsTo (or hasOne) a product and the dot notation is used to search by the related product name.
I am using slim3 eloqent/laravel and am trying to build a query to delete an entry from the database using multiple where clauses.
According to laravel's documentation this query should delete correctly;
$deleteGalleryItem = Home_Page::where("ul_id",$ul)
->where("ul_update_no",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
I also tried;
$deleteGalleryItem = Home_Page::where("ul_id","=",$ul)
->where("ul_update_no","=",$ul_update_no)
->delete();
var_dump($deleteGalleryItem);
die();
However each time I run the var_dump I get returned integer 0
Is this the correct way to structure a mysql delete statement in eloquent/laravel in slim3?
Or should I first select the data then delete?
There is nothing wrong with the way you've constructed your query.
The number returned is how many rows were removed with that query, so the reason you'll be getting 0 is simply because you didn't have any rows in the database with those constraints.
Hope this Helps!
I was wondering if i could get a bit of help.
Is there any way to change the 'find' parameters to search in another column?
If i use
$test = Model::find($id)
This searches the ID field of a table, but what if i want to search by the token column instead.
Is there any way of doing that?
Cheers,
You can use:
$test = Model::where($column, '=', $value)->first()
The find method is attached to the primary key. You could maybe override it in your model, but you have other ways to achieve your goal without overriding it.
I'm fairly new to Laravel having come over from Codeigniter and for the most part I really like it, but I really can't get my head around Eloquent.
If I want to do a simple query like this:
SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id
I try doing something like this (with a "belongs to"):
$site = Site::with('tweeter')->find($site_id);
But now I have two queries and an IN() which isn't really needed, like so:
SELECT * FROM `site` WHERE `id` = '12' LIMIT 1
SELECT * FROM `tweeter` WHERE `id` IN ('3')
So I try and force a join like so:
$site = Site::join('tweeter', 'tweeter.id', '=', 'site.tweeter_id')->find($site_id);
And now I get an error like so:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
SQL: SELECT * FROM `site` INNER JOIN `tweeter` ON `tweeter`.`id` = `site.tweeter_id` WHERE `id` = ? LIMIT 1
Bindings: array (
0 => 12,
)
It's obvious where the error is, the where needs to use something like "site.id = ?". But I can't see anyway to make this happen?
So i'm just stuck going back to fluent and using:
DB::table('site')->join('tweeter', 'tweeter.id', '=', 'site.tweeter_id')->where('site.id','=',$site_id)->first()
I guess it's not a massive problem. I would just really like to understand eloquent. I can't help but feel that i'm getting it massively wrong and misunderstanding how it works. Am I missing something? Or does it really have to be used in a very specific way?
I guess my real question is: Is there anyway to make the query I want to make using Eloquent?
I actually find this behaviour advantageous. Consider this (I'll modify your example). So we have many sites and each has many tweeters. Each site has a lot of info in the DB: many columns, some of them text columns with lots of text / data.
You do the query your way:
SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id
There are two downsides:
You get lots of redundant data. Each row you get for a tweeter of the same site will have the same site data that you only need once so the communication between PHP and your DB takes longer.
How do you do foreach (tweeter_of_this_site)? I'm guessing you display all the sites in some kind of list and then inside each site you display all of it's tweeters. You'll have to program some custom logic to do that.
Using the ORM approach solves both these issues: it only gets the site data once and it allows you to do this:
foreach ($sites as $site) {
foreach($site->tweeters as $tweeter) {}
}
What I'm also saying is: don't fight it! I used to be the one that said: why would I ever use an ORM, I can code my own SQL, thank you. Now I'm using it in Laravel and it's great!
You can always think of Eloquent as an extension of Fluent.
The problem you're running into is caused by the find() command. It uses id without a table name, which becomes ambiguous.
It's a documented issue: https://github.com/laravel/laravel/issues/1050
To create the command you are seeking, you can do this:
$site = Site::join('tweeter', 'tweeter.id', '=', 'site.tweeter_id')->where('site.id', '=', $site_id)->first($fields);
Of course, your syntax with join()->find() is correct once that issue fix is adopted.