I'm using laravel 5.6 and try to add pagination to bootstrap datatable. But it's not working.I got error.Please help me to find mistake I did.
Error
Method Illuminate\Database\Eloquent\Collection::links does not exist.
designation.blade.php(View)
#if(count($designations)>=1)
<table class="table table-dark" id="designationTable">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Designation Type</th>
<th scope="col">Status</th>
<th scope="col">Create At</th>
<th scope="col">Action</th>
</tr>
</thead>
#foreach ($designations as $designation)
<tbody>
<tr>
<th scope="row">{{$designation->id}}</th>
<td>{{$designation->designation_type}}</td>
<td>{{$designation->status}}</td>
<td>{{$designation->created_at}}</td>
<td>
<button type="" class="btn btn-secondary" id="btn_update_designation">Update</button>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $designations->links() }}
#endif
DesignationController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use App\Designation;
class DesignationController extends Controller
{
public function index()
{
$designations = Designation::all();
return view('pages.designation')->with('designations',$designations);
}
}
You should use the paginate() method in your controller file.
public function index()
{
$designations = Designation::paginate(10);
return view('pages.designation')->with('designations',$designations);
}
Paginating Eloquent Results
You may also paginate Eloquent queries. In this example, we will
paginate the User model with 15 items per page. As you can see, the
syntax is nearly identical to paginating query builder results:
$designations = Designation::paginate(15);
You may call paginate after setting other constraints on the query,
such as where clauses:
$designations = Designation::where('column', 'value')->paginate(15);
You should use the paginate() method in your controller file.
public function index()
{
$designations = Designation::paginate(15);
return view('pages.designation')->compact('designations',$designations);
}
Related
Can someone help me with this problem?
I have 2 tables in phpMyAdmin
the first one where the with the department_id of the person
The second one where you can see the department_id and then the department_name
I have a table with this code:
<div class="container">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Department Name</th>
</tr>
</thead>
<tbody>
#foreach ($employee as $name)
<tr>
<td>{{ $name->first_name }}</td>
<td>{{ $name->last_name }}</td>
<td>{{ $name->!!!IDK YET!!! }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Basically, for each employee I want to grab the department_id and check what the matching department_name
Extra code that makes the table on my page work:
EmployeeController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employee;
use Illuminate\Http\Request;
class EmployeeController extends Controller
{
public function index()
{
$employee = Employee::all();
return view('employee.index', compact('employee'));
}
}
Employee.php (Model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $employees = 'employees';
protected $EmployeeValues = ['first_name','last_name'];
protected $departments = 'departments';
protected $DepartmentValues = ['department_id','department_name'];
}
Okey so I found the answer after trying a lot of things.
My new code is:
EmployeeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class EmployeeController extends Controller
{
public function index()
{
$employee = DB::table('employees')
->leftJoin('departments','employees.department_id',"=",'departments.department_id')
->get();
return view('employee.index', compact('employee'));
}
}
Employee (Model)
Wasn't necessary anymore
index.blade.php
<div class="container">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Department Name</th>
</tr>
</thead>
<tbody>
#foreach ($employee as $name)
<tr>
<td>{{ $name->first_name }}</td>
<td>{{ $name->last_name }}</td>
<td>{{ $name->department_name }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
This here is my join controller. There is no error but the problem is that the data is not being shown in the table. The headers from blade.php file are being shown but the data is not being fetched or shown in the table.
My Join Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class JoinController extends Controller
{
//
public function report()
{
$data = DB::table('pmedicalreadings')
->join('medrecords','medrecords.id','=','pmedicalreadings.id')
->join('payments','payments.id','=','pmedicalreadings.id')
->select('pmedicalreadings.bp','pmedicalreadings.temp','pmedicalreadings.ecg','medrecords.prerecord','payments.ptype','payments.tpayment','payments.rpayment')
->get();
return view('report',compact('data'));
}
}
My Routes from web.php:
Route::post('/report','JoinController#report');
Route::get('/report', function () {
return view('/report');
});
My report.blade.php file:
<center><h1>Patient Report</h1></center>
<form >
<table class="table table-bordered table-striped">
<tr class="bg-danger text-light">
<th>Patient Blood Pressure Reading</th>
<th>Patient Temperature Reading</th>
<th>Patient ECG Reading</th>
<th>Patient Medical Record</th>
<th>Payment Type</th>
<th>Total Payment</th>
<th>Remaining Payment</th>
</tr>
#if(isset($data))
#foreach($data as $row)
<tr class="bg-secondary text-light">
<td>{{$row->bp}}</td>
<td>{{$row->temp}}</td>
<td>{{$row->ecg}}</td>
<td>{{$row->prerecord}}</td>
<td>{{$row->ptype}}</td>
<td>{{$row->tpayment}}</td>
<td>{{$row->rpayment}}</td>
</tr>
#endforeach
#endif
</table>
</form>
Here is the output I'm getting,
The problem is in your web routes. Your get request doesnt return any data. It just returns the blade. You return the data in the post request. When you access the url your are making a get request
I have two different tables, answers table and question table, i need to get question ID and answer selected by a user which i have managed to retrieve, my problem is from the same form view i need to retrieve the correct answer and the point that i created in question table.From my view the first two column are retrieved from answers table, therefore i need the second two column from question table to be retrieved
please anyone who can help me, below is how i have tried to do
Here is my view:
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">Question No</th>
<th scope="col">Answer</th>
<th scope="col">Correct Answer</th>
<th scope="col">Marks</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($results as $data)
<tr>
<td>{{$i++}}</td>
<td>{{$data->question_id}}</td>
<td>{{$data->answer}}
<td>{{$data->qns_ans}}
<td>{{$data->qns_point}}
</tr>
#endforeach
</tbody>
</table>
Here is my controller
public function index()
{
$results = Answers::all();
$qns = Questions::all();
return view('test.result')
->with('results', $results)
->with('qns', $qns);
}
first you need to make relation one to many between questions and answers
check first that answers table has question_id column
then add this method to your Question Model to make relation
function answers(){
return $this->hasMany(\App\Answer::class);
}
So now you can easily from an instance of Question Model say that
$question = Question::first()->get();
dd($question->answer);
you will see now when you dump it, it will give you the answers that belongs to this question
Here is how I did to query my two tables data
This is my controller
public function index()
{
$results=Answers::all();
$qns = Questions::all();
return view('test.result')->with('results',$results)
->with('qns',$qns);
}
This is my view
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th>S/N</th>
<th scope="col">Question No</th>
<th scope="col">Answer</th>
<th scope="col">Correct Answer</th>
<th scope="col">Marks</th>
</tr>
</thead>
<tbody>
<?php $i=1; ?>
#foreach($results as $data)
<tr>
<td>{{$i++}}</td>
<td>{{$data->question_id}}</td>
<td>{{$data->answer}} </td>
<td>{{$data->question['ans']}} </td>
<td>{{$data->question['point']}}
</tr>
#endforeach
</tbody>
</table>
Here is my model
class Answers extends Model
{
protected $fillable = ['question_id','answer'];
public function question(){
return $this->belongsTo(Questions::class);
}
}
class Questions extends Model
{
protected $fillable =
['question_name','opt1','opt2','opt3',
'opt4','ans','point'];
public function answer(){
return $this->hasOne(Answer::class);
}
}
I am trying to get the data from two different tables and display it on one page, i means at one view. But only one function working on same time while the other not and vice versa, Both the functions work seperately but not combinely. Please help. my code is give.
PdfContoller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use PDF;
use Auth;
class PdfController extends Controller
{
public function personalpdf()
{
if(\Auth::check()){
$user = \Auth::user();
return view('pdf/personalpdf',compact('user'));
}
else
{
return view('pdf/personalpdf');
}
}
public function personalphd()
{
$user_id = Auth::user()->id;
$result['result'] = DB::table('education')->where('user_id' ,'=', $user_id)->get();
if(count ($result)>0){
return view('pdf/personalpdf',$result);
}
else
{
return view('pdf/personalpdf');
}
}
}
My controller contains the above two functions for fetching data from two different tables.
My view file is personalpdf.blade.php is given here.
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#if(isset($user))
<tr>
<th>Name</th>
<td>{{ $user ->tname}}</td>
</tr>
<tr>
<th>Father Name</th>
<td>{{ $user ->fname}}</td>
</tr>
<tr>
<th>Contact Number</th>
<td>{{ $user ->phone}}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ $user ->email}}</td>
</tr>
<tr>
<th>Official Email</th>
<td>{{ $user ->off_email}}</td>
</tr>
#endif
</table>
<div class="text text-success text-center">
PHD Research
</div>
<table class="table">
<tr>
<th>
PHD Research Area
</th>
<th>University</th>
<th>Country</th>
</tr>
#foreach($result as $value)
<tr>
<td>{{$value->research_area}}</td>
<td>{{$value->univ}}</td>
<td>{{$value->country}}</td>
</tr>
#endforeach
</table>
#endsection
My routes is given here.
Route::get('pdf/personalpdf','PdfController#personalpdf');
Route::get('pdf/personalpdf','PdfController#personalphd');
Route::get('/personal','PdfController#personal');
Actually I want to make a view file where I can see all the personal details of current login user. And below it I want to display his PHD research details getting from education table. But I am getting only one result on a same time. I means if I got personal detail of login user then phd detail not showing, if I get phd details it gives me all the phd details, not of current login user.
Please help.
class PdfController extends Controller
{
public function personalpdf()
{
if(\Auth::check()){
$user = \Auth::user();
$user_id = $user->id;
$result = DB::table('education')->where('user_id' ,'=', $user_id)->get();
return view('pdf/personalpdf',compact('user', 'result'));
}else {
return view('pdf/personalpdf');
}
}
}
Routes file
Route::get('pdf/personalpdf','PdfController#personalpdf');
Route::get('/personal','PdfController#personal');
Try this. You have to pass both the user and results from the same controller action to the view. I think you lack some basics of laravel. Please go through the docs before you proceed.
"Undefined variable: Pro (View: E:\xampp\htdocs\mypro\resources\views\folder\product.blade.php)" I am using laravel 5.6
trying to get data from existing database.
Product Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table='products';
protected $primaryKey = 'p_id';
protected $fillable = ['p_title', 'p_element', 'p_description', 'p_duration', 'p_amount', 'p_startDate', 'p_endDate', 'p_old_price', 'p_new_price', 'p_keyWords', 'p_category', 'p_status', 'prefertime'];
}
Product Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$pro=Product::all();
return view('Folder.product'), compact('pro'));
}
}
Product.blade.php
#extends('layouts.app')
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
#foreach($Pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
</tr>
</table>
#endsection
As identified pro is not capitalised so you should correct as follows. You should also check that the array is not empty before you execute the page otherwise you can find yourself with more errors;
Also note that I've moved your 's inside the foreach as this is needed to make the table display properly.
#section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
#if(! empty($pro))
#foreach($pro as $row)
<tr>
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="2">No records</td>
</tr>
#endif
</table>
#endsection
Also, in your controller the view should read
return view('Folder.product', compact('pro'));
not
return view('Folder.product'), compact('pro'));
The capital, you need to use $pro not $Pro.
#foreach($pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
#endforeach
I hope it works.