How to add Pagination in laravel 5 - laravel-5

I need to show one row per page.
this my contoller class code
public function showcontent($id)
{
$story=storybook::find($id);
$content=storycontent::where('titleid', $story['id'])->paginate(1);
//return $content;
return view('storyBook/viewStory',compact('story','content'));
}
this my view class code
#foreach($content as $content)
<?php $a=null; ?>
<?php $b=null; ?>
<?php $a=$content->file1; ?>
<?php $b=$content->file2; ?>
<table style="height: 330px;width: 680px;margin-top: 120px;margin-left: 50px">
<tr>
<td style="width: 340px"><img src="/{{$a}}" alt="Mountain View" style="width:330px;height:300px;"></td>
<td style="width: 340px"><img src="/{{$b}}" alt="Mountain View" style="width:330px;height:300px;"></td>
</tr>
</table>
<br>
#endforeach
In here i get only 1st row, rest of the rows not visible..
I do not know how to add pagination.

#foreach($content as $content)
So first of all change the naming of vars for example:
#foreach($content as $entry)
<table style="height: 330px;width: 680px;margin-top: 120px;margin-left: 50px">
<tr>
<td style="width: 340px"><img src="/{{$entry->file1}}" alt="Mountain View" style="width:330px;height:300px;"></td>
<td style="width: 340px"><img src="/{{$entry->file2}}" alt="Mountain View" style="width:330px;height:300px;"></td>
</tr>
</table>
<br>
#endforeach
$content->links()
As you can see I've added $content->links() after foreach loop. This is the method of Paginator that print out the pagination navigation.

Related

Laravel 9 404 Error on GET Request updating database when clicking button

I was having another laravel question here. I was facing 404 error when clicking the button
Confirm Registration on the purpose of changing its database based on the item id on the view. Logically, I think my code should have work on this part.
Confirm Registration
However, it comes up 404 Error. Herby I include all the related code in the section.
Error
Frontend (To prove that the data passing is successful in the view)
web.php
Route::controller(ResidenceController::class)->group(function(){
Route::get('/residence/register', 'registration')->name('residence.register');
Route::get('/residence/search', 'SearchProperty')->name('residence.search');
Route::get('/registration/confirmation/{$uniquecode}', 'ConfirmRegistration')->name('registration.confirm');
});
The focus is on:
Route::get('/registration/confirmation/{$uniquecode}',
'ConfirmRegistration')->name('registration.confirm');
Controller
public function ConfirmRegistration($uniquecode){
$confirm = Properties::where('uniquecode', $uniquecode)->update(['status'=>'pending', 'tenant_id'=>Auth::user()->id]);
if($confirm){
session()->flash('alert-success','Successfully Applied');
}
else{
session()->flash('alert-warning','Error Occured');
}
}
register.blade.php
#foreach ($toSearch as $key=>$view)
<div class="card" style="width: 50rem;">
<div class="card-body">
<div class="container">
<div class="row">
<div class="col-md-3">
<img src="{{ (!empty($tenant->unit_image))? url('uploads/properties/'.$tenant->unit_image):url('uploads/properties/default.jpg') }}" class="img-fluid" alt="unit image" >
</div>
<div class="col-md-9">
<table>
<tr>
<td style="width: 20%">Property</td>
<td style="width:10%; text-align: center">:</td>
<td>{{$view->name}}</td>
</tr>
<tr>
<td>Address</td>
<td style="text-align: center">:</td>
<td>{{$view->address1}},{{$view->address2}},{{$view->city}}</td>
</tr>
<tr>
<td>Landlord</td>
<td style="text-align: center">:</td>
<td>{{$view->landlord->name}}</td>
</tr>
<tr>
<td>Contact No</td>
<td style="text-align: center">:</td>
<td>{{$view->landlord->phone_number}}</td>
</tr>
<tr>
<td>Status</td>
<td style="text-align: center">:</td>
<td>{{$view->status}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<br>
<!--To confirm registration-->
#if( $view->status != 'approved')
Confirm Registration
#endif
</div>
#endforeach
The focus is on:
#if( $view->status != 'approved')
uniquecode) }}" class="btn btn-primary">Confirm
Registration
#endif
Database:
you need to use post method for inserting data (register),
anyway, change your route parameter $uniquecode to uniquecode.
and when you use route second argument is a compact, use
route('registration.confirm',['uniquecode' => $view->uniquecode]);

pagination in laravel 5.3 when count of pages = 1

