laravel blade - show only one data from database - laravel

I have a table with multiple fields called "skills" . how do I show only one(first) data associated with "skills" .
#foreach ($others as $other )
#if ($other->type == 'skills')
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
#endif
#endforeach
in my controller-
public function index()
{
$sliders = Slider::latest()->with('service')->get();
$clients = OurClient::all();
$galleries = Gallery::all();
$services = Service::all();
$others = Others::all();
return view('frontend.pages.home',compact('services','galleries','clients','others','sliders'));
}
this is showing the same data twice. I want only the first data

I solved it. changed the blade file
<div class="row">
<?php $count = 0; ?>
#foreach ($others as $other )
#if ($other->type == 'skills')
<?php if($count == 1) break; ?>
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
<?php $count++; ?>
#endif
#endforeach
</div>

Related

Dynamic Carousel In Laravel not displaying proper data

<div class="container mt-4 mb-4">
<div class="row">
<div class="col-lg-12">
<div id="blogCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
#foreach($reviews as $items)
<div class="carousel-item #if($loop->first) active #endif">
<div class="row">
#foreach($reviews as $review)
<div class="col-lg-4 ">
<a>
<div class="home1-img mt-3">
<?php
for($i=1;$i<6;$i++){
$check = '';
if($review->number_of_stars >= $i){
$check = 'checked';
}
echo '<span class="fa fa-star '.$check.'"></span>';
}
?>
<div class="home1-text mt-1" style="margin-bottom:30px;">
<p>{!! Illuminate\Support\Str::limit($review->customer_review,100) !!}</p>
<h5 class="text-color font-weight-bold">{{ $review->customer_name }}</h5>
</div>
</div>
</a>
</div>
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
I am getting all the records in all sides but I want 3 records in 1st slide then 3 records in 2nd slide and so on. I have tried many times but I am not able to fix it.

i want to get a video id so that it can help me get comments related to that video in my site, it only displays a video but not comments

The biggest issue here is how to get these comments using the videoid in my bladeview -- my blade
<div id="videoid">{{$id->id}}</div>
<div id="videotitle">{{$id->title}}</div>
#php($comments = \App\comments::where('video_id','{{$id->id}}')->get() )
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
My controller works well --
mycontroller
public function watch($id)
{
return view('video/watch', compact('id'));
}
This is your blade. Thought there's no need to query from the view but rather from the controller.
<div id="videoid">{{ $video->id }}</div>
<div id="videotitle">{{ $video->title }}</div>
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
Then from your controller you can fetch your data and pass them to the view using Laravel's magic method:
public function watch($video_id)
{
$video = Video::whereId($video_id)->first();
$comments = \App\comments::where('video_id',$video_id)->get()
return view('video/watch',[
'video'=>$video,
'comments'=>$comments
]);
}
in controller
public function watch($id)
{
$video = Video::with('comments')->find($id);
$comments = \App\comments::where('video_id','{{$video->id}}')->get()
return view('video/watch', compact('video','comments'));
}
in view
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
#foreach($comments as $comment)
<div id="username">
<div id="con"><h6>{{$comment->id }}</h6></div>
<div id="con"><h6>{{$comment->user_id }}</h6></div>
<div id="con">{{$comment->created_at }}</div>
</div>
<div id="comment">{{$comment->comment }}</div>
#endforeach
</div>
You seem to be missing a key part of using Eloquent.
Relationships.
// Video model:
public function comments()
{
return $this->hasMany(Comment::class);
}
// Comment model:
public function video()
{
return $this->belongsTo(Video::class);
}
// Controller code: (Switched to [Route-model binding][2])
public function watch(Video $video)
{
return view('video.watch', [
'video' => $video
]);
}
// Update routes for Route-model-binding
Route::get('/watch/{video}', 'VideoController#watch')->name('video.watch');
// View:
<div id="videoid">{{$video->id}}</div>
<div id="videotitle">{{$video->title}}</div>
<div id="displaycomment">
#foreach ($video->comments as $comment)
<div id="username">
<div id="con">
<h6>{{$comment->id }}</h6>
</div>
<div id="con">
<h6>{{ $comment->user_id }}</h6>
</div>
<div id="con">$comment->created_at</div>
</div>
<div id="comment">$comment->comment</div>
#endforeach
</div>
Route-model binding

gallery images does not get updated in database

