How can I do a self-join in Laravel? - php

Here is my code:
$role_id = Auth::user()->role_id;
$related = Page::where('path', $request->path())->where('method', $_SERVER["REQUEST_METHOD"])->first()->related;
$pages = Page::where('related', $related)->get();
foreach ($pages as $page){
$accessGiven = page_role::where('role_id', $role_id)->where('page_id', $page->id)->first();
if ( sizeof($accessGiven) > 0 ) {
return $next($request);
}
}
return redirect('/');
It works well logically, but it is a little slow for huge dataset. You know, it's actually a middleware and will be executed before most of requests.
Anyway, I guess I can combine line 2 and line 3 and make one query of them. Any idea how can I do that?

Try this:
In your Page model:
public function relatedPages(){
return $this->hasMany(self::class, 'related', 'related');
}
then
$pages = Page::where('path', $request->path())->where('method', $_SERVER["REQUEST_METHOD"])->first()->relatedPages

Look in this example (I didn't test it):
$requestPath = $request->path();
$serverMethod = $_SERVER["REQUEST_METHOD"];
$pages = Page::where('related', function($query) use($requestPath,$serverMethod) {
$query->where('path', $requestPath )->where('method',
$serverMethod)->first()->related; }
)->get()

Related

OctoberCMS Pagination

I use octobercms and User Extended plugin(Clacke). I try to render a pagination because for now i have a lot of registered users and they display on one page.
I use random users function from \classes\UserManager.php
public static function getRandomUserSet($limit = 7)
{
$returner = new Collection;
$userCount = User::all()->count();
if(!isset($userCount) || empty($userCount) || $userCount == 0)
return [];
if($userCount < $limit)
$limit = $userCount;
$users = User::all(); //paginate(5)
if(empty($users))
return $returner;
$users->random($limit);
$friends = FriendsManager::getAllFriends();
foreach($users as $user)
{
$userAdd = true;
if(!$friends->isEmpty())
{
foreach($friends as $friend)
{
if($user->id == $friend->id)
{
$userAdd = false;
break;
}
}
}
if($user->id == UserUtil::getLoggedInUser()->id)
$userAdd = false;
if($userAdd)
{
$returner->push($user);
}
}
return $returner->shuffle();
}
try to do this with changing return $returner->paginate(25); and $users = User::paginate(25); but throws me an error
An exception has been thrown during the rendering of a template
("Method paginate does not exist.").
After that i try to change directly in \components\User.php
public function randomUsers()
{
return UserManager::getRandomUserSet($this->property('maxItems'))->paginate(12);
}
But again the same error.
Tryed and with this code and render in default.htm {{ tests.render|raw }}
public function randomUsers()
{
$test = UserManager::getRandomUserSet($this->property('maxItems'));
return $test->paginate(10);
}
Again with no success. Could anyoune give me some navigation and help to fix this?
If you are using random users function from \classes\UserManager.php
I checked the code and found that its using Illuminate\Support\Collection Object. So, for that Collection Object pagination works differently
You need to use forPage method.
On the other hands paginate is method of Illuminate\Database\Eloquent\Collection <- so both collection are not same
Use forpage
// OLD return UserManager::getRandomUserSet($this->property('maxItems'))
// ->paginate(12);
TO
return UserManager::getRandomUserSet($this->property('maxItems'))
->forPage(1, 12);
forPage method works like forPage(<<PAGE_NO>>, <<NO_OF_ITEM_PER_PAGE>>);
so if you use forPage it will work fine.
if any doubt please comment.

helper vs controller performace in laravel 5.*

Lets assume I have a helper called engine.
if( ! function_exists('engine') )
{
function engine($user_id_1, $user_id_2, $league, $log = true)
{
/*
* variables
*/
$questionsLevel = 1;
$user1 = \App\User::where('id', $user_id_1)->first();
$user2 = \App\User::where('id', $user_id_2)->first();
$league = \App\Models\League::where('id', $league)->first();
$users = $league->all_users;
/*
* check if users joined to league correctly
*/
$user1_ok = $user2_ok = false;
foreach($users as $user)
{
if( $user->id == $user_id_1 )
{
$user1_ok = true;
}
if( $user->id == $user_id_2)
{
$user2_ok = true;
}
$check_users = [
$user1_ok,
$user2_ok
];
}
if( in_array(false, $check_users) )
{
return [
'errcode' => 404,
'errmessage' => 'one ro both user/s did not joined to league'
];
}
//DO SOME STUFF
}//function engine
}
As you know, I can write a controller to do same.
Does anyone know Which is faster based on Laravel architecture? and how can I test performance in both cases?
I'm using laravel 5.7.*
Fastest would be a solution that does not load unnecessary data:
if(! function_exists('engine'))
{
function engine(int $user1, int $user2, int $league)
{
return League::query()
->where('id', $league)
->whereHas('users', function($query) use ($user1) {
$query->where('id', $user1);
})
->whereHas('users', function($query) use ($user2) {
$query->where('id', $user2);
})
->exists();
}
}
In general, I don't think such a function should return complex results like an error message though. For this particular job, you should use a controller action. The helper method should only return true or false in my opinion. You could also place the same logic in a service class though (probably the best solution).
About the performance, I do not think there will be any changes as same query will be executed as well as same number of operation will be done, for example the foreach() loop it is O(n) in both the cases, so what will be difference, but you may like to change your code to something like below or may use ternary operator also. The difference will be in organization of codes. In controller the testing will be easier.
Note: But how id will be both $user1 and $user2, so let's make it OR in stead of AND
foreach($users as $user)
{
if( $user->id == $user_id_1 )
{
$user1_ok = true;
}elseif( $user->id == $user_id_2)
{
$user2_ok = true;
}
$check_users = [
$user1_ok,
$user2_ok
];
}
to
foreach($users as $user)
{
if( in_array($user->id, [$user_id_1, $user_id_2])
{
$user1_ok = true;
}
$check_users = [
$user1_ok,
$user2_ok
];
}
or
foreach($users as $user)
{
if( $user->id == $user_id_1 )
{
$check_users['user1_ok'] = $user->id == $user_id_1 ? true : false;
}
if( $user->id == $user_id_2)
{
$check_users['user2_ok'] = true;
}
}
If you want to get enginees with user1_ok, user2_ok, you may like to run a query as #Namoshek has suggested.
or you may like to change the query to something like
League::query()->where('id', $league)
->whereHas('users', function($query) use ($user1, $user2) {
$query->where('id', $user1)->where('id', $user2);
})->exists();
but, how id will be both $user1 and $user2?so, if it is OR instead of AND.
League::query()->where('id', $league)
->whereHas('users', function($query) use ($user1, $user2) {
$query->whereIn('id', [$user1, $user2]);
})->exists();
or even $users = [$user1, $user2]; and
League::query()->where('id', $league)
->whereHas('users', function($query) use ($users) {
$query->whereIn('id', $users);
})->exists();
By the way, if you want this using these query, you just may put it in Engine model and user or reuse it when ever required. Also for helper, it can be reusable where as in controller it is not.

How to open specific page of paginate?

I get comments paginated. And I need to check, if I have the id of a comment, then open the page that comment is in. Here is my code:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
if ( isset( $cmnt_id ) ){
// I need to get all comments paginated but start from the page that $cmnt_id is in it
} else {
// starts from page 1
$comments = Cmnt_tb::paginate(10);
}
return View('show_cmnts', compact('comments'));
}
How can I do that? any idea?
I think I have to make a new URL and redirect to it.
Maybe there is a better solution, but I guess something like this will work for you:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
$perPage = 10;
if (isset($cmnt_id)){
// Get row number.
$rowNumber = Cmnt_tb::where('id', '<=', $cmnt_id)->count();
// Get page.
$page = ceil($rowNumber / $perPage);
return redirect('show-comments-url?page='.$page);
} else {
$comments = Cmnt_tb::paginate($perPage);
return view('show_cmnts', compact('comments'));
}
}

