I want to shuffle and paginate data in laravel.
when I use this code :
`$questions = Question::where("exam_id",$exam->id)->shuffle()->paginate(1);`
gives me this error : BadMethodCallException
Call to undefined method Illuminate\Database\Eloquent\Builder::shuffle()
and when I use this code:
$questions = Question::where("exam_id",$exam->id)->paginate(1)->shuffle();
in dd($questions = Question::where("exam_id",$exam->id)->paginate(1)->shuffle());gives me a collection that has the first item and other items don't exist. And there is also no paging information.And when I don't use dd() gives me this error : Method Illuminate\Database\Eloquent\Collection::links does not exist. While I use $questions->links() in the
view.
When I use this code :
$questions = Question::where("exam_id",$exam->id)->get()->shuffle()->paginate(1);
Gives me this error : Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
And when I use this code :
$questions = Question::where("exam_id",$exam->id)->inRandomOrder()->paginate(1);
It works, but the data is repeated on later pages. For example, the data on the first page may be repeated on page seven or other pages.
In fact, I want the data to be sorted by random And then paginated so that when a data is loaded on one page not repeated on subsequent pages.
please help me!!Thanks
I was facing this same problem and solved it by adding the shuffle()->all() methods to the collection variable in the view file, which seems to work perfectly.
Once you pass the variable to the view to loop over using a foreach I added the following:
$rows->shuffle()->all()
So my foreach loop looks like:
foreach($rows->shuffle()->all() as $row){ ... }
The rows on the front end when loaded using ajax still work as they should with the paginator since the collection is untouched and the selection of the next rows is correctly loaded and then shuffled, meaning there are no duplicates.
Related
I'm trying to paginate my json response in terms of multiples of 20 but for some reason, I'm getting Error: Call to a member function chunk() on array.
To clarify, $results has data in it and $request->get('offset'); has a value. Is there something I'm doing wrong? I'm seeing examples when searching on SO where people are doing this on a collection but for some reason, it isn't working for me. How can I make this work?
Note: If I do dd($results['num_results']);, it displays 360. I feel like I need to use this some how but I'm not sure how. I'm trying to achieve this in the most Laravel way possible. Also note, no database is being used - this is simply a get request on an external API.
Thanks in advance.
collect($results);
$outputs = $results->chunk(20)[$request->get('offset')];
dd($outputs);
$results is still an array. You are not setting the new Collection you are creating to a variable: collect($results). This does not change $results. So you are calling chunk on the array, $results. Call chunk on the Collection instead:
$collection = collect($results);
$outputs = $collection->chunk(20)...;
I am trying to list database details using where condition with parameter after calling this function CSS not working but all data is list down properly and without parameters, CSS working properly.
web.php
Route::get('/viwelist/{id}','Front\SISProfileController#check');
controller
class SISProfileController extends Controller
{
public function check($district){
$list = SISAccount::all()->where('District', '==', $district);
//dd($list->all());
return view('Front.listSIS', compact('list'));
}
}
link
click
What you are doing is getting all the elements from the database and the checking that condition through Collection::where method, and this will cause a lot of problem if in the database there're a lot of data. Instead you should use the where method directly on the database, and than get back only those records, so you should do something like:
$list = SISAccount::where('District', '=', $district)->get(); //to get back a Collection with all the records
$list = SISAccount::where('District', '=', $district)->first(); //to get only the first record, like if District is your primary key
The css problem is probably caused by the fact that you have put a relative link on your header as position of the css file, instead you should have something like /asset/css.css, so with a / at the beginning
I make two arrays (A1 & A2) in server side by result of ->paginate(20) and send theme to index blade via two variables. depended on contents some times all the first 20 posts is based of A1 array, so page returns error Undefined variable: A2 .
Even while A2 is completely null (and not only because of pagination), I want to handle it in blades to only returns empty and not errors.
I tried #if($A2) , #if(!is_null($A2)) , #empty checks on blade but still same error occurs.
Also i make $A2=""; on sever side before operations and then page returns Trying to get property of non-object .
It sounds like your blade page has multiple places where the $A2 variable is being called. You can try to go through and find all of them and preceed them with an if check such as #if(isset($A2) { do something with $A2 }
But, the easier approach, and perhaps better for readibilty and future code might be to initialise the variable to whatever type of collection it is on your controller. You had the right idea with $A2="";, but that is a string, and your code on the blade page is looking for an object (you probably have something like $A2->field called).
Here is a simplistic example - you can clean this up, but hopefully it makes it easy to understand. On your Controller something like this:
$A2 = MyModel::find($someId);
if(!isset($A2)){
$A2 = new MyModel();
}
Then make sure to send through to your blade page as at least an initialised model object.
return view('page.pages', compact('A2'));
I'm writing an Omeka Plugin and wants to get the list of all public Items with their all elements, in a controller under my plugin.
I've tried get_items() but the function doesn't exists, it looks like the function is only available for the views - not sure how.
another try was to manually fetch the records from database, but that's not a standard way.
So, the question is, is there a predefined function/class or way to get all the items in a controller?
I'm not sure if there is a function that will get you the items along with all of their element texts, but if you want a list of items, inside the controller you should be able to make a call like:
$items = $this->_helper->db->getTable('Item')->findAll();
The Omeka docs warn against getting all the items at once because it could be memory intensive. So, alternatively, you can loop through items.
$items = $this->_helper->db->getTable('Item');
$item = $items->findFirst();
while($item != NULL){
// Do something
$item = $items->findNext($item);
}
There is a "public" property on an item that tells you if it's public. In order to get the element texts for an item, I think you'd have to make a query on the ElementText table.
For more information, see the Omeka read the docs page for Table_Item, Omeka_Db_Table and Item:
http://omeka.readthedocs.io/en/latest/Reference/libraries/Omeka/Db/Table.html
http://omeka.readthedocs.io/en/latest/Reference/models/Table/Item.html
http://omeka.readthedocs.io/en/latest/Reference/models/Item.html
I am trying to pass data from my controller to my view, but am getting an undefined variable error. usersID is a column in my MySQL table.
Here is the code in my controller
$arrayWithCount = DB :: table("users_has_activities")
-> where("usersID", "=", 19)
-> pluck("usersID");
$countNumber = sizeof($arrayWithCount);
return view('pages.progress', ['countNumber' => $countNumber]);
I have also tried the following return statement without any success
return view::make('pages.progress') -> with('countNumber', $countNumber);
I have also tried reversing the puck and where clauses without any success, I didn't have high hopes that reversing them would fix the problem but thought I would try it any way. Below is the relevant code in the blade file.
<?php echo $countNumber; ?>
This is the error I am currently getting
Undefined variable: countNumber
You code looks fine, if dd() doesn't stop execution of the controller, then another controller is executing. So double check your routes and controllers.
First sizeof should be sizeOf, and is simply an alias for count(). Most people would prefer count over sizeOf as sizeOf (in many languages) would indicate something related to size on disk.
Anywho, being that pluck returns a collection, you have access to count() directly from the collection.
You can probably simply do something like:
$countNumber = $arrayWithCount->count();
Sidenote: Unless there is a particular reason why you are using <?php ?>, in blade, it would be preferred to use {{ and }}.
I had all the controller code in a method I wasn't calling, so the variable was never passed to the blade file. The method was set up to be called on a button press, after fixing that everything works. Thanks Alexey for the help.