Limit in ignited datatable - mysql

$this->db->select('employee_name');
$this->db->from('employee');
$this->db->limit(1,0);
$query = $this->db->get();
return $query->result();
How to write this query in datatable. I wrote query as below.
$this->datatable->select('employee_name')
->from('employee');
$this->datatable->limit(1,0);
return $this->datatables->generate();
But it shows error in limit.

I think you used codeigniter method chaining along the general query,try any of these
By Method Chaining
$this->datatables->select('employee_name')
->from('employee')
->limit(1);
return $this->datatables->generate();
another way
$this->datatables->select('employee_name');
$this->datatables->from('employee');
$this->datatables->limit(1);
return $this->datatables->generate();

Related

Need suggestion in query builder of Laravel 9 application

How to avoid foreach-loop because Laravel query builder is used to get a single record detail.
When I tried without foreach-loop, the following error occurred "Property [name] does not exist on this collection instance".
Controller
public function show(Student $student)
{
$result = DB::table('students')
->select('*')
->where('id', "=" ,$student['id'])
->get();
foreach($result as $studentData){}
return view('student.show', ['studentData' => $studentData]);
}
Blade View
{{ $studentData->name}}
->get() always returns a Collection, even if there's only one match. If you only want the first record, you can use ->first() instead.
Do you really have to use raw queries? I assume Student is a Laravel Model, in that case, you should be able to use Student::find($id) to get the model directly.

Laravel "->where(like) issue"

I have this code
$users = array();
$users_to_add = User::all()->where('unit', 'LIKE', '%'.$request->unit.'%');
But i get nothing on $users_to_add. The like doesn't work. What am i missing?
all() actually runs a query to get all users. Therefore, your where() call is on the Collection (PHP), not the Query Builder (SQL).
You'll want to use get() but make sure you declare your query conditions prior to calling it.
get() will execute the query, with your defined conditions, and return a collection of results.
User::where('unit', 'LIKE', '%'.$request->unit.'%')->get();

Laravel Select Query Accessing Returned Data

I've been working with Laravel for a short time, and am confused about accessing data retrieved from queries. I'm trying to save data to variable but am getting the following:
Trying to get property of non-object
Queries tried:
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->get();
// also tried
$data = DB::table('table_1')->where('email', '=', Input::get('email_address'))->pluck('user_id');
// accessing data
$userID = $data->user_id;
Both return the same.
To elaborate a little bit, ->get() will return a Collection. You might iterate over it as an array, but you can profit from methods that this class offers. pluck is one of these.
That's why $userID = $data->user_id; wouldn't work.
first() did the trick, rather than get().
$data = DB::table('table_1')->select('user_id', 'email')->where('email', '=', Input::get('email_address'))->first();

Group By cause non object

I'm using codeigniter and I'm trying to perform a query execution passing a where clause, this where is an array, this is my code:
$now = date('Y-m-d H:i:s');
$this->record_model->getAllRecord(array('guid' => $guid,
'end_datetime <' => $now,
'GROUP BY hash'));
this is the content of getAllRecord
$query = $this->db
->select('*')
->from('appointments')
->where($where_clause) 'is passed as parameter..
->get()->result_array();
this is the content of where
array(3) {
["guid"]=>
string(36) "c3682d6f-75b2-4686-966d-cb3c9344369b"
["end_datetime <"]=>
string(19) "2016-01-21 12:03:58"
[0]=>
string(13) "GROUP BY hash"
}
Now if I remove the Group by all working well, but if I insert the Group By hash for don't have (duplicate) in the result this error appear:
Call to a member function result_array() on a non-object
Essentially I want get the information of all records but some records have the same hash, so the GROUP BY should join this record in one. But I did something wrong...
Group by is not part of the where clause, therefore codeigniter will generate a syntactically incorrect code, therefore ->get() call does generate a proper resultset. You should add the group by clause using a separate ->group_by(...) call to your query.
Try this
$query = $this->db->select('*')
->where($where_clause)
->get('appointments');
$result = $query->result_array();
return $result;

What is wrong with this Code Igniter mySQL query?

I have two tables for storing information about a user. One is for authentication, the other is information the user will enter themselves. I am writing a model that will be used when the user interacts with this information. The following method is to return data for display and modification.
I need a query that will return 'email' and 'username' from $accounts_table and * from $profiles_table. I can't seem to get my head around the JOIN syntax though. I understand how joins work, but my queries throw sentax errors.
function get_userdata($id){
$data = array();
$this->db->get_where($this->profiles_table, array('user_id' => $id));
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data= $this->db->get();
return $data;
}
I see a couple of issues:
You should be using $this->db->where(), instead of $this->db->get_where(). get_where() executes the query immediately.
$this->db->get_where('user_id', $id);
Also the first argument of $this->db->join() should only be the table name, excluding the field.
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
And you're returning $data which is just an empty array(). You would need to pass the query results to $data like this:
$data = $record->result_array();
get_where executes the query. So, your join is its own query, which doesn't work.
You need to break get_where into where and from.
Also, in MySQL, you JOIN a table, not a field. If you want that field, add it to the SELECT.
$this->db->select($this->profiles_table.'.*');
$this->db->select($this->accounts_table.'.email,'.$this->accounts_table.'.username');
$this->db->from($this->profiles_table);
$this->db->where('user_id', $id);
$this->db->join($this->accounts_table, $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
$data = $this->db->get();
NOTE: $this->db->get() returns a query object, you need to use result or row to get the data.
I think you've a mistake:
$this->db->join($this->accounts_table.'.email', $this->accounts_table.'.id = '.$this->profiles_table.'.user_id');
First parameter should a table NOT a field: $this->accounts_table.'.email' is wrong IMHO. Or only a typo :)