I have cities table and it has 7 cities I have a view page to display these cities with 10 cities per page
Controller:
$cities = City::orderBy('id', 'desc')->paginate(10);
return view('cities.home', compact('cities'));
View:
<div class="table-responsive panel panel-sky">
<table class="table table-striped table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>Created At</th>
<th>Action</th>
</tr>
#foreach($cities as $city)
<tr id="product{{$city->id}}">
<td>{{$city->id}}</td>
<td>{{$city->name}}</td>
<td>{{ $city->created_at }}</td>
<td>
<i class="glyphicon glyphicon-edit action-icon"></i>
<a type="button" class="pointer" data-toggle="modal" data-target="#deleteModal" data-type="btn-danger" data-action="delete-product" data-id="{{ $city->id }}" data-msg="Are you sure you want to delete this city?" data-title="Confirm Deletion">
<span class='glyphicon glyphicon-remove action-icon'></span>
</a>
</td>
</tr>
#endforeach
</table>
<div>
<div class="col-md-12"><?php echo $cities->render(); ?> </div>
but the issue is the paginate render is displayed with page#1, this issue occurred in laravel5.3 and did not find in laravel5.2
If you want to render pagination in your own way, you can use paginator methods. For example, to check if there is just one page, you could try:
#if ($cities->total() > 10)
// Render pagination
#endif

my modal in image responsive show only first row image from database

I tried to make responsive image for all image thumbnail from my database.
the thumbnail is showing, but when I clicked it, all of it display the first image only. why this is happening?
here is a few line code from view file
<div data-toggle="modal" data-target="#myModal">
<table id="dynamic-table" method="post" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<img src="<?php echo base_url().'uploads/'.$row->file ?>" height="75">
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" class="img-responsive">
</div>
</div>
</div>
</div>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#myModal').on('show.bs.modal', function(e) {
var image = $(e.relatedTarget).attr('src');
$(".img-responsive").attr("src", image);
});
});
</script>
</div>
here is a code from model
<? php
class Model_data_cuti extends CI_Model {
public
function getdata() {
$hasil = $this - > db - > query("SELECT * FROM form");
return $hasil;
}
public
function create($data_form) {
$this - > db - > insert('form', $data_form);
}
public
function find($no) {
$hasil = $this - > db - > where('no', $no) - > limit(1) - > get('form');
if ($hasil - > num_rows() > 0) {
return $hasil - > row();
} else {
return array();
}
}
} ?>
and this is from controller
<? php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data_cuti extends CI_Controller {
function __construct() {
parent::__construct();
$this - > load - > model('model_data_cuti');
}
public
function index() {
$data['hasil'] = $this - > model_data_cuti - > getdata();
$this - > load - > view('data_cuti_view', $data);
}
} ?>
this is the result page
enter image description here
look at the file column. if i clicke th image, it only display image from first row. when i click others image, it display the first image too.
I see there in your view code, you tried to get the image by
e.relatedTarget for modify your modal.
Mistake :
You do loop the modals, but declare the same id, it was write such as :
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static" aria-labelledby="myModalLabel" aria-hidden="true">
If you inspect the element in your browser you will find that you have a lot of modals with the same id.
Approach Solution :
Just move out the modals from the loop condition, and try to modify it by onclick event listener inside of the true image of yours.
Code Provided ( Untested ) :
<td> <img onclick="photoModal('<?php echo base_url().'uploads/'.$row->file ?>');" src="<?php echo base_url().'uploads/'.$row->file ?>" height="75">
<script>
function photoModal(imgPath){
$('#myModal < div < div < div <img').attr("src",imgPath);
$('#myModal').modal('show');
}
</script>
Explanation
So, while you clicked the image, it will trigger function photoModal() to send the src attribute to the Modal and proccess it to change the src image inside of Modals
I already trick it with some code. My purpose is to make the image larger, so the user can read it.
ps: I already removed the modals.
here is my code in view
<table id="dynamic-table" method="post" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<a class="thumbnail" href="<?php echo base_url().'uploads/'.$row->file ?>">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" width="50" height="50">
</a>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
if you have another way to make the modal working right, just tell me. thanks. ^^,

Laravel Trying to get property of non-object when i get data

