I was working with Laravel and I am trying to paginate my table of books. I got this error "Call to a member function links() on array in Laravel". It may be as the duplicate's error, but I still can't figure out how to solve my thing.
BookController fiddle:
public function index()
{
$books = Book::simplePaginate(3)->all();
$authors = Author::all();
$genres = Genre::all();
return view('books.all')->withBooks($books)->withAuthors($authors)->withGenres($genres);
}
books/all.blade.php fiddle
<table class="table table-hover">
<tr class="info">
<td>#</td>
<td>Name</td>
<td>Author</td>
<td><center>Visit</center></td>
</tr>
#foreach($books as $book)
<tr>
<td width="50px"><img width="50px" height="75px" src="{{ asset('images/' . $book->image) }}"></td>
<td width="50px">{{ $book->name }}</td>
<td width="50px">{{ $book->author->name }}</td>
<td width="50px"><center><button class="btn btn-success">Visit</button></td>
</tr>
#endforeach
</table>
{{ $books->links() }}
Just remove the ->all() method from the $books variable.
$books = Book::paginate(10);
paginate() function considers taking all content from the table, which is taken here by Book model
Related
I'm trying to get the count of different models and list them by company.
I have a collection of different models.
public function listAllRequets()
{
$requests = collect();
$terms = Termination::with('user.company')->where('default_status', 2)->get();
$deacs = LoaDeactivation::with('user.company')->where('manager_status', 2)->get();
$reacs = LoaReactivation::with('user.company')->where('manager_status', 2)->get();
// Use push() to collect them all in one collection
foreach ($terms as $term)
$requests->push($term);
foreach ($deacs as $deac)
$requests->push($deac);
foreach ($reacs as $reac)
$requests->push($reac);
return $requests;
}
In my index function I'm adding them to $requests.
To get the list of companies I'm grouping them using the groupBy() method.
public function index()
{
$requests = $this->listAllRequets();
$requestCompanies = $requests->groupBy(function($requests) {
return $requests->user->company->company_acronym;
});
In my view I have this:
<table class="table table-sm">
<thead class="thead-light">
<tr>
<th>Company</th>
<th class="text-center">Terms</th>
<th class="text-center">Deacs</th>
<th class="text-center">Reacs</th>
</tr>
</thead>
<tbody>
#foreach ($requestCompanies as $company => $requests)
<tr>
<td class="pt-3"><strong>{{ $company }}</strong></td>
#foreach ($requests as $request)
<td class="text-center">{{ $request-> }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I need a way to get the count of each model by company.
My companies list out as expected but I'm not sure how to get the correct model count.
If you have relationships among your company and each request model (Termination, LoaDeactivation, LoaReactivation), wouldn't be easier to do that with a query at database level?
That seems a nice use case for with count method that eloquent provides:
Assuming the relations in the company model are called terminations, deactivations and reactivations, you could do:
public function index()
{
$companies = Company::withCount('terminations','deactivations','reactivations')->get();
// here pass $companies array to your view
}
Then in your view you can access the count for each type on each company:
<table class="table table-sm">
<thead class="thead-light">
<tr>
<th>Company</th>
<th class="text-center">Terms</th>
<th class="text-center">Deacs</th>
<th class="text-center">Reacs</th>
</tr>
</thead>
<tbody>
#foreach ($companies as $company)
<tr>
<td class="pt-3"><strong>{{ $company->name }}</strong></td>
<td class="text-center">{{ $company->terminations_count }}</td>
<td class="text-center">{{ $company->deactivations_count }}</td>
<td class="text-center">{{ $company->reactivations_count }}</td>
</tr>
#endforeach
</tbody>
</table>
I have tried to refer all the possible solutions in stackoverflow but could not solve this.
This is my Controller.
public function getMessages(){
$messages = Message::all()->toArray();
return View('admin.thesis', ['messages' => $messages]);
}
This is my route/web.php
Route::get ('/thesis','MessagesController#getMessages');
And this is my view
<div class="panel-heading">Thesis Details</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>E-Mail</th>
<th>Contact</th>
<th>State</th>
<th>Country</th>
<th>Article</th>
<th>Author</th>
<th>Uploaded File</th>
</tr>
#foreach($messages as $row)
<tr>
<td>{{$row['firstName']}}</td>
<td>{{$row['lastName']}}</td>
<td>{{$row['email']}}</td>
<td>{{$row['contactNumber']}}</td>
<td>{{$row['state']}}</td>
<td>{{$row['country']}}</td>
<td>{{$row['article']}}</td>
<td>{{$row['author']}}</td>
<td>
<a href="{{ URL::to('/') }}/uploads/{{$row['file_path']}}">
{{$row['file_path']}}
</a>
</td>
</tr>
#endforeach
</table>
</div>
</div>
This is my error
ErrorException
Undefined variable: messages (View:
C:\xampp\htdocs\fileupload\resources\views\admin\thesis.blade.php)
How do I resolve this?
Try this on your Controller
public function getMessages(){
$messages = Message::all();
return view('admin.thesis')->with(['messages' => $messages]);
}
And in your blade file
#foreach($messages as $row)
<tr>
<td>{{ $row->firstName }}</td>
<td>{{ $row->lastName }}</td>
<td>{{ $row->email }}</td>
<!-- etc... -->
<td>
<a href="{{ URL::to('/') }}/uploads/{{ $row->file_path }}">
{{ $row->file_path }}
</a>
</td>
</tr>
#endforeach
Try this on your Controller
public function getMessages(){
$messages = Message::all()->toArray();
return view('admin.thesis', compact('messages'));
}
I used following code and its work for me :
class SidebarController extends Controller
{
public function news_widget() {
$posts = Post::take(5)->orderBy('updated_at', 'DESC')->take();
return view('index', array('data'=>$posts));
}
}
And within the view I simply use $data and its working fine for me.
I keep getting this error after fixing a new code that I had just put in and I have no idea why it is giving me this error "Property [id] does not exist on this collection instance" which point to the id of my route in home.blade.php. Can someone help me take a look thanks a lot
The part that I added in was the hire_status part and after fixing it, it then gave me this error.
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Hire Status: </big></strong></th>
<th><strong><big>Action: </big></strong></th>
</tr>
<td>
<tr>
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th>
#foreach($data1 as $value1)
{{$value1->hire_status}}
</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="get">
{{ csrf_field() }}
<button type="submit">Delete</button>
</form></th>
</tr>
#endforeach
#endforeach
</tr>
</tr>
</table>
HomeController:
public function getData(){
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
$data1 = DB::table('hires')->where('hire_status','Yes')->get();
if(count($data)>0){
return view('home',compact('data','data1'));
}else{
return view('home');
}
}
Just remove key from array,
you are trying to get data in your view directly so get it directly not to data of data...
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
change it to,
$data = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
Hi Guy's Im always getting an error from laravel 5.4.
Im creating a list of members... but when i use #foreach it says
ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38:
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php)
Here's my controller
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first();
$count = count($teams);
if (!$count) {
return redirect('404');
} else {
return view('/admin/memberlist', ['team' => $teams]);
}
}
and here's my view code:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($qmember as $get_member)
<tr>
<td><img src="{{ $get_member->member_pic }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
When i remove the foreach the code is working.. but i try to add the #foreach($qmember as $get_member) it not working anymore...
Check this line:
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
when you are using first() then it does not return an Std Class object. If you want it so then use get()
Using first() method you are fetching only one record from DB that match with the ID (First one matched). You need to use get().
Controller:
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first()
$count = count($teams);
if(! $count)
{
return redirect('404');
}
else
{
return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade
}
}
View:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($teams as $team)
<tr>
<td><img src="{{ $team['member_pic'] }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
I'm working on an invoice system in a homemade CRM. I try to sum up the price for all of my items to get the grand total.
I don't know how to get the grandtotal in my index view.
Here is my table in factures/show.blade.php
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Description</th>
<th class="text-right">Prix</th>
</tr>
</thead>
<tbody>
#foreach($facture->items as $item)
<tr>
<td>{{$item->description}}</td>
<td class="text-right">{{$item->prix}} $</td>
</tr>
#endforeach
<tr>
<td class="text-right">TPS</td>
<td class="text-right">{{ $item->calcultps() }} $</td>
</tr>
<tr>
<td class="text-right">TVQ</td>
<td class="text-right">{{ $item->calcultvq() }} $</td>
</tr>
<tr>
<td class="text-right">Total</td>
<td class="text-right">{{ $item->calculgrandtotal() }} $</td>
</tr>
</tbody>
</table>
and here is my table in factures/index.blade.php
<table class="table table-striped">
<thead><tr>
<th><p># de facture</p></th>
<th><p>Date</p></th>
<th><p>Description</p></th>
<th><p>Client</p></th>
<th class="text-right"><p>Montant</p></th>
<th class="text-right"><p>TPS</p></th>
<th class="text-right"><p>TVQ</p></th>
<th class="text-right"><p>Grand total</p></th>
</tr></thead>
#foreach($factures as $facture)
<tr>
<td>{{$facture->id}}</td>
<td>{{ $facture->created_at->format('d F Y') }}</td>
<td>{{ $facture->courtedescription}}</td>
<td>{{ $facture->client->prenom}} {{ $facture->client->nom}}</td>
<td class="text-right">{{ $facture->montant}} $</td>
<td class="text-right">{{ $facture->tps}} $</td>
<td class="text-right">{{ $facture->tvq}} $</td>
<td class="text-right"> IDONTKNOW $</td>
</tr>
#endforeach
</table>
here is my Item.php model
use Illuminate\Database\Eloquent\Collection;
class Item extends \Eloquent {
protected $fillable = ['nom', 'prix'];
public function facture()
{
return $this->belongsTo('Facture');
}
public function calcultps(){
$total = DB::table('items')->sum('prix');
$tps = $total *0.05;
echo $tps;
}
public function calcultvq(){
$total = DB::table('items')->sum('prix');
$tvq = $total *0.09975;
echo $tvq;
}
public function calculgrandtotal(){
$totalsanstaxes = DB::table('items')->sum('prix');
$tps = $totalsanstaxes *0.05;
$tvq = $totalsanstaxes *0.09975;
echo $totalsanstaxes+$tps+$tvq;
}
}
and my Facture.php model
use Illuminate\Database\Eloquent\Collection;
class Facture extends \Eloquent {
protected $fillable = [];
public function client()
{
return $this->belongsTo('Client');
}
public function items(){
return $this->hasMany('Item', 'facture_id');
}
}
my index function for my FacturesController.php
public function index()
{
$factures = Facture::all();
return View::make('factures.index')->withFactures($factures);
}
my show function for my FacturesController.php
public function show($id)
{
$facture = Facture::find($id);
return View::make('factures.show')->withFacture($facture);
}
I tried these (in the factures/index.blade.php) without success:
{{ $item->calculgrandtotal() }}
{{ $items->calculgrandtotal() }}
{{ $facture->items->calculgrandtotal() }}
and this returns an array
{{$facture->items}}
but this doesn't work
{{$facture->items->prix}}
I know there is something I don't understand here. and I wonder how to print out the grandtotal in my index view for each of the invoice?
I used the comment of #MattBurrow (thank you) and I managed to print out what I want using this:
index.blade.php
{{ Item::calculgrandtotal() }}
Item.php model
static function calculgrandtotal(){
$totalsanstaxes = DB::table('items')->sum('prix');
$tps = $totalsanstaxes *0.05;
$tvq = $totalsanstaxes *0.09975;
return $totalsanstaxes+$tps+$tvq;
}