I have a Postgres database with a table for orders and a table order_contents, which includes all items of the order, and the order_id. I'm trying to make a view with order history.
In my model I have two functions, one to retrieve orders, and another to retrieve all the contents of an order, like so:
function retrieve_orders($userID){
$query = $this->db->get('orders');
$this->db->where('user_id', $userID);
return $query->result_array();
}
function get_order_contents($orderID){
$query = $this->db->get('order_contents');
$this->db->where('order_id', $orderID);
return $query->result_array();
}
So in my controller I call retrieve_orders() and pass the resulting array to the view. In the view, I want to create an HTML table for each order with all it's contents. So I iterate through each order and call get_order_contents in the loop.
Everything should work, however what happens is this error shows up in the source:
Error Number: ERROR: column "user_id" does not exist
LINE 3: WHERE "user_id" = '3'
^SELECT *
FROM "order_contents"
WHERE "user_id" = '3'
As you can see, for some reason, when I call get_order_contents() in the view, it uses the where clause I specified for the retrieve_orders() function.
What I have done so far is try to manually stop caching, but to no avail. Any help here is appreciated.
In each function, try to put where() before get().
When you try to get() order_contents it will use where clause from previous where().
Common practice is, use get() at the last call of $this->db after other calls such as where(), etc. See the Active Record documentation in codeigniter.
Chain your db functions like this
return $this->db->where('user_id', $userID)
->get('orders')
->result_array();
why not use $this->db->get_where()
Related
I dont undertand how these methods work, I'm trying to get last records inserted in my db and for this, I need a query builder so I made y consult with something like this:
$costo = Costo_promedio::where('producto_nombre_id',$id_prod->id)->latest()->take(1);
The thing is I'm still getting all records inserted, in these methods take or limit I send the paramether of how many i want to get, and my question is. Why does this happen??
And no, I dont want to use the first() or get() methods these dont work when you do server side processing in a datattable
first() is not return Collection. It returns a Model instance. Maybe you are not clear about Collection vs Model instance.
$costo = Costo_promedio::where('producto_nombre_id',$id_prod->id)->latest()->first();
Alternative:
$costo = Costo_promedio::where('producto_nombre_id',$id_prod->id)->latest()->get();
// $costo[0]
take(1) or limit(1) just adds limit 1 to the query in builder. You need to call get() to fetch the results and it will return a Collection and you can get its only element by calling first() on it.
$costo = Costo_promedio::where('producto_nombre_id',$id_prod->id)->latest()->take(1)->get()->first();
Or you can replace take(1)->get()->first() with just first()
$costo = Costo_promedio::where('producto_nombre_id',$id_prod->id)->latest()->first()
first() will add limit 1 and fetch the results and return the only item that's there
Try this
$costo = Costo_promedio::orderBy('created_at','desc')->take(3)->get();
That Will get the last three records from the database assuming that your timestamp is named created_at.
I'm having problems fetching data using Model::find() with two conditions. I want to return one record that has the given member_id AND its role is guest:
$member = CalendarMembers::find(["member_id" => $r->member_id, "role" => "guest"]);
But this isn't working. I have one user that when created, a new register is added to calendar_members table specifying this user as parent, yet, when attempting to fetch this user automatically created by just giving the member_id, it will be fetched, which SHOULD NOT.
I cannot use other ways such as:
$member = \DB::table("calendar_members")->where([ ["member_id", "=", $r->member_id], ["role", "=", "guest"] ])->first();
Because then $member->delete() or $member->destroy() will throw an error saying either delete or destroy do not exist.
The find() method uses ID to get a record. You need to use the where() and first() methods:
CalendarMembers::where(['member_id' => $r->member_id, 'role' => 'guest'])->first();
you should use where for multiple conditions and get the first row with first() method, find is basically an alias for wehre('id', $id)
To make it clear, find() method will perform database query and returns single model object as result. so find is equal to,
Model::where('id', 1)->first().
find() returns Model object and where() returns QueryBuilder. But you can directly perform on query Builder without fetching model, like,
Model::where('id', 1)->delete().
REF : https://laravel.com/docs/5.6/eloquent
I have 2 tables and models.
So, 1 CV has Many Diplomas. Relationship is defined in models.
In my edit action :
$cv = Cv::where('link', '=', $link)->firstOrFail();
Then If I'm calling relationship function diplomas like here :
$cvDiploms = $cv->diplomas;
return $cvDiploms;
It returns me array of JSON objects.
Now, I'm trying to make a query like :
$deletedDiplomas = $cv->diplomas->whereNotIn('image', $request['test-diploma'])->get();
It returns me that error :
Missing argument 1 for Illuminate\Support\Collection::get(), called in
/home/name/project/app/Http/Controllers/CvsController.php on line 272
and defined
How should I call that query to get all Cv's diplomas, where 'image' is not in $request['test-diploma'] ?
$cv->diplomas automatically calls get() on the query, so it returns a collection. You are then calling get() again, but the Collection method get() requires an argument.
You need to write $cv->diplomas() (referencing the function not the attribute) to fetch the query, which you can then filter before calling get()
$deletedDiplomas = $cv->diplomas()->whereNotIn('image', $request['test-diploma'])->get();
You are probably trying to make a query on an array object. You should use the relation itself to make the query. Try this:
$deletedDiplomas = $cv->diplomas()
->whereNotIn('image', $request['test-diploma'])
->get();
I am trying to make a filter in laravel. This following filter works
$posts= Post::where('category',$request->category)->orderBy('id','desc')->paginate(10);
But when I try to do something like this
public function index(Request $request)
{
$posts= Post::where('category',$request->category)->get();
$posts->latest()->paginate(10);
dd($posts);
It doesn't work. Can someone explain why is this and provide me the code that works. My project have multiple filter.
Error
Because $posts = Post::all(); already execute a query.
Post::where('category',$request->category)->latest()->paginate(10)->get();
would be what you want.
A note:latest requires the created_at column
You should go
$posts = Post::where('category',$request->category)->latest()->paginate(10);
the get request is unnecessary as the paginate will execute the query.
The first one makes the query by pagination i.e fetch 10 records per constructed page
For the second one, based on observation, you most likely have encountered at least 2 errors:
The first, on the line that used the get method because that method requires at least one parameter.
Type error: Too few arguments to function Illuminate\Support\Collection::get()
The other since its a collection, and since there is nothing like paginate or latest method on collection therefore throws other errors. You should check Collection's Available methods to have a glimpse of the methods allowed on collection.
One of the best solutions is to simply order the result when making the query:
Blog::where('category',$request->category)
->orderBy('created_at', 'desc') //you may use also 'updated_at' also depends on your need
->paginate(10);
This way you have the latest coming first int the pagination and also having not worrying about paginating a collection
I have a problem with laravel
I want to use a query result in my controller with a if clause to manage what i return to the view.
$res = Chaussures::where('id',$id);
if($res->name=='Adidas'){
return...
}
else {
return...
}
I tried this but it didn't work
You need to execute the query to get the result first. Then you can do whatever you need with the data.
$res=Chaussures::where('id',$id)->first();
Searching for models by ID is such a common task that Laravel has a method called find() to make it easier. This line does the same as the above.
$res=Chaussures::find($id);