Delete Join in query builder Laravel - laravel

I have a delete query like this:
DELETE prefix_tableA, prefix_tableB, prefix_tableC
FROM prefix_tableA
INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id
INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id
WHERE prefix_tableA.id = 148
Now I want to convert it to query builder. I searched a lot of posts here but still not found expected answer.
I don't want to use DB::raw because I have prefix in my table name. When I change the prefix, my query will error.
Anyway to write this in query builder?
Thank you!

You can do this using DB::delete() or DB::statement()
Try this query :
\DB::delete('DELETE prefix_tableA, prefix_tableB, prefix_tableC FROM prefix_tableA, prefix_tableB, prefix_tableC INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id WHERE prefix_tableA.id = 148');

Try this Query your Controller
DB::table('prefix_tableA')->join('prefix_tableB','prefix_tableA.user_id','=','prefix_tableB.user_id')->join('prefix_tableC','prefix_tableB.user_id','=','prefix_tableC.user_id')->where('prefix_tableA.id',148)->delete('prefix_tableA', 'prefix_tableB', 'prefix_tableC');

Thanks for your reply guys!
Like I said, I config my table prefix in config/database.php, so use raw query will be error if I change the prefix name in that file.
So my only way for me to do this is write a delete query in each single table and call it in my controller. I implemented about 3 model, long code, but it's the only way in Laravel!

You should try as this way. This way works form me.
$raw_query = 'DELETE prefix_tableA, prefix_tableB, prefix_tableC
FROM prefix_tableA
INNER JOIN prefix_tableB ON prefix_tableA.user_id = prefix_tableB.user_id
INNER JOIN prefix_tableC ON prefix_tableB.user_id = prefix_tableC.user_id
WHERE prefix_tableA.id = ?';
$status = \DB::delete($raw_query, array('148'));
Documentation: http://laravel.com/docs/database#running-queries

Related

how to convert query this query to laravel

SELECT *
FROM `digital_useraccess`
left join digital_publisher ON
access_publisher = pub_auto
left join digital_issue ON
issue_publisher = pub_auto
WHERE 1
without using laravel DB class method
Try this as reference
ModelName::where(conditions)
->leftjoin('digital_publisher','access_publisher','pub_auto')
->leftjoin('digital_issue','issue_publisher ','pub_auto')
->select('column_1','column_2',...)->get();
This might help :
::query()->where('<cplumn>' , '<operator>' ,
'<value>')->leftJoin('digital_publisher', 'access_publisher',
'=','pub_auto')->leftJoin('digital_issue', 'issue_publisher', '=',
'pub_auto')->select('<columns>')->get();/find();/first();

Is it safe to use DB :: select and others in Laravel?

Queries are not always simple, and sometimes I need to create a pure SQL query, the query builder also does not fit.
При использовании DB::select, подготовливаются ли
переменные, которые подставлены в запрос?
Will there be a sql injection in this case?
$mastersInCity = DB::select('SELECT
master_user.master_id,
masters.specialization,
category_letter_master.category_letter_id AS master_letter,
COUNT(*) AS count_in_city
FROM master_user
LEFT JOIN masters ON master_user.master_id = masters.id
LEFT JOIN category_letter_master ON category_letter_master.master_id = master_user.master_id
WHERE ' . $chooiseId . ' = ' . $cityId . ' GROUP
BY master_user.master_id, master_letter');
Or, in this case, it is better to use PDO directly, so as to manually prepare the request yourself, is it possible?
$mastersInCity = DB::select('SELECT
master_user.master_id,
masters.specialization,
category_letter_master.category_letter_id AS master_letter,
COUNT(*) AS count_in_city
FROM master_user
LEFT JOIN masters ON master_user.master_id = masters.id
LEFT JOIN category_letter_master ON category_letter_master.master_id = master_user.master_id
WHERE ? = ? GROUP
BY master_user.master_id, master_letter', [$chooiseId, $cityId]);
This is equivalent to a prepared statement.
Docs: https://laravel.com/docs/5.7/database#running-queries
Edit: I am sure this can be done with eloquent simply, there is nothing too complex here. Something like:
MasterUser::with(['master', 'master_letter'])->withCount()->where($chooiseId, $cityId)->get()

Join Nhibernate

I have list with ids
List<int> listOfIds;
And I have a query with cars objects.
I would like to do something like this:
query = from e in query
join b listOfIdson e.b.Id equals b
select e;
but then I get error:
Specified method is not supported.
I know that its possible to do that by Join method which is avaiable on my query, but I have no idea how to do this. I can run following Join methods on my query:
query.Join()
query.JoinGroup()
Thats all
Thanks for help
Could you try with QueryOver ?
var results = session.QueryOver<Car>().AndRestrictionOn(x=> x.id).IsIn(listOfIds)

linq where clause problem on multiple join tables

var studentDetails = from aspUser in db.aspnet_Users
join aspMembership in db.aspnet_Memberships on
aspUser.UserId equals aspMembership.UserId
join expUser in db.explore_users on
aspUser.UserId equals expUser.aspnetUserId
where expUser.Id.Equals(studID)
select new { expUser.DOB,
aspMembership.Email,
aspUser.UserName,
aspUser.LoweredUserName,
expUser.Id };
gv1.DataSource = studentDetails;
gv1.DataBind();
I have no idea why this did not work. When i remove where clause everything is running.
I have try to put where expUser.Id == studID is also did not work
if anyone can help me pls
Shouldn't that be where expUser.aspnetUserId.Equals(studID)?
If ID is the correct column then (as already commented) of what type are expUser.ID and the variable studID? Are they of the same type? If so then does the studID value exist in db.explore_users?

Doctrine query only returning one row?

I'm new to Doctrine but somewhat familiar with SQL. I have a very simple schema with Users and Challenges. Each Challenge has a "challenger id" and a "opponent id" which are foreign keys into the User table. I want to print a list of all challenges, with the output being the names from the User table. Here is my Doctrine query;
$q = Doctrine_Query::create()
->select('u1.name challenger, u2.name opponent')
->from('Challenge c')
->leftJoin('c.Challenger u1')
->leftJoin('c.Opponent u2');
The problem is that this only returns one row. I've used the getSqlQuery() command to look at the generated SQL which ends up being:
SELECT u.name AS u__0, u2.name AS u2__1 FROM challenge c
LEFT JOIN user u ON c.challenger_id = u.id
LEFT JOIN user u2 ON c.opponent_id = u2.id
When run in a 3rd party SQL client this query retrieves all of the rows as expected. Any idea how I can get all of the rows from Doctrine? I'm using $q->execute() which I understand should work for multiple rows.
Thanks.
For me it worked by chaning the hydration mode:
$result = $query->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
Set result set then returns an array instead of objects.
I just ran into this issue and in my case the problem was that my query didn't select any field from the FROM table. Example:
$query = Doctrine_Query::create()
->select(
'ghl.id as id,
ghl.patbase_id as patbase_id,
ghl.publication_no as publication_no,
ghl.priority_no as priority_no
'
)
->from('GridHitListContents ghlc')
->leftJoin('ghlc.GridHitList ghl')
As you can see there is no selected field from the GridHitListContents table.
with a $query->count() I got 2000ish results, but with $query->fetchArray() only the first one.
When I added
$query = Doctrine_Query::create()
->select(
'ghlc.id,
ghl.id as id,
...
'
)
->from('GridHitListContents ghlc')
->leftJoin('ghlc.GridHitList ghl')
I got back all my results.
$query->fetchOne() work fine for me.
Use this $result = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY)

Resources