Related
I have a database query that needs filtering further. The id needs to match a number in an array I have. For some reason, I can select where using every column except id. Even something as simple as $query->where('users.id', 1); doesn't work. How can I perform a database query by id?
$portfolio_query_array = UserPortfolio::all()->pluck('user_id')
$query = DB::table('users');
$query->select('users.id as user_id','users.user_slug', 'users.social_id', 'users.social_img_update_status', 'users.avatar', 'users.first_name', 'users.last_name', 'users.bio', 'comments.rating', 'user_portfolios.title', 'user_portfolios.description', 'user_portfolios.filepath');
$query->leftjoin('comments', 'comments.comment_to', '=', 'users.id');
$query->leftjoin('user_portfolios', 'user_portfolios.user_id', '=', 'users.id');
$query->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', '=', 'users.id');
$query->where('users.status', 1);
$query->where('users.user_type', 3);
// The below simple query line doesn't work
$query->where('users.id', 1);
// The lines I do want to work also fail.
foreach ($portfolio_query_array as $user_id){
$query = $query + $query->where('users.id', $user_id);
}
Thank you for your help!
First of all, you should take advantage of Eloquent ORM, there is no need to use raw queries unless you have some performance issues or really complex queries.
Assuming you have 3 models User, UserPortfolio, Comment, and all relationships defined, you could do something as simple as this:
$users = User::with('comments', 'portfolio')
->where('status', 1)
->where('user_type', 3)
->whereHas('portfolio')
->get();
In this way, you are getting all users with portfolios which is what I assume you want.
Anyway, if you insist to use a raw query, there whereIn() method is what you need
$portfolio_query_array = UserPortfolio::pluck('user_id')->toArray();
$query = DB::table('users')
->select(
'users.id as user_id',
'users.user_slug',
'users.social_id',
'users.social_img_update_status',
'users.avatar',
'users.first_name',
'users.last_name',
'users.bio',
'comments.rating',
'user_portfolios.title',
'user_portfolios.description',
'user_portfolios.filepath'
)
->leftjoin('comments', 'comments.comment_to', 'users.id')
->leftjoin('user_portfolios', 'user_portfolios.user_id', 'users.id')
->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
->where('users.status', 1)
->where('users.user_type', 3)
->whereIn('users.id', $portfolio_query_array)
->get();
BTW, why are you doing a leftJoin to user_profile_category_selects if no fields are selected from that table?
In addition, if you are looking for all users that have a portfolio, starting from the whole list of portfolios, wouldn't be better to do a join to portfolios and get only those users having portfolios?
$query = DB::table('users')
->select(
'users.id as user_id',
'users.user_slug',
'users.social_id',
'users.social_img_update_status',
'users.avatar',
'users.first_name',
'users.last_name',
'users.bio',
'comments.rating',
'user_portfolios.title',
'user_portfolios.description',
'user_portfolios.filepath'
)
->leftjoin('comments', 'comments.comment_to', 'users.id')
->join('user_portfolios', 'user_portfolios.user_id', 'users.id')
->leftjoin('user_profile_category_selects', 'user_profile_category_selects.user_id', 'users.id')
->where('users.status', 1)
->where('users.user_type', 3)
->get();
I have 4 tables clinics , locations , services & location_services in which relations are clinicid is present in locations and both serviceid and location id is present in location_services table.
My requirement is that i want to retrive all the cinics and their coreesponding locations and services .
But when i tried it is retrving the results of only one clinic id , i dont know how can i retrive complete list
following is my code
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid','locations.locationName as locname')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->toSql();
dd($locationservices);
die();
Please help me to solve this
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
I need to get specific fields from the database and then paginate the results but I am not sure where to place ->paginate().
My code is as below:
Upload::where('sponsor_id', Request::segment(2))
//->paginate(20)
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->get([
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
])
I'm getting the correct results without paginating but I would like to paginate the results.
First, you need to use select then pass in pagination.
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select('uploads.filename','users.first_name','users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at')
->pagiate(10);
You can use select method for filtering columns. And then put paginate method end of your query.
Finaly your query will be like this:
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select(
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
)
->paginate(20);
im trying to fetch multiple Id's from one array for then using it on a query relationship.
This is what I tried:
$following = $user->following;
$followingId = $following->id;
I get this error:
'Undefined property: Illuminate\Database\Eloquent\Collection::$id'
If I log $following, I get the following array, as you can see I'm following 4 different users and I need to get those id's:
local.INFO: [{"id":32,"email":"test#mail.com","username":"test#mail.com","photo":"97dfebf4098c0f5c16bca61e2b76c373","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":1,"pivot":{"follower_id":1,"followed_id":32}},{"id":69,"email":"fhamada#paginar.net","username":"fhamada#paginar.net","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":69}},{"id":79,"email":"sandrastun#gmail.com","username":"sandrastun#gmail.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":79}},{"id":101,"email":"cristobalalfonzo#outlook.com","username":"cristobalalfonzo#outlook.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":101}}]
Query relationship:
$activities = DB::table('notifications')
->join('users', 'users.id', '=', 'notifications.sender_id')
->join('followers', 'followed_id', '=', 'users.id')
->join('user_details', 'user_details.id', '=', 'notifications.user_id')
->join('winwins', 'winwins.id', '=', 'object_id')
->leftJoin('groups', 'notifications.object_id', '=', 'groups.id')
->where('notifications.sender_id', '=', $user->id)
->orWhere('winwins.id', '=', 'notifications.object_id')
->orWhere('sender_id', '=', $user->id)
->orWhere('followers.followed_id', '=', $followingId)
->orderBy('sent_at', 'desc')
->skip($page * $amount)
->take($amount)
To fetch multiple columns from a collection (e.g. multiple IDs from your following collection), use this:
$followingId = $following->pluck('id')->toArray();
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();