I want to delete the user account as well as I want check if the user contains any 'posts', 'messages', 'friends' and if it does I want it to delete them as well.
I haven't made any relations in their models.
I am having hard time develop this logic can anyone help me with it.
Here's what I have done so far which dosen't works:-
public function delete()
{
$delete = User::find(Auth::user()->id);
$post = DB::table('posts')
->where('created_by', '=', Auth::user()->uuid)
->get();
$messages = DB::table('messages')
->where('from', '=', Auth::user()->id)
->where('to', '=', Auth::user()->id)
->get();
$friend = DB::table('friends')
->where('my_id', '=', Auth::user()->id)
->where('friends_id', '=', Auth::user()->id)
->get();
$delete->delete();
$post->delete();
$message->delete();
$friend->delete();
}
get() will give you an array, so delete() may not work after you use ->get.
Try this :
$delete = User::find(Auth::user()->id);
$delete->delete(); // Example of Model
$post = DB::table('posts')
->where('created_by', '=', Auth::user()->uuid)
->delete();
$messages = DB::table('messages')
->where('from', '=', Auth::user()->id)
->where('to', '=', Auth::user()->id)
->delete();
$friend = DB::table('friends')
->where('my_id', '=', Auth::user()->id)
->where('friends_id', '=', Auth::user()->id)
->delete();
Related
My laravel "where" is getting ignored it seems. ->where('products.hide_product', '=', 'N')
Any ideas on what I am doing wrong?
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images','products.product_id','=','images.product_id')->where('img_priority','=','1')->orWhere('img_priority', '=', null)
->where('products.hide_product', '=', 'N')->where('products.group_id', '=', $groupID)->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name','products.product_id','products.product_name','products.collect_part_no','brands.brand_name','categories.cat_name','products.status','images.file_type','products.pop','products.color_label','products.ai_complete'));
You can move the join in a closure an use the first where there:
$products = Product::join('brands', 'products.brand_id', '=', 'brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function ($join) {
$join->on('products.product_id','=','images.product_id')
->where('img_priority','=','1')
->orWhere('img_priority', '=', null);
})
->where(
['products.hide_product', '=', 'N'],
['products.group_id', '=', $groupID],
)
->select('products.en_71', 'products.astm', 'images.file_name', 'products.product_id', 'products.product_name', 'products.collect_part_no', 'brands.brand_name', 'categories.cat_name', 'products.status', 'images.file_type', 'images.img_priority', 'products.pop', 'products.color_label', 'products.ai_complete')
->orderBy('img_priority', 'DESC')
->get();
Please try below code.
$products = Product::join('brands','products.brand_id','=','brands.brand_id')
->join('categories','products.cat_id','=','categories.cat_id')
->leftJoin('images', function($join)) {
$join->on('products.product_id','=','images.product_id')
->where(function($query){
$query->where('img_priority','=','1')
->orWhere('img_priority', '=', null)
})
}
->where(function($query) {
$query->where('products.hide_product', '=', 'N')
->where('products.group_id', '=', $groupID)
})
->orderBy('img_priority','DESC')
->get(array('products.en_71','products.astm','images.file_name',
'products.product_id','products.product_name',
'products.collect_part_no', 'brands.brand_name',
'categories.cat_name', 'products.status',
'images.file_type','products.pop','products.color_label',
'products.ai_complete')
);
I am using Laravel v4.2. I want to delete conversation between two users but Now I want that If one user delete the conversation than other user can view the conversation until second user also delete the conversation.
For this I have two column to delete conversation name "delete_on" and "delete_two". For this purpose I am using eloquent chunk method which always return false or null.
$return = Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
if(empty($msg->delete_one)){
$msg->delete_one = $userData['id'];
}else{
$msg->delete_two = $userData['id'];
}
if($msg->save()){
}
}
});
To know more about chunk() refer to this answer mentioned in the comments Thanks Gokigooooks for the acknowledgement.
The syntax you need to employ is the following for in your case it's eloquent:
Eloquent
For first of all check whether you are getting chunk result here like this:
Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
dd($msg); or print_r($msg);
}
});
Now try this
$return = Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
if(empty($msg->delete_one)){
$msg->delete_one = $userData['id'];
}else{
$msg->delete_two = $userData['id'];
}
$msg->save();
}
});
I need the same query for two different user roles. Difference is only in one whereNotIn condition.
So for the Basic user it would be:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->whereNotIn('ChannelName', $sky)
->get();
And for Premium:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->get();
I know I can do it with simple if statement:
if($user->userRole == "Basic"){
//first $chart2
}
else{
//second $chart2}
but I have a lots of queries where I need just to add or remove this whereNotin condition and rewriting the queries (using if statement) is not a nice solution.
Try scope.
In your TVModel.php:
public function scopeConditionalWhereNotIn($query, $doesUse, $col, $val) {
if($doesUse)
$query->whereNotIn($col, $val);
}
Usage:
$condi = true;//or false.
$chart2 = TVModel::select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->conditionalWhereNotIn($condi, 'ChannelName', $sky)
->get();
Inside your model add this:
public function scopeBasicUser($query,$channel){
return $query->whereNotIn('ChannelName', $channel);
}
and in your controller:
$query = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid);
if($user->userRole == "Basic")
$query = $query->basicUser($channel);
return $query->get();
$userRole = $user->userRole;
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->where(function ($query) use ($userRole){
if($userRole == "Basic"){
$query->whereNotIn('ChannelName', $sky)
}
})
->get();
This code worked for me.
I'm using laravel 5 in current project. Here is my query to database:
$users = User::where('status', '=', '3')->orWhere('status', '=', '2')->whereExists(function($query1)
{
$query1->select(DB::raw(1))
->from('firsts')
->whereRaw('firsts.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query2)
{
$query2->select(DB::raw(1))
->from('seconds')
->whereRaw('seconds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query3)
{
$query3->select(DB::raw(1))
->from('thirds')
->whereRaw('thirds.user_id = users.id')
->where('status', '=', 'approved');
})
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})
->get();
Idea behind that is to pick all valid users. As you can see at the begining of query I wrote first condition for users. The problem is that users with status = '2' will never have their thirds table status = 'approved'. Is there possibility to put if statment before $query3? Thanks in advance!
Try the query below
$users = User::whereIn('users.status', array(2, 3))
->leftJoin('firsts f', 'f.user_id', '=', 'users.id')
->leftJoin('seconds s', 's.user_id', '=', 'users.id')
->leftJoin('thirds t', 't.user_id', '=', 'users.id')
->where(function($query) {
$query->where('users.status', 2);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
})
->orWhere(function($query) {
$query->where('users.status', 3);
$query->where('f.status', 'approved');
$query->where('s.status', 'approved');
$query->where('t.status', 'approved');
});
I'm not sure what to do with the last part of your query, as it has no relation to any of users, firsts, seconds, thirds:
->whereExists(function($query4) use($category_id)
{
$query4->select(DB::raw(1))
->from('jobcategories')
->where('provider_id', '!=', 0)
->where('category_id', '=',$category_id);
})
Trying to query a model with a lower level relationship (agreement) and I cannot get chaining to work. I tried the three methods below (the second get me 99% of the way there, but it misses out on one of the results - an inactive equipment "status" and an "inactive" agreement (should have been omitted). What am I doing wrong?
Every piece of equipment needs to be "active" and have an active existing agreement (an active agreement is one that is either "Active", "Cancel on expiry" or "Unsigned").
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->where('status', '=', 'Active')
->orWhere('status', '=', 'Unsigned')
->orWhere('status', '=', 'Cancel on expiry');
})->Get();
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('status', '=', 'Active')
->orWhere(function($q2)use($month)
{
$q2->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})
->where('status', '=', 'Unsigned')
->orWhere(function($q3)use($month)
{
$q3->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})
->where('status', '=', 'Cancel on expiry')
->orWhere(function($q4)use($month)
{
$q4->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
});
})->Get();
$thing = Equipment::wherehas('agreement', function($q)use($month)
{
$q->where('status', '=', 'Active')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->orWhere('status', '=', 'Unsigned')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%')
->orWhere('status', '=', 'Cancel on expiry')
->where('equipment.status', '=', 'Active')
->where('maintenance_months', 'like', '%'.$month.'%');
})->get();
i am not quite understand why the where equipment.status is being filter for the same thing over and over again, if i am not understand wrongly the following code might be the result you are looking for.
$thing = Equipment::whereHas('agreement', function($q)use($month)
{
$q->where('maintenance_months', 'like', '%'.$month.'%')
->whereIn('status',['Unsigned','Active','Cancel on Expiry'])
})->where('status','Active')->get();