Returning null from model function - codeigniter

I want to select the records from database but this function returns null.
model
function pub_article()
{
$where=array('public'=>'1');
$query=$this->db->get_where('article',$where);
if($query->num_rows() > 0)
return $query->result();
else
return false;
}
What is wrong with this code?

Related

fetching data of joining tables in Codeigniter

can you please tell me what's wrong with my function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
}
I wanna display Two tables in one table using the foriegn key (code_filiere)
Solution 1 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query->result();
}
In the controller
$data = $this->your_model->get_by();
Solution 2 : You need to return data for controller
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return $query;
}
In the controller
$data = $this->your_model->get_by()->result();
From the doc
nothing wrong in your function, but you didn't return the captured value from the function to the controller.
You should use
$query->result() for multi-value
OR
$query->row() for single value
from database
update your function
public function get_by(){
$this->db->select('*');
$this->db->from('filiere');
$this->db->join('module', 'module.code_filiere = filiere.code_filiere');
$query = $this->db->get();
return ($query->num_rows() > 0) ? $query->result() : false;
}

Return null from eloquent relationship?

I have this relationship in one of my model classes:
public function userLike()
{
if (Auth::check()) return $this->hasOne('App\Like')->where('user_id', Auth::user()->id);
// RETURN NULL IF RECORD DOESN'T EXIST?
}
As you can see, it checks if the user is logged in and returns the Like record. However, how can I return null if the record doesn't exist?
I tried this:
public function userLike()
{
if (Auth::check()) return $this->hasOne('App\Like')->where('user_id', Auth::user()->id);
return null;
}
But I get the error:
local.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message
'Call to a member function addEagerConstraints() on null' in
/var/www/social/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:680
As a side question, am I doing this right? Is this the correct way of doing this or is there a better way?
I'm not sure it is the right way to do this. In your model you should only declare your relationships:
public function userLike()
{
return $this->hasOne('App\Like');
}
And then in a controller or something you go get the like of your user like this:
class LikesController extends Controller {
public function getLike()
{
$like = null;
if (Auth::check()) {
$like = Like::where('user_id', Auth::user()->id)->first();
}
}
}
So we have a getLike() function that get the like of a user from your model Like where the user_id is equal to the authenticated user's id.
Hope it helps!
I'm not sure! But I think it is possible to use count! When it returns 0 you know that there are no records, so you can return null.
You can just use this style in your Like Model :
// return the number of likes
function userLikes()
{
$count = $this->where('user_id', $this->post_id)->count();
// check if not null
return $count ? $count : 0; // if null return 0
}

Codeigniter get database value in controller

This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}

Codeigniter: Extract partial data from a query

I have a small question pls:
Let's say i have a long query:
$this->db->select('*')
->from('x')
->join(...)
->where('y >',$id)
->where('z <',$nr)
->where(...)
...
How can i put into a different variable all where statements so i can use them individually but only using one single query?
Thank you.
You need to create different model methods. Each method should have the where query outputting results. In your controller, you call each of these methods and store the results in variables.
Why not do this:
Controller:
function controller_method(){
$res1 = $this->model_name->get_where_results1($id, $nr);
$res2 = $this->model_name->get_where_results2($id, $nr);
$res3 = $this->model_name->get_where_results3($id, $nr);
}
Model:
function get_where_results1($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results2($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results3($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
UPDATE - if you wanted to use filters, i would this way:
Controller:
function controller_method() {
$res1 = $this->model_name->model_method($filters);
}
Model:
function model_method($filters) {
$this->db->select('*');
$this->db->from('x');
$this->db->join("");
if ($filters['filt1']) {
$this->db->where("something", $filters['filt1']);
}
if ($filters['filt2']) {
$this->db->where("something", $filters['filt2']);
}
$query = $this->db->get();
return $query->result();
}

Call to a member function addEagerConstraints() on a non-object

I get this error msg Call to a member function addEagerConstraints() on a non-object when I am trying to call a function from my model.
Here is the function:
public static function checkClaimById($claim_id) {
$result = Claim::with('orders')
->find($claim_id)
->where('orders.user_id', Auth::user()->id)
->whereNull('deleted_at')
->count();
if($result >= 1) {
return true;
} else {
return false;
}
}
I have the Model "claim" with has a field order_id and it belongs to an order.
And I have the model "order" and one Order has many claims.
Can someone help me?
thanks
Regards
I have modified my function like this:
public static function checkClaimById($claim_id) {
$result = Claim::with(array('orders' => function($query)
{
$query->where('user_id', Auth::user()->id);
}))
->where('id', $claim_id)
->whereNull('deleted_at')
->count();
if($result >= 1) {
return true;
} else {
return false;
}
}
Found the solution here: http://laravel.com/docs/eloquent#eager-loading

Resources