Output query object into view - php

My search function is not returning the results I expect.
How can I output the query object to see what is returned by the db query?
public function search()
{
//Save requests to variables
$price = Helper::explodeString(Request::get('price'));
$texts = Helper::explodeString(Request::get('texts'));
$minutes = Helper::explodeString(Request::get('minutes'));
$data = Helper::explodeString(Request::get('data'));
$contract = Request::get('contract');
$phone_option = Request::get('phone-option');
//get all selected carriers
$carriers = array();
foreach(Carrier::all() as $carrier){
if(Request::has($carrier->name)){
$carriers[] = $carrier->id;
}
}
// build query
$query = Plan::whereBetween('price', $price)
->whereBetween('text', $texts)
->whereBetween('minutes', $minutes)
->whereBetween('data', $data)
->whereIn('carrier', $carriers)
->where('contract', $contract)
->where('phone_inlcuded', $phone_option);
return view('planresults')->with('plans', $query);
}
I am then trying to output the object into my view as follows
#foreach($plans as $plan)
<div class="panel price">
<div class="panel-heading arrow_box text-center">
<h3>{{$plan->Carrier->name}}</h3>
<h3>{{$plan->name}}</h3>
</div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong>${{$plan->price}} / month</strong></p>
</div>
<ul class="list-group list-group-flush text-center">
<li class="list-group-item">{{$plan->Carrier->name}}</li>
<li class="list-group-item">{{$plan->contract}} Month term contract</li>
#if($plan->phone_included == 0)
<li class="list-group-item">No phone included</li>
#else
<li class="list-group-item">Phone option included</li>
#endif
</ul>
<div class="panel-footer">
<a class="btn btn-lg btn-block btn-success" href="{{$plan->url}}">BUY NOW!</a>
</div>
</div>
<!-- /PRICE ITEM -->
#endforeach
No results are being displayed in my view so I assume my object is empty... I'm very confused and have been stuck on this for ages now

You never execute the query in $query. Read up: http://laravel.com/docs/5.0/queries
You'll need to specify one of the following:
$query->pluck()
$query->get()
$query->first()

Related

Undefined variable $p and foreach() argument must be of type array|object, null given in codeigniter 3

I am trying to pass an array that gets initialised when it gets queried from the database.
The array of products that I try to pass and display in the view just throws an error and i cannot figure out why.
I am using codeigniter 3.
ProductsModel.php
function get_all_products()
{
$this->db->select("produceCode,description,category,supplier, quantityInStock, bulkSalePrice,photo");
$this->db->from('products');
$query = $this->db->get();
return $query->result();
}
ProductsController.php
public function listProducts()
{
$data['p']=$this->ProductsModel->get_all_products(/*2, $this->uri->segment(3)*/);
$this->load->view('products',$data);
}
products.php
<?php foreach( $p as $row) { ?> // line of code thats causing the error
<div class="col-md-3">
<div class="card p-3">
<div class="text-center"> <!--<img src="../assets/images/products/thumbs/--><?php /*$row->photo;*/?>" width="200"> </div>
<div class="product-details"> <span class="font-weight-bold d-block">$ 7.00</span> <span><?php echo 'hi'/*$row->description;*/?></span>
<div class="buttons d-flex flex-row">
<div class="cart"><i class="fa fa-shopping-cart"></i></div> <button class="btn btn-success cart-button btn-block"><span class="dot">1</span>Add to cart </button>
</div>
</div>
</div>
</div>
<?php } ?>
any suggestions would be great
You are returning a variable named $data in your controller, but in your view you are looping through $p.
Try this:
foreach ($data['p'] as $row)

(Undefined offset:0) can't generate more data to trouble shoot