I have an event management company website. https://redcarpetevents.in. the only issue is i cannot update the image gallery. i can upload new images, that works fine. but when i edit the images already in the database, it is not getting updated.
routes.php
Route::get('admin/editGalleryForm/{type}/{editId}', array('as'=>'editGalleryForm','before'=>'authAdmin','uses'=>'AdminController#editGalleryForm'));
Route::post('admin/editGallery', array('as'=>'editGallery','before'=>'csrf|xss_clean|authAdmin','uses'=>'FormController#editGallery'));
Model.php
public static function editGallery($data){
$gallery = Gallery::find($data['editId']);
$gallery->type = $data['type'];
$gallery->tag = $data['tag'];
$gallery->position = $data['order'];
if (isset($data['status'])) {
$gallery->status = $data['status'];
}else{
$gallery->status = 'off';
}
if (isset($data['home'])) {
$gallery->home = $data['home'];
}else{
$gallery->home = 'off';
}
if (isset($data['title'])) {
$gallery->title = $data['title'];
}
if($gallery->update()){
return true;
}else{
return false;
}
}
public static function getGalleryCounts(){
$galleryCounts = array();
$galleryCounts['all'] = count(Gallery::all());
$galleryCounts['allActive'] = count(Gallery::where('status','=','on')->get());
$galleryCounts['allInActive'] = count(Gallery::where('status','=','off')->get());
$galleryCounts['personal'] = count(Gallery::where('type','=','1')->get());
$galleryCounts['personalActive'] = count(Gallery::where('type','=','1')->where('status','=','on')->get());
$galleryCounts['personalInActive'] = count(Gallery::where('type','=','1')->where('status','=','off')->get());
$galleryCounts['corporate'] = count(Gallery::where('type','=','2')->get());
$galleryCounts['corporateActive'] = count(Gallery::where('type','=','2')->where('status','=','on')->get());
$galleryCounts['corporateInActive'] = count(Gallery::where('type','=','2')->where('status','=','off')->get());
return $galleryCounts;
}
Admincontroller.php
public function editGalleryForm($type,$id){
$data['id'] = $id;
$data['type'] = $type;
$data['galleryCounts'] = Gallery::getGalleryCounts();
$data['image'] = Gallery::getGalleryById($id);
$data['personalTags'] = GalleryTag::getPersonalActive();
$data['corporateTags'] = GalleryTag::getCorporateActive();
return View::make('admin.editGalleryForm', $data);
}
Formcontroller.php
public function editGallery(){
$type = Input::get('type');
$editId = Input::get('Id');
$filename=Input::get('image');
$rules = array(
'order'=>'Numeric');
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
$data['type'] = $type;
$data['Id'] = $editId;
return Redirect::route('editGalleryForm',$data)->withInput()->withErrors($validation);
}
if ($validation->passes()) {
$fileName='';
if (Input::hasFile('image')) {
$name = Input::file('image')->getClientOriginalName();
$destinationPath = 'assets/img/gallery/';
$data['Id'] = $Id;
$fileName = $name;
Input::file('image')->move($destinationPath, $fileName);
//Gallery::convertImage($destinationPath,$fileName);
}
$editGallery = Gallery::editGallery(Input::all());
if ($editGallery) {
$data['success'] = 'Gallery Image Updated!';
$data['type'] = $type;
return Redirect::route('newGallery',$data);
}
else
{
$data['failure'] = 'Operation failed!';
$data['type'] = $type;
return Redirect::route('newGallery',$data);
}
}
}
this is the form
#extends('admin.layouts.master')
#section('pageHeader')
<h2>Edit Gallery Image</h2>
<ol class="breadcrumb">
<li>{{HTML::linkRoute('adminHome','Dashboard')}}</li>
<li>{{HTML::linkRoute('newGallery','Gallery',array('type'=>$type))}}</li>
<li class="active">Edit Gallery Image</li>
</ol>
#stop
#section('content')
<div class="cl-mcont">
<div class="status_bar">
<div class="butpro butstyle {{($type=='1')? 'status_active':''}}"><a href="{{URL::route('newGallery',array('type'=>'1'))}}">
<div class="sub"><h2>PERSONAL</h2><span id="total_clientes">{{$galleryCounts['personal']}} Image{{($galleryCounts['personal'] > 1)? 's':''}}</span></div>
<div class="stat">{{$galleryCounts['personalActive']}} <i style="color:#00FF00" class="fa fa-check"></i> {{$galleryCounts['personalInActive']}} <i style="color:#FF0000" class="fa fa-times"></i></div>
</a></div>
<div class="butpro butstyle {{($type=='2')? 'status_active':''}}"><a href="{{URL::route('newGallery',array('type'=>'2'))}}">
<div class="sub"><h2>CORPORATE</h2><span id="total_clientes">{{$galleryCounts['corporate']}} Image{{($galleryCounts['corporate'] > 1)? 's':''}}</span></div>
<div class="stat">{{$galleryCounts['corporateActive']}} <i style="color:#00FF00" class="fa fa-check"></i> {{$galleryCounts['corporateInActive']}} <i style="color:#FF0000" class="fa fa-times"></i></div>
</a></div>
</div>
<div class="row">
<div class="col-md-12">
{{Form::open(array('action'=>'editGallery','method'=>'POST','files'=>true,'class'=>'form-horizontal'))}}
<div class="form-group">
{{Form::label('forType', 'Type', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
{{Form::select('type', array('1'=>'personal','2'=>'corporate'), ($image->type=='2')? '2':'1', array('class'=>'form-control','onChange'=>'tagChange(this)'))}}
<div class="color-danger">{{$errors->first('type')}}</div>
</div>
</div>
<div class="form-group">
{{Form::label('forTag', 'Tag', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
<select class="form-control" id="tags" name="tag">
#if($type == 2)
#foreach($corporateTags as $tag)
<option {{($image->tag == $tag->id)? 'selected':''}} value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
#else
#foreach($personalTags as $tag)
<option {{($image->tag == $tag->id)? 'selected':''}} value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
#endif
</select>
<div class="color-danger">{{$errors->first('tag')}}</div>
</div>
</div>
<div class="form-group">
{{Form::label('forTitle', 'Title', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
{{Form::text('title', $image->title, array('class'=>'form-control','placeholder'=>'Image Title'))}}
<div class="color-danger">{{$errors->first('title')}}</div>
</div>
</div>
<div class="form-group">
{{Form::label('forImage', 'Gallery Image', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
{{Form::file('forimage', array('class'=>'form-control','placeholder'=>'image'))}} (600 X 400)px
<div class="color-danger">{{$errors->first('image')}}</div>
</div>
</div>
<div class="form-group">
{{Form::label('', '', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
<div class="col-md-2 padding photo">
<img src="{{asset('assets/img/gallery/'.$image->image)}}" width="200px" height="120px">
</div>
</div>
</div>
<div class="form-group">
{{Form::label('forOrder', 'Order', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
{{Form::text('order', $image->position, array('class'=>'form-control','placeholder'=>'Image Order'))}}
<div class="color-danger">{{$errors->first('order')}}</div>
</div>
</div>
<div class="form-group">
{{Form::label('forStatus', 'Publish Status', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
<div class="switch" data-on="success">
<input type="checkbox" name="status"{{($image->status=='on')? 'checked':''}}>
</div>
</div>
</div>
<div class="form-group">
{{Form::label('forShow', 'Show on Homepage', array('class'=>'col-sm-2 control-label'))}}
<div class="col-sm-6">
<div class="switch" data-on="success">
<input type="checkbox" name="home"{{($image->home=='on')? 'checked':''}}>
</div>
</div>
</div>
{{Form::text('editId', $image->id, array('style'=>'display:none'))}}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
{{Form::submit('Update Gallery', array('class'=>'btn btn-primary'))}}
{{HTML::linkRoute('newGallery','Cancel',array('type'=>$type),array('class'=>'btn btn-default'))}}
</div>
</div>
{{Form::token().Form::close()}}
</div>
</div>
</div>
#stop
#section('customScripts')
<script type="text/javascript">
function tagChange(element){
console.log(element.value);
$('#tags').empty();
$.ajax({
url:'{{URL::route("getTagsAjax")}}',
type:'POST',
data:{'type':element.value},
dataType:'JSON',
success:function(data){
console.log(data);
for (var i = 0; i <= data.length; i++) {
$('#tags').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
};
}
});
}
</script>
#stop

How to display the array element to view

I have a problem regarding the return view('doctor_index', compact('result') on my contoller
here is my controller
public function index()
{
$data = Auth::user()->patient;
$data = explode(',', $data);
foreach ($data as $key => $datas) {
$result = DB::table('patients')->where('id', $datas)->get();
foreach ($result as $key => $res) {
$output = ' <h4><b>'. $res->patient_name .'</b></h4>
</p>Birthday: <strong>'. $res->post_date .'</strong> Age: <strong>'. $res->patients_age .'</strong></p>
<p>Address: <strong>'. $res->patient_address .'</strong></p><br><br>';
}
echo $output;
// return view('doctor_index', compact('output'));
}
}
at first i used echo $output; this is what it displayed
Output of echo $output:
Now if i use the return view on the controller it displays
Output of the return view:
as you can see when i use the return view it only display the first element
My question is how can i display all of the elements to my view using the return view
my view code:
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h2><b>{{ Auth::user()->name }} </b></h2>
<p>Email: <strong> {{ Auth::user()->email }} </strong></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h3>Patients</h3>
</div>
<div class="col-md-12">
<div class="container">
<?php echo $output ?>
</div>
</div>
</div>
</div>
</div>
With functions and methods once it reaches the return statement it won't continue through the other loops. Instead you should pass the array to the view and loop over the array within your view. So your controller would look something like this:
public function index()
{
$data = Auth::user()->patient;
$data = explode(',', $data);
$responseData = [];
foreach ($data as $key => $datas) {
$result = DB::table('patients')->where('id', $datas)->get();
foreach ($result as $key => $res) {
$responseData[] = ' <h4><b>'. $res->patient_name .'</b></h4>
</p>Birthday: <strong>'. $res->post_date .'</strong> Age: <strong>'. $res->patients_age .'</strong></p>
<p>Address: <strong>'. $res->patient_address .'</strong></p><br><br>';
}
}
return view('doctor_index', compact('responseData'))
}
And then your blade template would look something like this.
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h2><b>{{ Auth::user()->name }} </b></h2>
<p>Email: <strong> {{ Auth::user()->email }} </strong></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h3>Patients</h3>
</div>
<div class="col-md-12">
<div class="container">
#foreach($responseData as $output)
{!! $output !!}
#endforeach
</div>
</div>
</div>
</div>
</div>

Codeigniter - Inserting Ads after each x rows

I want to insert advertisements after each X rows returned from database.
My Controller looks like this:
class Home extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database();
$this->load->library('parser');
$this->load->helper('url');
$this->load->helper('download');
$this->load->model('Categorii');
$this->load->model('Comentarii');
}
public function index($pages = ""){
$c_ar = $this->Categorii->iaCategorii();
$sql = (!empty($pages)) ? "SELECT * FROM bancuri WHERE categorie = '" . $c_ar[$pages] . "'" : "SELECT * FROM bancuri";
$dbQ = $this->db->query($sql);
$renderData = array("bancuri" => $dbQ->result_array(), "ad" => "<img src='http://placehold.it/350x150'>"); //Se stocheaza datele intr-o matrice
$this->parser->parse('pages/bancuri',$renderData);
}
And view looks like this:
<div class="col-lg-8 col-sm-12 col-xs-12">
{bancuri}
{ad}
<div class="corgi_feed_well">
<div class="individual_feed_item">
<div class="feed_item">
<div class="feed_body">
<div class="row">
<!--
<audio controls>
<source src="<?php echo $this->config->item('base_url'); ?>resources/mp3/{mp3}" type="audio/mpeg">
</audio>
-->
</div>
<div class="row">
<div class="feed_profile_pic">
<span class="label label-info">Rating: {rating}</span>
<div class="clearfix"> </div>
<button type="button" class="btn btn-success">Like</button>
</div>
<div class="feed_text">
<p class="well">{banc}</p>
</div>
</div>
</div>
<div class="comment_area">
<p>Adauga un comentariu</p>
<form>
<textarea rows="3" class="span6"></textarea><br/>
<input type="submit" class="btn" />
</form>
</div>
<hr class="feed_hr" />
<div class="bottom_meta">
<div class="row">
<div class="bottom_left">
<div class="share_wrapper">
<div class="share"><i class="icon-heart"></i></div>
<div class="share_hidden">
<ul class="hover_heart">
<span class="internal_heart"><i class="icon-heart"></i> Trimite bancul pe</span>
<div class="social_links">
<li><span><i class="icon-twitter"></i> Twitter</span></li>
<li><span><i class="icon-facebook"></i> Facebook</span></li>
<li><span><i class="icon-pinterest"></i> Pinterest</span></li>
</div>
</ul>
</div>
</div>
</div>
<div class="bottom_left">
Descarca <span class="label label-primary">{descarcari} descarcari</span>
{comentarii} comentarii<br>
</div>
<div class="bottom_right">
{autor} <span>|</span> Adauga comentariu <span>|</span> {data}
</div>
</div>
</div>
</div>
</div>
</div>
{/bancuri}
The problem is that at the moment, it displays the ad (simple image atm) for every database row.
Regardless of the code you posted, In short, you just add a counter and use modulo.
so something like
$arr = range('a','z');
$n = 0;
foreach ($arr as $item)
{
if($n % 4 ==0) // 4 equals to the interval between ads
echo 'print banner';
$n++ ;
}
this will print an ad, or whatever you want, every 4 lines, play around with the numbers as you see fit and add a zero check if you don't like ads to appear in the first line.

Resources