I have eager loaded the data but when I am using $object->attribute it again fetch the data from database.
My query is :
$user = User::with([
'Comment' => function($query){
$query->where('active', 1);
$query->with('CommentReply.User');
$query->orderBy('updated_at', 'desc');
}
]);
But when I use $user->comment then it again load all the comments, which results to N+1 problem. Any reason why this is happening? Thanks in advance.
Everything is working fine, just stick to one convention:
$user = User::with([
'comment' => function($query){
$query->where('active', 1);
$query->with('CommentReply.User');
$query->orderBy('updated_at', 'desc');
}
]);
//then
$user->comment
or
$user = User::with([
'Comment' => function($query){
$query->where('active', 1);
$query->with('CommentReply.User');
$query->orderBy('updated_at', 'desc');
}
]);
//then
$user->Comment
Notice letter casing.
Also mind that $comment->commentReply->user will do just the same, so you need to call $comment->CommentReply->User.
Related
I was wondering how can I take posts from the database ordered from the latest to the oldest but with pagination.
This is my current code:
return view('index', [
'featured' => Post::orderBy('created_at', 'desc')->first(),
'posts' => Post::orderBy('created_at', 'desc')->skip(1)->take(6)->get(),
]);
So, I am taking the latest post as "featured" and for the rest of the posts I want to skip first (because it is already taken as "featured" one) and take others from the latest to the oldest but with pagination.
EDIT
If someone need this later this piece of code worked for me
$featured = Post::orderBy('created_at', 'desc')->first();
$posts = Post::orderBy('created_at', 'desc')->where('id', '!=', $featured->id)->paginate(2);
return view('index', [
'featured' => $featured,
'posts' => $posts,
]);
Try the below code:
return view('index', [
'featured' => Post::orderBy('created_at', 'desc')->first(),
'posts' => Post::orderBy('created_at', 'desc')->where(id, '!=', 1)->paginate(6),
]);
To display them and order them only by created_at
this code below worked for me:
$posts = Post::orderBy('created_at', 'desc')->get();
i am getting error, Internal server error,
GET http://isp.local/teachers/168/edit 500 (Internal Server Error)
Controller:
public function edit($id)
{
$teacher = DB::table('teachers')
->find($id)
->select('*')
->first();
return response()->json([
'status' => 'success',
'teacher' => $teacher,
]);
}
while when i make following changes in controller i am getting correct results, what is issue in above code?
Controller:
public function edit($id)
{
$teacher = Teacher::find($id);
return response()->json([
'status' => 'success',
'teacher' => $teacher,
]);
}
That query is wrong. ->find() executes the query, as does ->first(), and everything is selected by default, so ->select('*') is unnecessary.
$teacher = DB::table('teachers')->find($id);
Should be enough. But you're already using the correct
$teacher = Teacher::find($id);
So there isn't much point to using the DB::table() approach.
Try
->whereId($id)
or
where('id',$id)
There is code in Laravel:
$product = Product::where('slug', $slug)->with('types')->with('brand')->firstOrFail();
return $product;
I'm retrieving array with types array and brand array.
Types has boolean options "is_active"
How I can return $product with types that is_active
ex.
$product = Product::where('slug', $slug)->with('types')->with('brand')->where('types.is_active', '=', '1')->firstOrFail();
Try this out,
$product = Product::where('slug', $slug)->with([
'types' => function($query){ $query->where('is_active',true); } ,
'brand'
])
->firstOrFail();
Source docs, https://laravel.com/docs/5.6/eloquent-relationships#constraining-eager-loads
If you need only products which type is active, you can also filter them:
$product = Product::with([
'types' => function($query) { $query->where('is_active',true); },
'brand'
])
->where('slug', $slug)
->whereHas('types' => function($query) { $query->where('is_active',true); },
->firstOrFail();
Can anyone help me to update in Codeigniter?
This is my model code:
function update($data,$userid)
{
$data = array(
'dob' => $dob,
'sex' => $sex,
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
Assuming you are passing dob and sex via the data variable in the function you're just trying to access those incorrectly.
function update($data,$userid)
{
$data = array(
'dob' => $data['dob'],
'sex' => $data['sex'],
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
And for that matter if that is all that's in that data variable you can lose the first part entirely since it is really just replicating what you already have.
function update($data,$userid)
{
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
$where = array('id'=>'10','email'=>'dude#dude.com');
$data = array('username'=>'lol');
function update($data,$where){
foreach($where as $key=>$value){
$this->db->where($key,$value);
}
$this->db->update('users',$data);
}
So, i'm trying to create a function that removes a post, and inserts the values into a table named "removed_post".
Now, i get the error:
No tables used
SELECT *
Line Number: 330
What causes this? I have google'd it ALOT, and tried different "answers", without any success.
Here is my code:
class Remove extends CI_Controller {
function post($id = null) {
$this->db->get_where('comments', array('id' => $id));
$query = $this->db->get();
$data = array(
'amne_id' => $query->row()->amne_id,
'user_id' => $query->row()->user_id,
'date_created' => $query->row()->date,
);
$this->db->insert('removed_post', $data);
$this->db->delete('comments', array('id' => $id));
redirect('/');
}
EDIT: And yes, the code is not optimized.
$this->db->get_where('comments', array('id' => $id));
$query = $this->db->get();
should be:
$query = $this->db->get_where('comments', array('id' => $id));
In your code the first query is run but you are not capturing the result. The second line tries to capture results for a blank query. get_where() calls get() for you so you don't have to call it explicitly.
new error
$row = $query->row();
$data = array(
'amne_id' => $row->amne_id,
'user_id' => $row->user_id,
'date_created' => $row->date,
);