i get this error when i get section data and the problem with section result part
the book part and author part working good
when i remove section part it working with out section_name
Laravel Framework version 5.1.25 (LTS)
Trying to get property of non-object (View: C:\xampp\htdocs\lib\resources\views\libraryViewsContainer\summary.blade.php)
my Book Model
class Book extends Model
{
public function section()
{
return $this->belongsTo('App\Section','id');
}
public function author()
{
return $this->belongsToMany('App\Author','books_authors_relationship','book_id','author_id')->withTimestamps();
}
my BooksController
public function summary(){
$all_results = Book::with('section')->with('author')->get();
return view('libraryViewsContainer.summary',compact('all_results',$all_results));
}
my summary blade
<div class="container">
<h1 class="well text-center">Library Summary</h1>
<table class="table">
<tr>
<th style="width: 25%">Section Name</th>
<th style="width: 25%">Book Title</th>
<th style="width: 25%">Book Description</th>
<th style="width: 25%">Authors</th>
</tr>
#foreach($all_results as $bookModel)
<tr>
<td>
<a href="/section/{{$bookModel->section->id}}">
<span class="label label-info">{{$bookModel->section->section_name}}</span>
</a>
</td>
<td>
{{$bookModel->book_title}}
</td>
<td>
{{$bookModel->book_description}}
</td>
<?php $authors = $bookModel->author; ?>
<td>
#foreach($authors as $author)
<a href="/author/{{$author->id}}">
<span class="label label-danger">{{$author->first_name}} {{$author->last_name}}</span>
</a>
#endforeach
</td>
</tr>
#endforeach
</table>
</div>
#stop
In controller, method "with" can be written like $all_results = Book::with('section', 'author')->get();
Method compact() takes the variable names, not the variables.
return $all_results before view to see your results to get whether you're getting the values correctly or not.
the error was in Book model
i changed
public function section()
{
return $this->belongsTo('App\Section','id');
}
to
return $this->belongsTo('App\Section');
and it worked thanks for all help me

CodeIgniter continuing a foreach loop after another one has been run through

I have 2 separate foreach loops being executed in the following code. At the end I have $row->websiteURL outputting right after the second foreach loop. The problem is $row->websiteURL is part of the first loop. So I get the following error when I run the code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$websiteURL
Filename: views/projects.php
Line Number: 135
" target="_blank">
How would I go about continuing the first foreach loop after the second foreach is complete?
<table style="width: 41%">
<?php $query = $this->db->query("SELECT * FROM projects ORDER BY idprojects DESC");
foreach ($query->result() as $row) { ?>
<tr>
<td style="height: 15px">
<div class="auto-style2">
<em><span class="auto-style1"><?=$row->projectName?></span></div>
<table cellpadding="5" style="width: 535px; height: 66;">
<tr>
<td class="box1" style="height: 49px; width: 800px;">
<table cellpadding="0" cellspacing="0" style="width: 466px; height: 185px">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" style="width: 225px">
<em>
<table style="width: 100%">
<tr class="box1">
<td class="innerbox" style="height: 88px"><em><span class="text1">Project name</span>:
<span class="underlined-link"><?=$row->projectName?></span><br>
<span class="text1">Description</span>: <?=$row->projectDesc?><br><span class="text1">Start
date</span>: <?=$row->startDate?><br><span class="text1">Finised date</span>:
<?=$row->finishedDate?><br><span class="text1">Created for</span>:
<?=$row->createdFor?><br><span class="text1">Contributers</span>:
<span class="underlined-link"><?=$row->contributors?></span></em></td>
</tr>
</table>
</em></td>
<td style="width: 12px"> </td>
<td valign="top" style="height: 185px; width: 229px">
<em>
<?php $query = $this->db->query("SELECT * FROM screenshots ORDER BY idscreenshot DESC");
foreach ($query->result() as $row) { ?>
<img alt="" src="<?=$row->screenshotURI?>" width="231" height="187"></em> </td>
</tr>
<?php } ?>
</table>
<br>
<a style="text-decoration:none" href="<?=$row->websiteURL?>" target="_blank"><div class="link1">
View This Product</div></a>
</td>
</tr>
</table>
</em></td>
</tr>
<?php } ?>
</table>
You need to change your variables in the second foreach like this:
$query = $this->db->query("SELECT * FROM screenshots ORDER BY idscreenshot DESC");
foreach ($query->result() as $row2) { ?>
<img alt="" src="<?=$row2->screenshotURI?>" width="231" height="187"></em> </td>
The $row variable is still in scope and it's looking in the first query for this. If you are doing an inner foreach loop you can't name the "item" variable the same as the outter foreach.
so more to the point:
foreach($arr as $row):
foreach($arr as $row2):
//do something
endforeach;
endforeach;
Back to the bigger issue. You should not being querying data in the views. Data is manipulated in the Model. The controller loads the model, uses the model to get the information, then passes the info to the view. It can be a difficult concept to grasp if you're new to MVC. Once you get this working, focus on moving these queries into their own functions in your model and retrieving the information that way.

Resources