How to use raw sql Pagination in Laravel5?

This is my Controller code:
$sql = "SELECT *,earth_distance(ll_to_earth(team.lat, team.lng), ll_to_earth(23.1215939329,113.3096030895)) AS distance FROM team where earth_box(ll_to_earth(23.1215939329,113.3096030895),1000) #> ll_to_earth(team.lat, team.lng); ";
$result = DB::select( \DB::raw( $sql ) );
How can I add pagination to this code to build my restful api?
iOS or android will send the "next page" parameter, how to use it and find the next section data?
As far as I know you can't paginate raw query, here's why:
$result = DB::select($sql);
$result here will have the array type and paginate() is the method from the Illuminate\Database\Query\Builder class.
Your case can be performed this way:
$items = DB::table('team')
->selectRaw('SELECT *,earth_distance(ll_to_earth(team.lat, team.lng), ll_to_earth(23.1215939329,113.3096030895)) AS distance')
->whereRaw('earth_box(ll_to_earth(23.1215939329,113.3096030895),1000) #> ll_to_earth(team.lat, team.lng)')
->paginate(10);
foreach($items as $item) {
echo $item->distance;
}
As you can see minimal effort is needed here to separate raw query to selectRaw() and whereRaw() methods.
Another option if you are trying to paginate dynamic columns that maybe you were processing calculations on for reporting is to create a sort method and pass in your array and params:
public function sort($array_of_objects, $sort_by=null, $order, $page)
{
$collection = collect($array_of_objects);
if ($sort_by)
{
if ($order=='desc') {
$sorted = $collection->sortBy(function($role) use ($sort_by)
{
return $role->{$sort_by};
})->reverse();
} else if ($order=='asc') {
$sorted = $collection->sortBy(function($role) use ($sort_by)
{
return $role->{$sort_by};
});
}
} else {
$sorted = $collection;
}
$num_per_page = 20;
if (!$page) {
$page = 1;
}
$offset = ( $page - 1) * $num_per_page;
$sorted = $sorted->splice($offset, $num_per_page);
return new Paginator($sorted, count($array_of_objects), $num_per_page, $page);
}