This morning my web app was working. After a few posts were made I tried to login and get the Undefined offset:0 error.
The only information I'm getting from Laravel is that it's in the file path laravel\framework\src\Illuminate\Support\Collection.php
On this file it's saying the error is on line 1688:
public function offsetGet($key)
{
return $this->items[$key];
}
Based on what I found online the issue is related to a bad data call. Perhaps trying to pull data that doesn't exist. I have looked through my database and last 4 Posts and Comments did not have any file attachments so I am not sure where to start looking.
Additionally, the code was written a few years ago by a coworker which makes it more difficult for me to decipher.
Here is my blade file:
#if(app('request')->input('gid'))
<input type="hidden" class="groupid_SeeMore" value="{{app('request')->input('gid')}}"/>
#endif
#foreach($timeline as $post)
<?php
/** the Below Script will grab all Group Names that go with the post and combine into a single stirng **/
$groups= array();
if(strpos($post->grouppermissions,"|")===false){
foreach(explode(',',$post->grouppermissions) as $id){
if($id!=""){
$group = DB::table('groups')->where('groupid','=',$id)->get();
// $groups[] .= $group[0]->name;
$groups[].=explode('|',$group[0]->name)[0];
}
}
}elseif(strpos($post->grouppermissions,"|")!==false){
foreach(explode(',',$post->grouppermissions) as $group){
if($group!="")$groups[].=explode('|',$group)[0];
}
}
$groups=implode(',',$groups);
?>
#if($post->status==0)
<div id="{{$post->id}}" class="Post_Container {{$post->type}} {{$post->type}}{{$post->id}}">
#endif
#if($post->status==1)
<div id="{{$post->id}}" class="Post_Container_Two {{$post->type}} {{$post->type}}{{$post->id}}"><center><p id="cldpsttxt">This post has been closed</center>
#endif
#if($post->favorite==0)
<div class="Post_Header Post_CCthbckgrnd" id="Post_Header{{$post->id}}">
#endif
#if($post->favorite==1)
<div class="Post_Header Post_CCthbckgrnd_Two" id="Post_Header{{$post->id}}"><center><p id="fvrtpsttxt">This post has been Favorited</center>
#endif
<input type="hidden" class="lastid" value="{{$post->id}}"/>
<input type="hidden" class="recent_comment" value="{{$post->recent_comment}}"/>
<div class="Profile_Picture_Container">
<div class="Profile_Picture"><img style="width:100%; height:100%;" src="https://www.vqventure.com/{{$post->pinfo[0]->img}}"/></div>
</div>
#if($groups !="")
<div class="Ginfo">
<center>
<!-- Below the if statement looks for posts with only one group permission-->
#if(strpos($groups, ",") === false)
<b>{{$groups}}</b>
#endif
<!-- Below the if statement looks for posts with multiple group permissions-->
#if(strpos($groups, ",") !== false)
<?php
$key = -1;
$perm = explode(',',$post->grouppermissions);
?>
#foreach(explode(',',$groups) as $group)
<?php $key++;?>
#if($group!="")
<b>{{$group}}</b>
#endif
#endforeach
#endif
</center>
</div>
#endif
<div class="Username_Container">
<div class="Username_Location">
<!--This will be the owner of the post name, for a business it will be the employee name-->
<div class="Username"><b>{{$post->pinfo[0]->name}}</b></div>
</div>
</div>
#if($post->bid != 0 && $post->bid==$user->bid || $post->bid==0 && $post->owner == $user->email)
<div class="Favorite_Container">
<button id="favorite" class="favorite{{$post->id}}" type="{{$post->favorite}}" type2="Timeline"value="{{$post->id}}"></button>
<button id="openclose" class="status{{$post->id}}" type="{{$post->status}}" type2="Timeline" value="{{$post->id}}"></button>
#if($user->bid!=0)
<div class="keywordTypes">
<select class="keywordType" id="{{$post->id}}">
<?php
$cat=$post->category;
if($cat=="")$cat="No category";
?>
<option value="{{$post->category}}" selected>{{$cat}}</option>
#foreach(explode('#$#',$types) as $type)
#if($type!="")<option value="{{$type}}">{{$type}}</option>#endif
#endforeach
</select>
</div>
#endif
</div>
#endif
<b style="position:absolute; top:10%; right:22%;">#{{$post->id}}</b>
#if($post->bid != 0 && $user->admin==1 && $post->bid==$user->bid || $post->owner == $user->email )
<div class="Delete_Button_Container">
<button class="PostDeleteButton" value="{{$post->id}}" id="DeleteMainBTN"></button>
</div>
#endif
</div>
<center>
<!-- Video -->
#if($post->videos!="[]")
<div class="Post_ContentContainer">
<div class="Post_Thumbnail_Container" pid="{{$post->id}}">
<a class="watchVideo" type="" id="{{$post->videos[0]->url}}">
<center> <div class="Post_Thumbnail">
<?php
$vpic = substr($post->videos[0]->url,0,-3)."jpg";
$vpic2 = str_replace("_correted","",$vpic);
?>
<img style="width:100%; height:100%;"src="https://www.vqventure.com/{{$vpic}}" onerror="this.src='https://www.vqventure.com/{{$vpic2}}'" class="video_thumbnail"/>
</div></center>
</a>
</div>
</div>
#endif
<!-- PDF -->
#if($post->else!="[]")
#if($post->else[0]->pics!="" || $post->else[0]->pdf!="")
<div class="Post_ContentContainer">
<div class="Post_Thumbnail_Container" pid="{{$post->id}}">
#if($post->else[0]->pdf!="")
<center><div class="Post_Thumbnail"><a id="wtfPDF" href="https://www.vqventure.com/<?php echo explode('$!$',$post->else[0]->pdf)[0];?>"><img id="PDF_postview" src="https://www.vqventure.com/imgs/pdf.jpeg"/></a></div></center>
#endif
<!-- Images -->
#if($post->else[0]->pics!="")
<center>
<div>
<ul class="Post_Thumbnail" style="list-style-type: none;" id="IMAGES">
#foreach(explode("$!$",$post->else[0]->pics) as $file)
#if($file !="")
<li><img id="IMG_postview" pid="{{$post->id}}"src="https://www.vqventure.com/{{$file}}" class="IMG_postview{{$post->id}}"/></li>
#endif
#endforeach
</ul>
</div>
</center>
#endif
</div>
</div>
<!--This is the end of if pics and pdf-->
#endif
#endif
</center>
<div class="Post_Info_Container2 Post_CCthbckgrnd">
<div class="Post_Info_Header">
<div class="Date_Container">
<!--Input the Date here -->
<?php
$date= explode(' ',$post->timestamp)[0];
$time= explode(" ",$post->timestamp)[1];
?>
<div class="Date"><center><b>{{$date}}</b></center></div>
</div>
<div class="VidTime_Container">
<!--Input the length of the video here -->
<div class="VidTime"><center></center></div>
</div>
<div class="TimeStamp_Container">
<!--Input the TimeStamp here -->
<div class="TimeStamp"><center><b>{{$time}}</b></center></div>
</div>
</div>
#if($post->videos!="[]")
<div class="Post_Title_Container">
<div class="Post_Title_Name"><center><b>Title:</b></center></div>
<div class="Post_Title">{{$post->videos[0]->title}}</div>
</div>
#endif
<div class="Post_Description_Container">
<div class="Post_Description_Name"><center><b>Description:</b></center></div>
<div class="Post_Description">
#if($post->videos!="[]") {!!nl2br(e($post->videos[0]->description)) !!}
#else
<p>{!! nl2br(e($post->else[0]->post)) !!}</p>
#endif</div>
</div>
</div>
#if($showComments=="yes")
<div class="Post_CommentContainer" id="Make_Comment{{$post->id}}">
<textarea class="Post_Comment_TextContainer Post_Comment_TextContainer{{$post->id}}" placeholder="Comment" maxlength="300" rows="2" onkeydown="expandtext(this);" ></textarea>
<input type="hidden" class="gperms{{$post->id}}" value="{{$post->grouppermissions}}"/>
<button class="ChooseFile"><label id="CF" for="CFile{{$post->id}}"></label> <input type="file" name="imgs[]" class="CFile" id="CFile{{$post->id}}" multiple/></button>
<input type="hidden" value="{{ csrf_token() }}" class="CToken{{$post->id}}"/>
<div class="Post"><button id="Pst" pid="{{$post->id}}" ></button></div>
</div>
#if($post->comments!="[]")
<div class="Post_CommentFeedContainer Post_CommentFeedContainer{{$post->id}}">
#foreach($post->comments as $comment)
#if($comment->owner==$user->email)
<div class="Owner_CommentContainer" id="CommentId{{$comment->id}}">
<div class="OCC_Left">
<div class="C_Owner_Name"><center>{{$comment->info[0]->name}}</center></div>
</div>
<div class="OCC_Right">
<div class="C_TimeStamp"><center>{{$comment->timestamp}}</center></div>
#if($post->bid != 0 && $post->bid==$user->bid || $post->bid==0 && $comment->owner == $user->email)
<button class="Delete_Button" value="{{$comment->id}}" id="C_dltbtn" type="Timeline"></button>
#endif
</div>
<div id="CTwo">{!!nl2br(e($comment->comment)) !!}</div>
#if($comment->files!="")
<div class="Owner_Media_Container CommentImages" cid="{{$comment->id}}">
#foreach(explode('#%%#',$comment->files) as $file)
#if(strpos($file, '.pdf') === false && $file!="")<div class="Media_Thumbnail"><img style="width:100%; height:100%;" src="https://www.vqventure.com/{{$file}}" class="CImage{{$comment->id}}"/></div>
#elseif(strpos($file, '.pdf') !== false)<div class="Media_Thumbnail"><img style="width:100%; height:100%;" src="https://www.vqventure.com/imgs/pdf.jpeg"/></div>
#endif
#endforeach
</div>
#endif
</div>
#endif
#if($comment->owner!=$user->email)
<div class="Guest_CommentContainer CommentImages" id="CommentId{{$comment->id}}" cid="{{$comment->id}}">
<div class="GCC_Left">
#if($comment->files!="")
<div class="Guest_Media_Container">
#foreach(explode('#%%#',$comment->files) as $file)
#if(strpos($file, '.pdf') === false && $file!="")<div class="Media_Thumbnail" ><img style="width:100%; height:100%; " src="https://www.vqventure.com/{{$file}}" class="CImage{{$comment->id}}"/></div>
#elseif(strpos($file, '.pdf') !== false)<div class="Media_Thumbnail" ><img style="width:100%; height:100%; " src="https://www.vqventure.com/imgs/pdf.jpeg"/></div>
#endif
#endforeach
</div>
#endif
#if($comment->files!="")
<div class="C_TimeStamp_Guest_Two"><center>{{$comment->timestamp}}</center></div>
#endif
#if($comment->files=="")
<div class="C_TimeStamp_Guest"><center>{{$comment->timestamp}}</center></div>
#endif
</div>
<div class="GCC_Right">
<div class="C_Guest_Name"><center>{{$comment->info[0]->name}}</center></div>
#if($post->bid != 0 && $post->bid==$user->bid && $user->admin==1|| $post->bid==0 && $comment->owner == $user->email)
<button class="Delete_Button" value="{{$comment->id}}" id="C_dltbtn"></button>
#endif
</div>
<div id="COne">{!! nl2br(e($comment->comment)) !!}</div>
</div>
#endif
#endforeach
</div>#endif
#if($post->comments!="[]") <div class="ExpandComment_Container" ><center><div class="Display_AllComments"><button class="Show_All_Comments" id="{{$post->id}}" action=1>Expand Comments...</button></div></center></div> #endif
#endif
</div>
<div id="loading-image" style="display:none; top: 10px; position:relative;"><i>Uploading Comment Please wait....</i></div>
#endforeach
And here is my controller:
public function timeline(Request $request){
if(!isset($this->user->email)){
if(!$request->queryType)return redirect('/');
}
if(isset($this->user->email)){
$email = $this->user->email;
// This needs to query where permissions are equal to the email and the owner = email
$mygroups= groups::where('owner','=',$email)->orwhere('groupadmin','LIKE',"%$email%")->orwhere('members','LIKE',"%$email%")->get();
$groups= "";
foreach($mygroups as $group){
if($group!="")$groups.=','.$group->groupid."$!$".$group->name;
}
$groups=explode(',',$groups);
$timeline = timeline::with('else','videos','comments');
if(!$mygroups->isEmpty()){
$timeline = $timeline->where(function ($query) use($groups) {
for ($i = 0; $i < count($groups); $i++){
if($groups[$i]!=""){
$gid=explode('$!$',$groups[$i])[0];
$gname = explode('$!$',$groups[$i])[1];
$query->orWhere('grouppermissions', 'like', '%' . $gid .'%')
->orWhere('grouppermissions', 'like', '%' . $gname .'%');
}
}
});
$timeline=$timeline->orwhere('owner','=',$this->user->email);
}
if($mygroups->isEmpty()) $timeline=$timeline->where('owner','=',$this->user->email);
if($request->queryType=="Original")$timeline=$timeline->orderBy('id','desc');
if($request->queryType=="Recent" || !$request->queryType)$timeline = $timeline->orderBy('recent_comment','desc')->orderBy('timestamp','desc');
$timeline=$timeline->limit(15)->get();
foreach($timeline as $g){
//THis is for grab business or user info for a post , maynot need it
$pinfo = user::where('email','=',$g['owner']);
$pinfo = $pinfo->get();
$g['pinfo']=$pinfo;
if($g['pinfo'][0]['name']==null)$g['pinfo'][0]['name']=$g['pinfo'][0]['companyname'];
$g['type']="";
foreach($g['comments'] as $c){
$info = user::select("name","img","phone")->where('email','=',$c['owner']);
$info = $info->limit(1)->get();
$c['info']=$info;
$ctimestamp = strtotime($c['timestamp']);
$cdate = new DateTime("#".$ctimestamp); // will snap to UTC because of the
$cdate->setTimezone(new DateTimeZone('America/New_York'));
$c['timestamp']=$cdate->format('Y-m-d H:i:s');
}
$timestamp = strtotime($g['timestamp']);
$date = new DateTime("#".$timestamp); // will snap to UTC because of the
$date->setTimezone(new DateTimeZone('America/New_York'));
$g['timestamp']=$date->format('Y-m-d H:i:s');
}
//if($this->user->admin==1){
//the $list will need to be the businesses branches data exploded by ','
$branches = DB::table('users')
->wherein('email',explode(',',$this->user->branches))->select('bid','email','name')
->get();
// The next few lines are for invitessent
// }
$sess= $this->user->invitessent;
$emails = explode(",",$sess);
$info = array();
foreach($emails as $email){
$test =user::select("name","img")->where('email','=',$email)->limit(1)->get();
if(!$test->isEmpty())$info[]=$test;
}
$website = $this->user->website;
$posts = DB::table('inquiry')->select('*')->where('bid','=', $this->user->bid)->latest()->get();
$types="";
if($this->user->bid!=0){
$biz= user::select('keywords')->where(array(['bid','=',$this->user->bid],['admin','=',1]))->limit(1)->get();
$types = $biz[0]->keywords;
}
if(!$request->queryType)return view('layouts.Timeline',['timeline'=>$timeline,'types'=>$types,'branches'=>$branches, 'posts'=>$posts ,'showComments'=>'yes','user'=>$this->user,'gp_admins'=>'','gp_members'=>'','type'=>'timeline']);
if($request->queryType)return view('includes.Timeline_Posts',['timeline'=>$timeline,'types'=>$types,'user'=>$this->user,'showComments'=>'yes']);
}
}
In addition to fixing the issue and allowing users to access their data, I am hoping someone can help me understand what caused the issue in the first place.
The entire project is in the process of being re-built and I would like to avoid issues with the revamp from lessons learned on this structure. However, I need a quick fix to get it back online until we complete the revamp.
UPDATE:
I changed the code to use ->first instead of [0] but its still giving the same issue. I think the issue is related to the following line of code but not sure how....
<?php
$key = -1;
$perm = explode(',',$post->grouppermissions);
?>
#foreach(explode(',',$groups) as $group)
<?php $key++;?>
So far I have not been able to get any progress...

