I'm nearly reaching rage mode!
Can anyone help me with this??
I setted a limit on a CodeIgniter Model so I can only that set of results. Why is it not working? The records are displaying correctly on function call, but I'm getting more than how it's supposed to
$this->CI->dbClient->select('id,title,description,url,date,id_first_image')
->from('posts')
->where('deleted',"0")
->where('date >',$date)
//Neither does work here!
->order_by('date','asc')
->limit(5);
$query = $this->CI->dbClient->get();
return $query->result();
1. Use db
Use db for the Active Record class.
$this->db->select('id, title, description, url, date, id_first_image')
->from('posts')
->where('deleted', "0")
->where('date >', $date)
->order_by('date', 'asc')
->limit(5);
$query = $this->db->get();
return $query->result();
2. $data is Valid
Ensure that $data is a valid MySQL date (or whatever your underlying database technology might be).
Related
Hey guys I have a query that looks like this
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
I need to filter the transaction based on the iso_id that belons to the current user logged in.
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
The iso_id I need to compare to, is inside the merchant table
auth()->user()->isIso() returns the correct iso_id if true or sends false if not
So my first try at this was to use where('merchant.iso_id', '=', auth()->user()->isIso())
But that returns that the column does not exist because for some reason, it's not switching from the transaction model to the merchant one.
I am not sure how to use the stuff inside with() as a selector for my where()
Any help would be appreciated!
Try using whereHas to add the constraint:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();
I'm very confused by this!
In a laravel controller:
$user = Auth::user();
return $user;
returns the entire user object
$user = Auth::user()->id;
return $user;
returns the id of $user as expected
however if I put that exact thing into a query such as:
$user = Auth::user();
$query = Model::where('user_id', $user->id)->get();
return $query;
I get an error that I'm trying to get an 'id' property of a non-object!!!
How can this be possible?
I've also checked 100 times that I'm logged in when testing this.
Editing to add that I've also tried:
$query = Model::where('user_id', 1)->get();
and that works fine
edit to show the function:
$user = Auth::id();
$result = Lesson::where('user_id', $user)
->whereNotNull('notes')
->get();
return response()->json($result, 200);
Expected results: A list of objects with a filled in "notes" column.
Actual results: an empty [] and an error saying can't get id of a non-object
Don't use Auth in Models.
You can set a relation between User and your Model and do query like:
Auth::user()->relation()->get();
This error occurs if the Model doesn't contain any raw with that user id. Make sure you have the raw in the Model where you are querying to get data by user id.
You should try this
$user = Auth::user();
$result = Model::where('user_id', $user['id'])->get();
The error occurs because of you passed property of non-object.
Even after error not solve then restart your xampp or wamp.
If possible, upload the screenshot of error.
Your code should work if you are authed, it seems your are not based on the error message. Since you return json, i assume this is an api request. Api-routes uses another auth driver as default in Laravel (token vs. session). Either change the driver/guard or move your route(s) from routes/api.php to routes/web.php.
See docs for auth
You can change the driver in config/auth.php.
try this
$user_id = Auth::user()->id;
$query = Model::where('user_id', $user_id)->get();
return $query;
I am using codeigniter for my application and i am a bit confused, I wrote some queries like that:
public function checkemail($email) {
$this->db->select('email')->from('user')->where('email', $email);
}
But in the manual of codeigniter ( http://codeigniter.com/user_guide/database/active_record.html ) they talk about $this->db->get();
Should I add it after the $this->db->select query?
My function works fine...
When should I use get() ?
Thanks you!
Yes, you'll need to run get() after the other methods. select(), from() and where() add their respective statements to the query, and get() actually runs the query and returns the result as an object.
In this case, you could just add it on to the end of the chain.
public function checkemail($email) {
$this->db
->select('email')
->from('user')
->where('email', $email)
->get();
}
If you want to work with the result afterwards, make sure that you are assigning it to a variable.
$user = $this->db
->select('email')
->from('user')
->where('email', $email)
->get();
If you use get("table_name") then you don't need to use from("table_name"). It's just an alternative syntax it seems.
From the user guide, all the way at the bottom it says: As shown earlier, the FROM portion of your query can be specified in the $this->db->get() function, so use whichever method you prefer.
I was wondering if someone could show me how to change the following sql query to codeigniters active record structure?
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE firstname LIKE "%'.$searchterm.'%" OR lastname LIKE "%'.$searchterm.'%"');
return $query->result();
Cheers
Try:
$this->db->like('firstname', $searchterm);
$this->db->or_like('lastname', $searchterm);
$query = $this->db->get($this->table_name);
return $query->result();
$query = $this->db->get($this->table_name)->like('firstname', $searchterm)->or_like('lastname', $searchterm);
For more information on CodeIgniter's database library, see this documentation.
It says , you must use the “set” method to update an entry. Pls help
My model is
$this->db->where('id', $this->uri->segment(3));
$this->db->update('mytable', $data);
My controller is
$data = $this->db->select('mytable', $_POST);
$this->contact_model->model_update_function($data);
Your $data variable doesn't contain a valid array. This is because $this->db->select(); doesn't actually run the query, you need $this->db->get(); or $this->db->get_where(); in order to do so. Even then, you also need to call $query->result(); to retrieve the data from the result.
Your controller should be
$query = $this->db->get_where('mytable', $_POST);
$data = $query->result();
$this->contact_model->model_update_function($data);