I am trying to display all users weather I send him friend request or not with friendship status for that. First of all I load all friend requests and received request in that manner.
$users2 = User::with(['sentRequests' => function ($query) {
...
}])->with(['recievedRequests' => function ($query) {
...
}])->where('users.id', Auth::id())->get();
But then I also want to load other users so for that I create some query like that
$sent = Friendship::where('user_id', Auth::id())
->pluck('friend_id');
$received = Friendship::where('friend_id', Auth::id())
->pluck('user_id');
$users3 = User::whereNotIn('id', $sent)
->whereNotIn('id', $received)
->where('id', '!=', Auth::id())
->get();
Now I want to append $users3 to $users2 but when I done like that
$users2->put('otherUsers',$users3);
It appended but not in the right manner , I want to append it in same manner as 'sentRequests' and 'recievedRequests' are.
Related
I am new to laravel,
I need to create a query for the db,
$query = Deal::query();
I want to use the wherehas operator.
this is my code that is worked.
else if ($group_by == 'precedence') {
if($get_deals_for == 'noprecedence'){
$get_deals_for = 0;
}
$precedenceStatus = $get_deals_for;
$query-> where('precedence', '=', $precedenceStatus);
// \Log::info('The request precedence: '.$precedenceStatus);
}
I want to add this code also to the query
if($person) {
$query->whereHas('personnel', function ($subQuery) use ($person) {
$subQuery->where('id', '=', $person);
});
}
So I need to change the first code?
how I can convert the first code to wherehas?
the first code is from table called deal, the second section is from realtionship called personnel.
the second section worked in other places in the code, I just need to fix the first section and not understand what to write in the use
I try this and get error on the last }
else if ($group_by == 'precedence') {
if($get_deals_for == 'noprecedence'){
$get_deals_for = 0;
}
$precedenceStatus = $get_deals_for;
$query-> where('precedence', '=', $precedenceStatus)
-> when ($person, function($query) use($person) {
$query->whereHas('personnel', function ($query) use ($person) {
$query->where('id', '=', $person);
});
})
}
There is a method you can use called when(, so that you can have cleaner code. The first parameter if true will execute your conditional statement.
https://laravel.com/docs/9.x/queries#conditional-clauses
$result = $query
->where('precedence', '=', $precedenceStatus)
->when($person, function ($query) use ($person) {
$query->whereHas('personnel', fn ($q) => $q->where('id', '=', $person));
})
->get();
You should also be able to clean up your precedence code prior to that using when( to make the entire thing a bit cleaner.
Querying to DB is so easy in laravel you just need to what you want what query you want execute after that you just have to replace it with laravel helpers.Or you can write the raw query if you cant understand which function to use.
using,DB::raw('write your sql query').
Now Most of the time whereHad is used to filter the data of the particular model.
Prefer this link,[Laravel official doc for queries][1] like if you have 1 to m relation ship so u can retrive many object from one part or one part from many object.like i want to filter many comments done by a user,then i will right like this.
$comments = Comment::whereHas('user', function (Builder $query) {
$query->where('content', 'like', 'title%');
})->get();
$comments = Here will be the model which you want to retrive::whereHas('relationship name', function (Builder $query) {
$query->where('content', 'like', 'title%');
})->get();
you can also write whereHas inside whereHas.
[1]: https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
I am developing a page where I want to display unique results from a relation and the entire result.
The entrie result I retreive it as follows:
$media = Media::whereHas('block', function ($query) {
$query->where('identifier', "page");
})->with(["texts" => function ($query) use ($language) {
$query->where("language_id", $language->id);
}])->get();
In the texts relation is a title field where I want to get only the unique results from.
I tried to do another query for it but it didn't work
$media = Media::whereHas('block', function ($query) use ($blockId) {
$query->where('identifier', "page");
})->with(["texts" => function ($query) use ($language) {
$query->where("language_id", $language->id);
}])->distinct("texts.title")->get();
How can I achieve that? and can I do it from the same result ( not another query )
edit:
what I want is a list of unique titles
Relationship queries are performed separately from the main query, any logic in retrieving relationships must go in the with function:
$media = Media::whereHas('block', function ($query) use ($blockId) {
$query->where('identifier', $blockId);
})->with(["mediaTexts" => function ($query) use ($language) {
$query->where("language_id", $language->id)
->select('title')
->distinct();
}])->get();
Here each Media object retrieved should have distinct titles in the mediaTexts relationship. If you want the entire relationship data things will get more complicated.
To get distinct titles of media texts among all Media objects after you've retrieved them you can do:
$media = Media::whereHas('block', function ($query) use ($blockId) {
$query->where('identifier', $blockId);
})->with("mediaTexts")->get();
$titles = $media->pluck('mediaTexts.title')->unique();
I currently have this which works for querying a relationship:
$users = User::query();
$post_id = $request->input('post_id');
$users->whereHas('posts', function ($query) use ($post_id) {
$query->where('id', $post_id);
});
return $users->get();
But in the results of this query I would also like to include Users that do not have any posts connected to them. So the result becomes: users without posts + users with a specific post (code above). Is this possible?
Use doesntHave():
$users->whereHas('posts', function ($query) use ($post_id) {
$query->where('id', $post_id);
})->orDoesntHave('posts');
Good evening everyone, I have some issues, with send mail to:
I have a code in controller, and have data taken from database with joins, this $survey variable shows gives me emails of the survey which belongs to team.
But I need some how to send an email for all emails which I gets from $survey variable. When I put $survey variable inside $message->to($survey); it shows me Undefined variable
my code below , how can I use it. When I put in ->to(); normal one email adress it works, but I need to send same email to all team members
public function startSurvey(Request $req) {
$nameSurvey = $req->input('SurveySelectBox');
$startDate = $req->input('surveyStartDate');
$endDate = $req->input('surveyEndDate');
$data = array(
'updated_at' => Carbon::now(),
'started_at' => $startDate,
'ended_at' => $endDate
);
DB::table('survey')->where('surveyId','=',$nameSurvey)->update($data);
$survey = Survey::where('surveyId' , '=', $nameSurvey)
->join('team','team.teamId', '=', 'survey.teamId')
->join('teammembersall','teammembersall.TeamId', '=', 'team.TeamId')
->join('users','users.id', '=', 'teammembersall.UserId')
->select('users.email')
->get();
Mail::raw('You have new survey to answer: http://localhost:8000/profile', function ($message) {
$message->from('kristijanask#gmail.com', 'New Survey released');
$message->to($survey);
});
return redirect('surveyDrafts');
}
You need to use use ($survey) to use the variable within the Mail anonymous function:
Mail::raw('...text', function ($message) use ($survey) {
$message->from('kristijanask#gmail.com', 'New Survey released');
$message->to($survey);
});
You might also need to use ->pluck() and toArray() on the query result, to make the result an array:
$survey = Survey::where('surveyId' , '=', $nameSurvey)
// long query
->get()
->pluck('email')
->toArray();
I using laravel 5.3 in my project.
I want to get result for duplicate value (i.e.,)If the column has same id it should be displayed.
public function getSendedRequests($broker_id) {
$broker = User::where('broker_id', $broker_id)->lists('id');
//my customer sended request
$getUser = ProfileRequest::whereIn('requested_id', $broker)->lists('requested_id');
$getReq = ProfileRequest::whereIn('requested_id', $broker)->lists('user_id');
$sendUsers = $this->commonRequestQuery($getUser);
$receivedUsers = $this->commonRequestQuery($getReq);
return view('profilerequest')->with(['sendUsers' => $sendUsers->paginate(10), 'receivedUsers' => $receivedUsers->get(), 'title' => trans('messages.customer-sended-requests')]);
}
public function commonRequestQuery($values) {
return DB::table('users')
->leftJoin('basic_informations', 'users.id', '=', 'basic_informations.user_id')
->leftJoin('physical_attributes', 'users.id', '=', 'physical_attributes.user_id')
->leftJoin('religion_and_ethnicity', 'users.id', '=', 'religion_and_ethnicity.user_id')
->leftJoin('occupational_attributes', 'users.id', '=', 'occupational_attributes.user_id')
->leftJoin('personal_interests', 'users.id', '=', 'personal_interests.user_id')
->select('users.id', 'users.user_name', 'users.email', 'users.gender', 'basic_informations.age', 'basic_informations.unique_id', 'basic_informations.mother_tongue', 'religion_and_ethnicity.religion_id', 'religion_and_ethnicity.caste_id', 'occupational_attributes.education_id', 'occupational_attributes.occupation_id')
->whereNotNull('basic_informations.user_id')
->whereNotNull('physical_attributes.user_id')
->whereNotNull('religion_and_ethnicity.user_id')
->whereNotNull('occupational_attributes.user_id')
->whereIn('users.id', $values);
}
I get users id from user table for same broker and check whether they have requested some one in another table.
My problem is if the same user requested for two another users then i cannnot get result twice instead of that i can get only one time but another users(request received users is displayed propery).
Profile request table looks like image
In requested_id table im having same id two times i want to display parallely like Who requested for whom