If statement inside elseif statement causing error

Okay, so I used to have this code and it worked fine:
$lastpost = ForumPos::where('user_id', '=', Auth::id())->orderby('created_at', 'desc')->first();
if ($validator->fails())
{
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
}
elseif ($lastpost->created_at->diffInSeconds() < 15)
{
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
else
{
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
}
and this worked fine, until I noticed a little problem so I had to change this a bit to get this:
$hasposted = ForumPos::where('user_id', '=', Auth::id())->count();
if ($validator->fails()){
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
} elseif ($hasposted != 0) {
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
} else {
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
}
Now when I post a thread and get to the if statement inside the elseif statement, I hit a roadblock. I get the following error:
I only get this error when I haven't specified the title variable in the controller so the view gets it, however there shouldn't be a view. Any ideas? :S
Take a look at your elseif block (second condition)
if(...)
{
//first condition
return ...;
}
elseif ($hasposted != 0) {
{
//second condition
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
}
else
{
//third condition
return ...;
}
When your nested if statement fails
$last_post->created_at->diffInSeconds() < 15
this block finishes, and the rest of the conditional finishes without issuing a Redirect. That is, your nested if statement knows nothing about the third conditional. PHP/Laravel are doing what you told it to -- so tell it to do something else.
This is purely a style suggestion, but I've reached a point where I avoid multiple branch conditionals whenever possible, especially when returning from inside a branch. A style more like
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
if(...)
{
return Redirect(); //...
}
might look longer on the page, but it's much clearer what's going on.
If this? Do something and go away (`return`)
Still here? Well if this-other-thing then do something and go away (`return`)
**Still** here? Well if this-other-thing then do something and go away (`return`)
You end up thinking in a series of yes/no tests, and avoid the very human/programmer problem you ran into with nested conditional logic.
In all your other conditions you do a redirect. If the elseif succeeds, but the if does not succeed then you do nothing. It is then trying to render a page using your master template but you have not set any of the variables that it needs. You could fix this by adding another redirect:
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
else
{
return Redirect::to('/somewhere/else/');
}
After discussing this in the Laravel IRC room, we found the solution (and I believe answers here would have sufficed too)
In the end, I came up with this:
$hasposted = ForumPos::where('user_id', '=', Auth::id())->count();
if ($validator->fails()){
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors($validator->messages());
} elseif ($hasposted != 0) {
$last_post = ForumPos::where('user_id', '=', Auth::id())->orderBy('created_at', 'DESC')->first();
if ($last_post->created_at->diffInSeconds() < 15) {
return Redirect::to('/forum/topic/'.$id.'/new')
->withErrors('You really need to slow down with your posting ;)');
}
}
$new_thread = new ForumThr;
$new_thread->topic = $id;
$new_thread->user_id = Auth::id();
$new_thread->title = Input::get('title');
$new_thread->save();
$new_post = new ForumPos;
$new_post->thread = $new_thread->id;
$new_post->user_id = Auth::id();
$new_post->body = Input::get('body');
$new_post->save();
return Redirect::to('/forum/thread/'.$new_thread->id.'');
If it passes all the if statements, it'll get through to the final request and now I'm happy to say it all works as planned. Thanks, lads!

Categories