Question: how to use and with where in laravel - php

when I want to get data from a database in native PHP I use queries but in laravel, you don't write queries you use their model and their functions to get the data for example:
ex::where('name','test')->get();
in native PHP:
select * from ex where name='test'
and my issue is I don't know how to use and like this query:
select * from ex where name='test' and id='5'
I searched and looked thru the documentation but no answer was found.

Just put another where, its really simple just put it like this
ex::where('name', 'test')->where('id', 5)->get();
//or if you want directly the instance and not a collection
ex::where('name', 'test')->find(5);
If you want to use the OR operator you can also do something like this
ex::where('name', 'test')->orWhere('id', 5)->get();
More in documentations

There is no 'AND' with where in Laravel Eloquent.
You have to write another 'where' with the required condition.
Another way is: run queries using the DB facade. The DB facade provides methods for each type of query: select, update, insert, delete, and statement.
Ex. DB::statement("UPDATE teachers SET price = ".$price." where id=".$id." AND status='A'");
Do not forget to use DB

Related

Automatically add where clause to every queries on a model

Currently I am to do Model::where('status', 1) to filter off inactive models before adding any other subqueries to it. I find myself doing this all the time and Its quite stressful as sometimes I could forget to add that part. I wish to have something that will automatically add the where clause anytime I run a query on the model. Something that works like the typical Laravel SoftDeletes that adds the WHERE deleted_at IS NULL clause automatically to all queries.
Example:
Instead of writing Model::where('status', 1)->first(), I could just write Model::first() and this will be automatically transformed to Model::where('status', 1)->first().
How do I achieve this please ?
What you're looking are the
Query Scopes

Insert from select in laravel using eloquent

I am using laravel 8. I want to insert multiple rows by selecting from one table to another. I am using DB::statement() but now want to do it using eloquent. Here is my sample code:
DB::statement("insert into bank_information (user_id, bank_id, account_name,account_number)
select applicant_id,bank_id,account_name,account_number from applicant_bank_information
where erec_applicant_bank_information.applicant_id =".$applicant_id);
How can I do it using single eloquent command ? Also, Is there any other smarter/faster way for it in laravel ??
I do not know of a Eloquent way for this, but after Laravel 5.7.17 we have a new method in the query builder insertUsing(). Unfortunately this is not mentioned in the official documentation.
The query builder code will be something like this:
$select = DB::table('applicant_bank_information as abi')
->select('applicant_id','bank_id','account_name','account_number')
->where('abi.applicant_id', $applicant_id);
$rows_inserted = DB::table('bank_information')
->insertUsing(['user_id','bank_id', 'account_name','account_number'], $select);
Tested and working. Keep in mind that you do not run the select request with ->get().

Use a join statement and save the results as a relation like "with" does? - Laravel 5.2

I'm trying to get data from multiple relationships. But I don't want to end up with multiple queries. So the with method won't work for me.
I want to use joins to get the data needed, but laravel overwrites keys if they are duplicated. Is there a way to save the results from a join as a relation. Something like this below (join is incorrect I know).
Post::select('post.*', 'category.*')->join('category');
If both have an 'id' field it's overwritten by the other. So I would like to have category.* results as a relation so I can call ->category->id like I can when I use the with method.
Is there any way to do this?
You may use the whereHas and orWhereHas methods
Check this link:
http://laravel.io/forum/04-19-2014-many-to-many-to-many-in-wherehas
And Laravel Documentation:
https://laravel.com/docs/5.1/eloquent-relationships

How I get comma-separated values form table in symfony2?

I have a MySQL table which contains comma-separated values like this:
first row=(A,AB)
second row=(AC, AE)
I want to select the rows which have A in their set not AC OR AE. I am using symfony2 in my code.
$query = $query->andWhere("FIND_IN_SET('".trim($tag)."',advNews.advNewsTags)");
Its Return Error: Expected known function, got 'FIND_IN_SET'
But if i use query in sqlyog like
select * from advice_news where adv_news_type = 'news' AND FIND_IN_SET ('sdf',adv_news_tags)
its work fine.
Tell me any solution i will use FIND_IN_SET in symfony2.
You need to add FunctionNode to Doctrine and Symfony. I had the same problem with SOUNDS LIKE. You can find my solution at http://piotrpasich.com/full-text-searching/ in SOUNDEX chapter.

Laravel 4 Build Query where clause on the fly

I am porting my code from CodeIgniter to Laravel. and have some question regarding the query builder.
In codeigniter, I can just add where clause to the active record object, as I initialize each property in a class like
$this->db->where('xxxx','bbbb');
in one property initialize function, and
$this->db->where('yyyy','aaaa');
in another property function, and it will all chain up until i fire off the query. But this doesn't seem to be the case of Laravel.
Here is what I do in laravel in each property initialize function
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
and when a actual method is call from outside, it runs
DB:table($this->table)->get();
but this gives me a SELECT * FROM TABLENAME without anywhere clause. So what am I doing wrong here :x or I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
Also in codeigniter, you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel? Thank you!
You can assign your current workings to a variable, and build upon that, let me show you an example based on your example:
Instead of this
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
Try this...
$query = DB::table($this->table)->where('xxxx','bbbb');
$query->where('yyyy','aaa');
$results = $query->get();
I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
This is not dynamic where clause.
and please, make a habit of reading the documentation.
From the docs of Fluent query builder
$users = DB::table('users')->where('votes', '>', 100)->get();
you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel?
$users = DB::table('users')->remember(10)->get();
Next time, just open up the docs. they contain all this.

Categories