Is it possible to have 3 div elements and have your loop fill them in order?

Usually when I loop through a database table records, I put them in 1 div, however, I have been wondering whether it is possible to create 3 divs and then put one record in each div, then start from the first div again and rinse and repeat.
Example of how I've done it so far:
<div class="container">
#foreach($albumImages as $albumImage)
<div class="centeredImage stickyContainer" style="background-image: url('/storage/uploads/albums/{{$albumName}}/{{$albumImage->file_name}}')">
<a class='specialA' href=''></a>
</div>
#endforeach
</div>
As you can see in this case, all the records are in the container div.
Example of what I've been thinking about:
<div class="flex-grid">
<div class="col-l"></div>
<div class="col-c"></div>
<div class="col-r"></div>
</div>
and have the first record go in col-l, the second in col-c, the third in col-r and then start from col-l again.
Try this
<div class="flex-grid">
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
<div class="col-l"></div>
#elseif($count % 3 == 1)
<div class="col-c"></div>
#else
<div class="col-r"></div>
#endif
#php($count++)
#endforeach
</div>
You can use this code but I later will update my answer with more good solution
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
#php($albumImages1[] = $albumImage)
#elseif($count % 3 == 1)
#php($albumImages2[] = $albumImage)
#else
#php($albumImages3[] = $albumImage)
#endif
#php($count++)
#endforeach
#if (!empty($albumImages1))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages2))
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages3))
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
#endif
Also you can split three part your initial array make global helper functions.
define global function
function split_sequence_by_count ($array, $count) {
$result = [];
for ($i = 0; $i < $count; $i++) {
$result[$i] = [];
}
$_count = 0;
foreach ($array as $current) {
$index = $_count % 3;
$result[$index][] = $current;
$_count++;
}
return $result;
}
usage in blade
#php(list ($albumImages1, $albumImages2, $albumImages3) = split_sequence_by_count($albumImages, 3))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
Another way would be array_chunk then just run through the array with 2 nested loops. Should be self-explaining.
<?php
$people =
[
'John',
'Paul',
'Ringo',
'George'
];
$i=0;
$classes = ['col-a','col-b','col-c'];
foreach($people as $name) {
$class = $classes[$i++%3];
?>
<div class='<?=$class?>'>
<?=$name?>
</div>
<?php
}
Formatted output:
<div class='col-a'>
John
</div>
<div class='col-b'>
Paul
</div>
<div class='col-c'>
Ringo
</div>
<div class='col-a'>
George
</div>
Regarding placing each name in each column (as per your comment), we could wrangle the array format:
$classes = ['col-a','col-b','col-c'];
$i=0;
foreach($people as $name)
$columns[$classes[$i++%3]][] = $name;
?>
<?php foreach($columns as $class=>$column) { ?>
<div class='<?=$class?>'>
<?php foreach($column as $name) { ?>
<div class="name">
<?=$name?>
</div>
<?php } ?>
</div>
<?php } ?>
Formatted output:
<div class='col-a'>
<div class="name">
John
</div>
<div class="name">
George
</div>
</div>
<div class='col-b'>
<div class="name">
Paul
</div>
</div>
<div class='col-c'>
<div class="name">
Ringo
</div>
</div>

