(Undefined offset:0) can't generate more data to trouble shoot - php
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...
Related
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>
Pagination replacing new search results with old ones when try to change page
I've added a ajax search box in app's home page. It works fine. But when i use the pagination after the new results shows up, pagination leads me to the old results. I'm not sure what I'm not doing right. So looking for expert's help to get a solution for it. I've these in routes -- Route::get('/', 'BookmarkController#index'); Route::post('/', 'BookmarkController#search'); I've these in their controller - public function index(Request $request) { $tags_list = Tag::orderBy('tag', 'asc')->get(); $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->paginate(10); $bookmarks_all = Bookmark::orderBy('created_at','desc')->where('public', '1')->get(); return view('welcome')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all); } public function search(Request $request){ $search_value = $_POST['search']; $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10); return view('public_bookmarks')->with('bookmark', $bookmarks); } In welcome.blade.php <div class="container content-container"> <div class="row "> <div class="col-sm-12"> <div class="page-header"> <div class="row"> <span class="title col-sm-8">Recent Bookmarks</span> <form id="demo-2" class="search-form col-sm-4" method="post"> {{ csrf_field() }} <div class="input-group"> <!-- <input id="search" class="form-control" onkeyup="search_data(this.value, 'result');" placeholder="Search" name="search" value="" type="text"> --> <input id="search" class="form-control" placeholder="Search" name="search" value="" type="text"> <span class="input-group-btn"> <button class="btn btn-primary" id="search-btn" type="button"><i class="fa fa-search"></i></button> </span> </div> <script type="text/javascript"> $(document).ready(function() { $('#search-btn').click(function(){ $.ajax({ url: '/', type: "post", data: {'search':$('input[name=search]').val(), '_token': $('input[name=_token]').val()}, success: function(data){ $('#search-results').html(data); }, error: function (data) { console.log('Error on Article extracting'); console.log(data); } }); }); }); </script> </form> </div> </div> </div> <div class="col-sm-9" id="search-results"> #include ('public_bookmarks') </div> </div> In public_bookmars.blade.php #if (count($bookmark) > 0) <div class="row card-row"> #foreach ($bookmark as $bookmark_single) <div class="col-sm-4 col-xs-12 card-parent" data-col="col-sm-4"> <div class="card"> <div class="card-part1"> <div class="img-card"> <img src="{{$bookmark_single->thumbnail}}" /> </div> <div class="card-content"> <h4 class="card-title"> {{ $bookmark_single->title }} </h4> <div class="card-desc"> {{ str_limit($bookmark_single->description, $limit = 50, $end = ' [...]') }} </div> </div> <div class="card-read-more"> <p><?php $tags = $bookmark_single->tags; $tag_list = explode(',', $tags); ?> #foreach ($tag_list as $tag) {{$tag}} #endforeach </p> <p class="card-user">- {{ $bookmark_single->bookmarker }}</p> <a class="v-link" target="_blank" href="{{ $bookmark_single->url }}">Visit the link</a> </div> <button type="button" class="btn btn-success btn-circle btn-lg btn-read-more"><i class="fa fa-chevron-right"></i></button> </div> <div class="card-part2 col-xs-0"> {{ print $bookmark_single->contents }} </div> </div> </div> #endforeach </div> {{ $bookmark->links() }} #endif
You can use flash() method to achieve this. Old Input In your search() method add $request->flash(). This will flash the current inputs in the session. And, next time you want retrieve it by $request->old('search'); Here's the complete search() method: public function search(Request $request){ $search_value = $_POST['search']; //assign the current value of search field if(!$search_value) //check if current value is not null, means this is new search or previous one { $search_value = $request->old('search'); //If search_value is null use the old value $request->search = $request->old('search'); //add the old value to current request so that it can be flashed } $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10); $request->flash(); //adding this request's search value to the session return view('public_bookmarks')->with('bookmark', $bookmarks); } Update: public function search(Request $request){ //$search_value = $_POST['search']; change this to use $request $search_value = $request->search; $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10); return view('public_bookmarks')->with('bookmark', $bookmarks)->withInput($request->only('search')); // flashed input to the view } Now, in your view you can get the value with old() method: {{ $bookmark->appends(['search' => old('search')])->links()}} Update 2: change your ajax call to use get method and point it to new address: type: "get", url: '/search' Add a route to handle search request as a get method: Route::get('/search', 'BookmarkController#search'); In your search() method use $request to get the search value: $search_value = $request->search;
How to solve logical issue with Laravel - Blade
I have $win->start, $article->from and $article->days Now I want to create HTML like this: if ($article->from) > (Carbon::now()->addDays($article->days)) { $r = Carbon::now()->addDays($article->days) if ($r > $win->start) { <div>get VOUCHER</div> } else { <div>ACTIVE</div> } else { $r = $article->from if ($r > $win-> start) { <div>get VOUCHER</div> } else { <div>ACTIVE</div> } } Please help me to solve this logical problem with proper Blade - Carbon code... How to create two if into blade?
I think this should do the trick: #foreach($wins as $win) <div class="row"> <div class="col-md-4"> <p>{{ date('d M Y', strtotime($win->start)) }}</p> </div> <div class="col-md-4 text-right"> <p><strong>{{$win->price}} </strong>euros</p> </div> #if($article->from > \Carbon\Carbon::now()->addDays($article->days)) {{ $r = \Carbon\Carbon::now()->addDays($article->days) }} #if($r > $win->start) <div>get VOUCHER</div> #else <div>ACTIVE</div> #endif #else {{ $r = $article->from }} #if($r > $win-> start) <div>get VOUCHER</div> #else <div>ACTIVE</div> #endif #endif </div> <hr style="margin:0px;"> #endforeach UPDATED: With your full snippet More info: https://laravel.com/docs/5.1/blade#control-structures
Output query object into view
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()
Optimization of Eloquent queries in Laravel 4
I've just recently started using Laravel and I've got a problem, that my page is really slow. The issue is that my queries are not ideal: ` <div class="container"> #foreach ($product as $single) <?php $tagy = $single->tags()->get(); echo "<div class='item"; foreach($tagy as $tag) { $premenna = Tags::where('id','=',$tag->tagID)->first(); echo " item".$premenna->id; } echo "'>"; ?> <div class="thumbnail porfolio"> <img class="image" src="<?=Croppa::url($single->destinationPath.'/'.$single->coverphoto, 250)?>" /> <div class="caption"> <div class="info"> <h4 style="font-weight: bold;">{{ $single->name }}</h5> <p style="font-size: 15px; margin-bottom: 0px;">{{ $single->city }}</p> <p class="date">{{ $single->date }}</p> </div> <div class="map-location"> <i class="fa fa-dot-circle-o fa-2x"></i> </div> <div class="tagy"> <?php foreach($tagy as $tag) { $variable= Tags::where('id','=',$tag->tagID)->orderBy('name')->first(); echo "<a class='hashtag tag".$variable->id."'>".$variable->name." </a>"; } ?> </div> </div> </div> </div> #endforeach </div>` This is my Controller function: protected function index() { $product = Product::where('active','=',1)->orderBy('date')->get(); $tagy = Tags::orderBy('name')->get(); return View::make('uvod',['product' => $product, 'tags' => $tagy]); } I've made one-to-many relationship between Product Class and ProductTag Class - that's the fucntion tags(); Could you please somehow help me optimizing the code (queries)? I would like to use #foreach function only once if possible - when i deleted the foreach functions inside the main one the page speed significantly improved. Thank you very much for any kind of help :)