Laravel with relation has record then fetch
I am working in laravel project and now I want to search only those users whose are friend of viewing user other wise fetch those users whose are allowed to chat with every one. Below is my working code that I code
public static function hasSearchTagable($searchText = null, $user_id) {
if (empty(trim($searchText))) {
return self::where(function($sql) use($user_id) {
$sql->where('archive', '=', false);
$sql->whereHas('follower_friend', function($sql) use($user_id) {
$sql->where('following_id', '=', $user_id);
});
});
} else {
$searchText = trim(preg_replace('/[^A-Za-z0-9._ -]/', '', strtolower($searchText)));
return self::where(function($sql) use($searchText) {
$sql->where('archive', '=', false);
$sql->where('username', 'LIKE', "%$searchText%")->orWhere('full_name', 'LIKE', "%$searchText%");
})->with(['follower_friend' => function($sql) use($user_id) {
$sql->where('following_id', '=', $user_id);
}])->with(['following_friend' => function($sql) use($user_id) {
$sql->where('follower_id', '=', $user_id);
}]);
}
}
public static function get_tagable_users($login_id, $searchText = null, $not_in = array(), $take = 40, $skip = 0, $is_messageable = false) {
$orderBy = " username DESC ";
if (!empty(trim($searchText))) {
$searchText = preg_replace('/[^A-Za-z0-9-_.]/', '', strtolower(trim($searchText)));
$orderBy = " CASE WHEN username = '$searchText' THEN 0
WHEN username LIKE '$searchText%' THEN 1
WHEN username LIKE '%$searchText%' THEN 2
WHEN username LIKE '%$searchText' THEN 3
WHEN full_name LIKE '$searchText%' THEN 4
WHEN full_name LIKE '%$searchText%' THEN 5
WHEN full_name LIKE '%$searchText' THEN 6
ELSE 7
END, username ASC ";
}
$users = User::hasSearchTagable($searchText, $login_id)->whereNotIn('id', $not_in)
->take($take)->skip($skip)
->orderByRaw($orderBy)
->get(array('id', 'username', 'full_name', 'message_privacy', 'picture'));
return !empty($users) ? $users->toArray() : array();
}
if I search people then it search public and private both users. But if private user is in following or followers list then it should come into search but if not then if this user allowed user to chat tjem it should come.
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-with-relation-has-record-then-fetch/
via Lzo Media
No comments:
Post a Comment