How do I fix this error?
1/1
BadMethodCallException in Macroable.php line 81:
Method paginate does not exist.
My code:
$tickets = Ticket::all()->paginate(7);
Your query should be as:
$tickets = Ticket::paginate(7);
When you call all() it queries the DB and returns all the tickets and then you are calling the paginate() method on the \Illuminate\Database\Eloquent\Collection which will through the error.
Check out the docs for more info.
If you want to get all records from the database pertaining to the tickets in a single request
$tickets = Ticket::all();
And if you want paginated results then use
$tickets = Ticket::paginate(7); //replace 7 with the number of records you want to retrieve in one set.
Related
I am fetching a list of data using Eloquent method and trying to reverse the order of array, I am getting an error.
Here is the code :(Previous Code)
$temp = Product::paginate(20);
New Code
$temp = Product::paginate(20)->orderBy('id','desc');
Error :
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
New method :
$temp = Product::paginate(20)->sortDesc();
Error :
Method Illuminate\Database\Eloquent\Collection::appends does not exist.
Can anyone help me?
use below syntax
Product::orderBy('id','desc')->paginate(20);
try to write it in this way,
use orderBy before the paginate
$temp = Product::orderBy('id','desc')->paginate(20);
->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Product::latest()->paginate(20)
When i try to use group_by function select query not working, and i am getting error Call to a member function result(), without group by query is working, i can't fix this issue ? can anyone please help me to resolve this issue ? here i have added my whole query
$query = $this->db->select('s.*,b.*,c.*,i.*')
->from('sales s')
->join('biller b','s.biller_id=b.biller_id','inner')
->join('customer c','s.customer_id=c.customer_id','inner')
->join('invoice i ','s.sales_id=i.sales_id','inner')
->group_by('sales_id');
$data = $query->get()->result();
return $data;
My guess is the sales_id column name on this line is ambiguous :
->group_by('sales_id');
So you could change it either using sales.sales_id or invoice.sales_id :
->group_by('s.sales_id');
Laravel 5.5 show me the error message:
Call to undefined method stdClass::update()
for the following codes in the Model
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)->first()
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
Try using ->limit(1) instead of ->first() in the chain.
In Laravel if you wish to update only one record then you can use ->first() method of Eloquent ORM to get eloquent object and then you can simply update the object by ->save().
Laravel query builder directly not supporting update only record as Update with limit of 1 is not directly supported by mysql MySQL - UPDATE query with LIMIT
In the example you have given here is some modification to be work,
DB::table('adminUserLogs')
->where('adminUserId', $id)
->where('adminUserOutTime', NULL)
->update(['adminUserOutTime' => \Carbon\Carbon::now()]);
This will result in updating the records where specified conditions. To update specific record pass id column in where clause of that particular record.
I am using rtconner/laravel-tagging package to get tags functionality to my app.
I can count attached tags by $o->tags->count()
I can loop the tags by a foreach: #foreach($o->tags as $t).
print attached tags by
the problem
Now I want to get a collection of random 10 Quotation with no tags attached.
While I can print a random 10 pieces with a given attribute:
$object = Quotation::where('deepness', null)->get()->random(10);
(Note: I have a random scope defined in the model, irrelevant for my issue)
... but this code, cloned from another model doesn't work:
$object = Quotation::whereHas('tags','>',0)->get()->random(10);
It produces this error message:
FatalThrowableError in Builder.php line 880:
Type error: Argument 2 passed to Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure, string given
I have also tried to execute this query
$object = Quotation::has('tags')->get()->random(10);
but I got this:
```
BadMethodCallException in Builder.php line 2431:
Call to undefined method Illuminate\Database\Query\Builder::tags()
```
Note 2: In the source model (the one I cloned from) the relation was counting a hasMany relation.
to do
Please help me to create the collection of Quotations with no tags assigned
Had the same problem and solved it this way:
$objects = Quotation::all();
$objects = $objects->filter(
function ($object, $key) {
return $object->tags->count() > 0;
}
)->random(10);
Hope issues still relevant :)
In my application controller, I have a method transaction_reimburse() that receives ids as POST string values. The method's function is to get all transactions that match the ids from the database, and update them accordingly.
Controller method:
public function transaction_reimburse() {
$reimburse = array('reimburse' => 1);
$transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
$this->Transaction_model->reimburse_these($transactions, $reimburse);
}
Model method:
function get_these_ids($ids) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $ids .")");
return $this->db->get();
}
Model method:
function reimburse_these($transactions, $reimburse) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $transactions .")");
$this->db->update($this->tbl_transaction, $reimburse);
}
However, I am getting the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330
What I need to know is why do I get these errors?
The errors mean that your $ids variable is not in a string format (meaning: 1,2,3,4. Its probably an array). To fix it, first find out what format it is on by using print_r($ids) or var_dump($ids). Once you know the format, use PHP functions(such as implode()) to transform it into strings.
IN is looking for a set in the format e.g. (1,2,3,4).
Either make your post values adopt the format 1,2,3,4 so that when the value is included into the query it's valid or if you're sending an array use a method in PHP such as implode(',',$array) to create the desired format for binding into the query..
I have reviewed your both function and found that in both function you put tbl_transactions.id in where clause and ids is passed from post and hence there is no need to call get_these_ids to get ids, you already have ids in post array and hence instead of transactions variable, just use $this->input->post('ids') for reimburse_these functions.
if you surely want to get tbl_transactions from get_these_ids function then you need to use group_concat with group by in query of get_these_ids function and just return that concated id in get_these_ids function then it will work.