I have a function in my helper
function somethingOrOther($id)
{
$posts = Posts::where('author_id', $id);
$posts_count = [];
$posts_count['total'] = $posts->count();
$posts_count['published'] = $posts->where('status', 'published')->count();
$posts_count['draft'] = $posts->where('status', 'draft')->count();
return $posts_count;
}
and this function works perfectly fine. But now I'm trying use it in this function in a controller to return it to a view
public function profile($id)
{
$posts_count[]= somethingOrOther($id);
return view ('display.profile')->withPosts_count($posts_count['total'])->withPosts_published_count($posts_count['published'])->withPosts_draft_count($posts_count['draft']);
}
But I get an error of 'Undefined index: total'. What do I need to do to separately return the array values of $posts_count?
I belive you can just do
$posts_count= somethingOrOther($id);
and then return it like
return view ('display.profile',["posts_count" => $posts_count]);
and in blade get it like
{{$posts_count["total"]}}
Try with
$post_count = $this->somethingOrOther($id);
if ( !$post_count ) {
return "no posts for this author";
}
...
return view('display.profile', compact('post_count'));
In view
{{ $post_count['total'] }}
Related
I use octobercms and User Extended plugin(Clacke). I try to render a pagination because for now i have a lot of registered users and they display on one page.
I use random users function from \classes\UserManager.php
public static function getRandomUserSet($limit = 7)
{
$returner = new Collection;
$userCount = User::all()->count();
if(!isset($userCount) || empty($userCount) || $userCount == 0)
return [];
if($userCount < $limit)
$limit = $userCount;
$users = User::all(); //paginate(5)
if(empty($users))
return $returner;
$users->random($limit);
$friends = FriendsManager::getAllFriends();
foreach($users as $user)
{
$userAdd = true;
if(!$friends->isEmpty())
{
foreach($friends as $friend)
{
if($user->id == $friend->id)
{
$userAdd = false;
break;
}
}
}
if($user->id == UserUtil::getLoggedInUser()->id)
$userAdd = false;
if($userAdd)
{
$returner->push($user);
}
}
return $returner->shuffle();
}
try to do this with changing return $returner->paginate(25); and $users = User::paginate(25); but throws me an error
An exception has been thrown during the rendering of a template
("Method paginate does not exist.").
After that i try to change directly in \components\User.php
public function randomUsers()
{
return UserManager::getRandomUserSet($this->property('maxItems'))->paginate(12);
}
But again the same error.
Tryed and with this code and render in default.htm {{ tests.render|raw }}
public function randomUsers()
{
$test = UserManager::getRandomUserSet($this->property('maxItems'));
return $test->paginate(10);
}
Again with no success. Could anyoune give me some navigation and help to fix this?
If you are using random users function from \classes\UserManager.php
I checked the code and found that its using Illuminate\Support\Collection Object. So, for that Collection Object pagination works differently
You need to use forPage method.
On the other hands paginate is method of Illuminate\Database\Eloquent\Collection <- so both collection are not same
Use forpage
// OLD return UserManager::getRandomUserSet($this->property('maxItems'))
// ->paginate(12);
TO
return UserManager::getRandomUserSet($this->property('maxItems'))
->forPage(1, 12);
forPage method works like forPage(<<PAGE_NO>>, <<NO_OF_ITEM_PER_PAGE>>);
so if you use forPage it will work fine.
if any doubt please comment.
I want to show variables value in blade file. Below is the controller code:-
public function ClientListReviews()
{
// to show client list get data from users booking table
$select_clientlist =
DB::table('users_booking')>where('service_provider', '=', 1)->get();
if(count($select_clientlist) > 0)
{
return view('client-database')->with($select_clientlist);
}
else
{
return view('client-database')->withMessage('No Details Found');
}
}
I want to show the values coming in $select_clientlist variable. Below is the code in my blade file:-
#foreach($select_clientlist as $clientlist)
{{$clientlist->firstname}}
#endforeach
And below is the route file code:-
Route::post('client_list_ajax','ClientDatabase\ClientdatabaseController#ClientListReviews');
I am receiving error.
What am I doing wrong?
Pass the variable using compact method
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
view1,view2,view3 are variable names
Since you only pass the variable when there is records wrap your for each inside isset
if (isset($select_clientlist)) {
foreach($select_clientlist as $clientlist) {
}
}
your query should be like this . you may be forget SELECT statement
$select_clientlist = DB::table('users_booking')->select('*')->where('service_provider', '=', 1)->get();
Either use as
return view('client-database')->with('select_clientlist',$select_clientlist);
Or
return view('client-database',compact('select_clientlist'));
Also add in select_clientlist else part to prevent undefined error
public function ClientListReviews()
{
// to show client list get data from users booking table
$select_clientlist =
DB::table('users_booking')>where('service_provider', '=', 1)->get();
if(count($select_clientlist) > 0)
{
return view('client-database')->with('select_clientlist',$select_clientlist);
}
else
{
$select_clientlist = [];
return view('client-database')->with('select_clientlist',$select_clientlist)->withMessage('No Details Found');
}
}
OR check by isset($select_clientlist) in blade file
$__currentLoopData = isset($select_clientlist)?$select_clientlist:[];
Pass that variable to your view either way .. it should be a collection. If there are no records, it is just empty. The foreach wont run if its empty. Its as simple as that. No need to check if anything is set or is empty etc... just always pass that collection.
public function ClientListReviews()
{
$select_clientlist = DB::table('users_booking')->where('service_provider', 1)->get();
$view = view('client-database', compact('select_clientlist'));
if ($select_clientlist->isEmpty()) {
$view->with('message', 'No Details Found');
}
return $view;
}
I am getting the following error.
Message: Trying to get property of non-object
Table Structure:
name ='fb' value='https://www.facebook.com/mypage'
Model:
function get_fb_url(){
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row();
}
Controller:
$data['fb_url']=$this->Home_model->get_fb_url()->value;
View:
<li><img src="css/imgs/fb.png" /></li>
When i run my page it give me error
Message: Trying to get property of non-object
only when if value in the table is link like above, if its simple text then text properly show on page.
How Can i get rid off this error?
Instead of echo use return type
Change
echo $this->db->get()->row();
to
return $this->db->get()->row();
And add $ before variable name in view
<a href="<?php echo $fb_url;?>"
UPDATED
Change your model code to
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
return $this->db->get()->row();
}
you must try like this:
function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name', 'fb');
$query = $this->db->get();
return $query->row();
}
and in Controller:
$result = $this->Home_model->get_fb_url();
//try print $result, it must be an stdObject
$data['fb_url']=$result->value;
it is because in $data['fb_url']=$this->Home_model->get_fb_url()->value; / differentiate the link to the other function, that's why u r getting this error.
get_fb_url is returning object array not object. so you have to return value from method.
function get_fb_url()
{
$query=$this->db->select('value');
$query=$this->db->from('preferences');
$query=$this->db->where('name','fb');
return $this->db->get()->row()->value;
}
and than
$data['fb_url']=$this->Home_model->get_fb_url();
In Your model do this
public function get_fb_url() {
$this->db->select('value');
$this->db->from('preferences');
$this->db->where('name','fb');
$query = $this->db->get();
$row = $query->row();
return $row;
}
In Controller :
$data = array();
$result = $this->Home_model->get_fb_url();
$data['fb_url'] = $result->value;
$this->load->view('your_view',$data);
In View :
echo $fb_url;
I am facing the following error:
Trying to get property of non-object and Undefined variable php
errors in my code
Controller:
function showDoctorInformation(){
$this->load->model('PatientModel');
$data['doctorinfo'] = $this->PatientModel->getDoctorInformation();
$this->parser->parse('patient_msgview', $data);
}
Model:
function getDoctorId() {
$this->db->from('person');
$this->db->select('doctorId');
$doctorId = $this->db->get()->result();
return $doctorId;
}
function getDoctorInformation() {
$doctorId = $this->getDoctorId();
$this->db->from('DoctorInfo');
$this->db->where('doctorId', $doctorId);
$this->db->select('name', 'surname', 'Bio', 'Address', 'img');
$doctorinfo = $this->db->get()->result();
return $doctorinfo;
}
View:
<?= $doctorinfo->name ?>
I have displayed information from the database before with this method and I can't see the error now.
result() return
This method returns the query result as an array of objects, or an
empty array on failure
So you need to fetch single data form your database using ->row()
function getDoctorId() {
$this->db->select('doctorId');
$this->db->from('person');
$this->db->select('doctorId');
$query = $this->db->get();
if ($query->num_rows == 1) {
$row=$query->row();// fetch single row
return $row->doctorId;// get doctor id
} else {
return FALSE;
}
}
And in viwe you have to get your data using foreach loop
For exm
foreach ($doctorinfo as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
i have this code:
public function test(){
$results = Cand::paginate(4);
if (Request::ajax()) {
return Response::json(View::make('admin.posts')->with('results', $results)->render());
}
$this->layout->content = View::make('admin.dashboard')->with('results', $results);
}
It works fine, but when a user enters a page that does not exist in the URL, like "?page=9999", how can I restrict this?
You can get the last page by calling getLastPage() on your paginator instance:
if ( Input::get('page', 1) > $results->getLastPage() )
{
App::abort(404);
}