How do I convert this query in laravel - laravel-5.4

can someone help me please how can I conver this to db query in laravel
select a.extn
from access as a
where not exists( select * from users as u where u.accesskey= a.accesskey)
Thank you in advance.

Assuming your model name for access table is Access :
$result = Access::select('extn')->whereNotIn('accesskey', User::select('accesskey')->toArray());

Related

Laravel 7 how to perform a query sql with join in mongoDB

I have 2 databases(one in mySql and the other one in MongoDB) in my project and I need to perform a query like this
public function getPosts() {
$user = Auth::user();
$follow = DB::connection("mongodb")
->collection("followers")
->where("user_id", $user->id)->get();
/*$res = DB::select("
SELECT *
FROM posts
WHERE user_id = ? OR user_id IN
(SELECT user_id from $follows)
ORDER BY created_at DESC", [$user->id, $user->id]);
*/
return response()->json($res);
}
This is a query which returns the posts from the logged user and from people the user follow
The followers table (the one in MongoDB) contains "user_id" and "follows_id"
The commented line is the original query (every table in one single database on mySql
Thank you
Edit: I solved through a query in mongodb, then I edited the result to get an array which I incorporated into the sql query through orWhereIn Thank you for your answers :)
I don't think so... you may try a true multimodel database such as Oracle XE (it's free) to achieve your goal here...

How to perform an update containing a sub query on an Eloquent Relationship in laravel 5

I am new to laravel. I'm working on this laravel 5 app but got stuck on trying to perform a query like below.
update applications
set application_status = 'sa'
where id in (select application_id from application_cart where cart_id = 12);
Appreciate help on how to translate this query.
The query below does the trick
return Application::whereIn('id', function($query) use ($id){
$query->from('application_cart')
->select('application_id')->where('cart_id', $id);
})->update(['application_status' => 'sa']);

How to call sql table' info into asp.net?

I want to connect and load any fields of table into a web form.more explain is that ,I want a SQL server connection in a web form and choose a database and then load all of fields and data type into a table.
for example i've a table with below features :
field type
id int
name nvarchar(50)
email nvarchar(50)
now i want to call them into web form not their values or records!
i hope explain it clearly
thanks for your solutions for help me...
In SQL you can make a query for sys tables ... you can try something like this
select tab.name, col.name, typ.name, col.max_length, * from sys.columns col
join sys.tables tab on col.object_id = tab.object_id
join sys.types typ on col.system_type_id = typ.system_type_id
where tab.name = 'Test'
Just change name of table and give it a go, you can use result of that query in your form

Sql- how to use relational field in condition

I have three tables which is mapped like this: paymentDetails <-employee<-designation.
Now I have to get datas from paymentDetails table by particular designation of employee..
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
And I am using Yii2 framework How can I achieve this in Yii2.
I get unknown column error. How to resolve this ?
$command = Yii::app()->db->createCommand()
->select(*)
->from('paymentDetails')
->where('payment_date=date')
->andWhere('employee.designation.desig_id=2')
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
this will not work in SQL either, it is beacause you are using the tables employee and designation and you do not actually join them in any way.
Now you have not given us any details regarding the name of the models, but it should be something like
$paymentDetails = PaymentDetails::find()->joinWith('employee.designation')-where(['employee.designation.desig_id' => 2, 'payment_date' => 'date'])->all();
This will execute
select *
from paymentDetails JOIN employee ON 'theDefinedRelation' JOIN designation ON 'theSecondDefinedRelation' where payment_date=date and employee.designation.desig_id=2;
Anyway, it will be a long day, if you do not know why the SQL fails you have to learn SQL first.

Symfon2 database record inserion

I have a problem with symfony2 record insertion
My requirement is to find out all the users who are in between particular ages and the given column is dob.
I am getting the result with this mysql query.
SELECT * FROM app_users WHERE YEAR(CURDATE())-YEAR(dob) BETWEEN 10 AND 20;
How to rewrite this query in the symfony- doctrine fromat?
Pls help...
$qb = $em->createQueryBuilder();
$qb->select('au')
->from('AppUsers au)
->where($qb->expr()->between('YEAR(CURDATE())-YEAR(au.dob)', 10,20));
$result = $qb->getQuery()->getResult();
I have not checked it. but it may help you.