Can somebody tell me why using delete() method won't delete the selected row in table? SoftDelete works well but the row that has been soft delete is still exist in the table. I'm expecting my table row will be hide or deleted but it can't. Any tips or help would appreciated! :) I SoftDelete my table like this.
Controller:
public function hideApprovalsDocument(Request $request, Document $id)
{
//Getting the request in the View.
$id = $request->get('softDelete');
$hide = Document::findOrFail($id)->where('id', '=', $id);
$hide->delete();
return redirect()->back();
}
public function documentsSentForApproval()
{
$pendingDocuments = DB::table('approvals_document')
->select('documents.title', 'documents.content', 'documents.id as documentId',
'categories.category_type',
'users.username', 'approvals_document.created_at',
'approvals_document.id', 'approvals_document.approver_id', 'approvals_document.requestedBy')
->join('documents', 'documents.id', '=', 'approvals_document.document_id')
->join('categories', 'categories.id', '=', 'documents.category_id')
->join('users', 'users.id', '=', 'approvals_document.approver_id')
->where('approver_id', '=', Auth::id())
->where('documents.deleted_at', '=', null)
->orWhere('requestedBy', '=', Auth::id())
->orderBy('approvals_document.id', '=', 'desc')
->paginate(10);
return view ('document.pending')
->with('pendingDocuments', $pendingDocuments);
}
Can you please change the join query to below
->join('documents', function ($join) `{ $join->on('documents.id', '=','approvals_document.document_id')
->whereNull('documents.deleted_at'); })`
Soft delete won't delete the row. It'll set a timestamp for the deleted_at field and eloquent will ignore any rows which have a value set for the field deleted_at. If you implemented soft delete and want to permanently delete a row, try using 'forceDelete()'
$hide->forceDelete();
You can't do any comparison to NULL value in Laravel SQL via where method. You may use whereRaw method:
public function documentsSentForApproval()
{
$pendingDocuments = DB::table('approvals_document')
->select('documents.title', 'documents.content', 'documents.id as documentId',
'categories.category_type',
'users.username', 'approvals_document.created_at',
'approvals_document.id', 'approvals_document.approver_id', 'approvals_document.requestedBy')
->join('documents', function ($join) {
$join->on('documents.id', '=', 'approvals_document.document_id')
->whereRaw('documents.deleted_at IS NULL');
})
->join('categories', 'categories.id', '=', 'documents.category_id')
->join('users', 'users.id', '=', 'approvals_document.approver_id')
->where('approver_id', '=', Auth::id())
->orWhere('requestedBy', '=', Auth::id())
->orderBy('approvals_document.id', '=', 'desc')
->paginate(10);
return view ('document.pending')
->with('pendingDocuments', $pendingDocuments);
}
Update
Joining soft-deleted records must be modified
Related
I'm not that good with Laravel Eloquent, so be cool pls.
I have 1 product table and 4 "target" tables (sales, novelties, liquidations and exposure).
I'm trying to get all products that have at least one of the four "targets" with this query:
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->where('noveltys.novelty_end_at', '!=', null)
->orwhere('sales.sale_end_at', '!=', null)
->orWhere('liquidations.liquidation_end_at', '!=', null)
->orWhere('exposures.exposure_end_at', '!=', null)
->get();
Problem is, it only return products that have the first "where" clause true. (In other word, currently, this query only return produtcs with a not null novelty_end_at, it doesn't return products with a sale_end_at and others).
How can I achieve that it also return products with a not null sale_end_at, exposure_end_at and a liquidation_end_at ?
Thank you!
seems a syntax mistake
->orwhere('sales.sale_end_at', '!=', null)
should be
->orWhere('sales.sale_end_at', '!=', null)
also you can try using orWhereNotNull
like
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->whereNotNull('noveltys.novelty_end_at')
->orWhereNotNull('sales.sale_end_at')
->orWhereNotNull('liquidations.liquidation_end_at')
->orWhereNotNull('exposures.exposure_end_at')
->get();
I found the solution, it's almost the same thing, execpt that I'm using orWhereNotNull and that I'm using DB::table at the start instead of Product::leftjoin directly.
return DB::table('products')
->leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->whereNotNull('sales.sale_end_at')
->orwhereNotNull('noveltys.novelty_end_at')
->orwhereNotNull('liquidations.liquidation_end_at')
->orwhereNotNull('exposures.exposure_end_at')
->get();
Thank you!
Perhaps you could use ->orWhereHas() method. To use this function, you must define the relationship in your Sales and the four target Model classes. Documentation: https://laravel.com/docs/9.x/eloquent-relationships
Then in your query,
return Product::whereHas('noveltys', function(Builder $query) {
// your where clause
})
->orWhereHas('sales', function(Builder $query) {
// your where clause
})
->orWhereHas('liquidations', function(Builder $query) {
// your where clause
})
->orWhereHas('exposures', function(Builder $query) {
// your where clause
})
->get();
I want to get data from table role_users , column role_id . For the moment, I have this in my controller :
$data['contact_users'] = DB::table('contacts')
->join('users' , 'users.id', '=', 'contacts.contact_id')
->join('industries' , 'industries.id', '=', 'users.industry_id')
->join('countries' , 'countries.id', '=', 'users.country_id')
->join('organization_types' , 'organization_types.id', '=', 'users.organization_type_id')
->select('users.*','industries.industry','countries.country','organization_types.organization_type')
->where('contacts.contact_id','!=',$id)
->where('users.deleted_at','=',NULL)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I'm using it in view with this code :
{{$contact->industry_id}} or {{$contact->country_id}}
I need to use something like this.
{{$contact->role_id}}
which is working for every user.But I need to get data from role_users,column role_id.I don't know how to use ->join() and I need it so much.Thank you.
I added one extra join and one select field at the end,
$data['contact_users'] = DB::table('contacts')
->join('users', 'users.id', '=', 'contacts.contact_id')
->join('industries', 'industries.id', '=', 'users.industry_id')
->join('countries', 'countries.id', '=', 'users.country_id')
->join('organization_types', 'organization_types.id', '=', 'users.organization_type_id')
->join("role_users", "role_users.user_id","=","users.id")
->select('users.*', 'industries.industry', 'countries.country', 'organization_types.organization_type', "role_users.role_id")
->where('contacts.contact_id', '!=', $id)
->where('users.deleted_at', '=', null)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I have the following that joins together 3 tables, Images, Users, Profiles. The problem I have is that the result of the following only gives me 1 id field and that is the Id of the Profiles table. All 3 tables have their own Id fields.
Can I seperate it so that it is images.id, profiles.id and users.id?
$images = \App\Image::where('image_approved', '=' ,"feature_approved")
->join('users', 'users.id', '=', 'images.user_id')
->join('profiles', 'profiles.user_id', '=', 'images.user_id')
->get();
You can alias them like this:
$images = \App\Image::select([
'users.id as user_id',
'profiles.id as profile_id',
'images.id as image_id',
//... add the rest of the columns that you want to select here.
])
->where('image_approved', '=' ,"feature_approved")
->join('users', 'users.id', '=', 'images.user_id')
->join('profiles', 'profiles.user_id', '=', 'images.user_id')
->get();
or you can simplify it by using Eloquent relationships.
So in your Image model you would have:
public function user()
{
return $this->belongsTo(User::class);
}
public function profile()
{
return $this->hasOneThrough(Profile::class, User::class);
}
Then you can retrieve them:
$images = Image::with('user', 'profile')
->where('image_approved', '=' ,"feature_approved")
->get();
// now each $image in $images will have user and profile relationship
// $image->id
// $image->user->id
// $image->profile->id
Use alias in select like this:
->join('profiles', 'profiles.user_id', '=', 'images.user_id')
->select('images.*', 'users.*', 'profiles.*', 'images.id as imageID', 'profiles.id as profileId', 'users.id as usersId')
->get();
What I am trying to achieve is to allow teachers to import a student into different classes.
Note: A student can be multiple classes.
The problem is that when I show the list of students in a select dropdown it should return all students except for students that are not in this class (the class being the page that I am on, app.com/classes/5 for example).
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'asc')
->get();
This works and shows all students that are not in this specific class BUT if a student that's in this class and another class their name appears in the list and as duplicate names.
What can I do?
When MySQL's only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY
You should try to select fields from schema on which you can apply group by instead of select *.
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->select('users.id', 'other fields you used')
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
Not IN is also useful in your case
User::select('fields you used')
->role('student')
->whereNotIn('id', DB::table('group_user')->where('group_id', $id)->pluck('user_id')) // $id = 5
->orderBy('name', 'ASC')
->get();
Modify your query to use distinct() like so;
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->distinct()
->get();
You could also groupBy('users.id')
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
I have two tables, one with users, one with the name of their document. The first table consists of two columns: id and username. The second one consists of three columns: id, userid and document_name.
Now, I'm trying to create a query in the controller. What should happen, ideally, is that if someone visits website.com/{documentname}, it displays the username of the owner. Also, this should only happen if the current logged in user is the owner of the document. However, this is proving more difficult than I imagined. As in, I can't see what I'm doing wrong.
Here's the query:
$user = DB::table('documents')
->join('users', function($join)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
**Try this query :**
$user = DB::table('documents')
->leftJoin('users', 'users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
->get();
$document_name isn't in scope for the join function: you need to pass it through to the closure via use
$user = DB::table('documents')
->join('users', function($join) use ($document_name)
{
$join->on('users.id', '=', 'documents.userid')
->where('documents.userid', '=', Auth::id())
->where('documents.document_name', '=', $document_name);
})
->get();
EDIT
Because the WHERE conditions apply to the base table, and not to the JOIN:
$user = DB::table('documents')
->join('users', function($join) {
$join->on('users.id', '=', 'documents.userid')
})
->where('userid', '=', Auth::id())
->where('document_name', '=', $document_name);
->get();