How to get data from codeigniter controller using Angularjs $http

The following Codeigniter controller outputs a JSON when accessed through url.
Codeigniter controller
function ttoday(){
$this->load->model('dashboard_stats');
$tours_today = $this->dashboard_stats->upcoming_tours();
$data_json = json_encode($tours_today);
echo $data_json;
}
Codeigniter Model
function upcoming_tours()
{
mysql_query("SET time_zone = '+3:00'");
$this->db->order_by('from_start_time','asc');
$this->db->where('DATE(from_start_time) = CURDATE()');
$this->db->where('from_start_time > DATE_SUB(NOW(), INTERVAL 90 MINUTE)');
$query = $this->db->get('tours');
return $query->result();
}
Angularjs Controller
translineClassic.controller('homeController', ['$scope', '$http', '$window', '$rootScope', function($scope, $http, $window, $rootScope){
$http.get('http://122.222.22.2/booking/public_html/admin/api/cities')
.success(function(result){
$scope.cities = result;
});
$http.get('http://122.222.22.2/booking/public_html/admin/api/ttoday')
.success(function(result){
$scope.tours = result;
});
}]);
Encoded Json
[{"tour_id":"4260","route_time":"121473438600","tour_route":"12","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 09:50:05"},{"tour_id":"4412","route_time":"111473438600","tour_route":"11","from_start_time":"2016-09-09 19:30:00","available_seats":"47","start_price":"800.00","additional":"0","book_online":"0","date_created":"2016-08-30 11:34:46"}]
In the database where the routes are stored. We have stored the tour_route id and not the route name. There is a separate table that stores the routes, and I can get the route name by using the following code.
function get_route_name($id){
$this->db->select('route_name');
$this->db->where('route_id', $id);
$query = $this->db->get('routes');
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->route_name;
}
}
Using the code as shown above, I can be able to use the $http for angularjs to get the data and then bind with the view.
The problem arises when I need to show the route name. but instead I have tour_route, I can be able to do this in php using the get_route_name() function, How can I print route name instead of tour_route? Do I have to use the $http again? Please help me on this. I am new to angularJs and any help will be appreciated.
My View
<article class="result">
<div ng-repeat="tour in tours">
<div class="one-fourth heightfix"><img class="img-responsive" src="assets/images/translinebus.JPEG" alt="" /></div>
<div class="one-half heightfix">
<h3>{{ tour.tour_route }} ?</h3>
<ul>
<li>
<span class="ico people"></span>
<p>Max people</strong> <br/>
Seats Remaining </strong> </p>
</li>
<li>
<span class="ico luggage"></span>
<p>Max <strong>3 suitcases</strong> <br />per vehicle</p>
</li>
<li>
<span class="ico time"></span>
<p>Departure Time<br />
<strong><span class="icon-clock" style="color:red;">
{{ tour.from_start_time }} </span>
</strong></p>
</li>
</ul>
</div>
<div class="one-fourth heightfix">
<div>
<div class="price"><small>KES</small> {{ tour.start_price }}</div>
<span class="meta">per passenger</span>
<button value="{{ tour.tour_id }}" type="submit" class="btn grey large">select</button>
</div>
</div>
<div class="full-width information" ng-show="showDetails">
x
<p>Free Wifi, Charging</p>
</div>
</div>
</article>

In category counts his item from database table

My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}

Categories