Display unit count in Laravel blade template - php

I want to show total count of the unit so I wrote {{ ($unit->unit_id)->count() }} to return the data.
blade.php
<tbody>
#foreach($developer as $key => $data)
<tr>
<td></td>
<td>{{$data->developer_name}}</td>
<td>
#foreach($data->projects as $key => $project)
{{ $project->project_name }}<br>
#endforeach
</td>
<td>
#foreach($data->projects as $key => $project)
#foreach($project->phase as $key => $phase)
#foreach($phase->unit as $key => $unit)
{{ ($unit->unit_id)->count() }}<br> //error
#endforeach
#endforeach
#endforeach
</td>
</tr>
#endforeach
</tbody>
UPDATE:
Unit.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Unit extends Model
{
//
protected $table="pams_unit";
protected $primaryKey="unit_id";
}
UnitController
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Unit;
use App\Developer;
class UnitByDeveloperController extends Controller
{
//
public function __construct()
{
$this->middleware('auth');
}
public function show(Request $request){
$dev_id = $request->input('dev_id');
$developer =Developer::where(function($query1) use($dev_id){
if($dev_id){
$query1
->where('id',$dev_id);
}
})
->active()
->paginate('8');
return view('reportlayout.unitByDeveloper',compact('developer'));
}
}
But, an error is showing :-
Call to a member function count() on integer
Anyone can help me to spot my mistake ?

Change this:
{{ ($unit->unit_id)->count() }}
To:
{{ $unit->count() }}

Related

How can I change a column time attribute to AM and PM for the blade view?

I have a table named time_entries and the column name is time_start.
I'm inserting Carbon::now(); on it. The data stored in the column is 2021-08-26 09:51:37 and blade.php shows same result.
Model TimeEntry:
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TimeEntry extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
public function getTimeStartAttribute($value)
{
return Carbon::createFromFormat('H:i:s', $value)->TimeString();
}
}
Controller:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\TimeEntry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AttendanceController extends Controller
{
public function index()
{
$data = TimeEntry::where('user_id', Auth::user()->id)->paginate(2);
return view('Admin.Attendance-History-page', compact('data'));
}
}
Blade view:
<tbody id="table_data">
#foreach ($data as $punch)
<tr>
<td>{{ $punch->user->name }}</td>
<td>{{ $punch->user->employee_id }}</td>
<td>19 Feb 2019</td>
<td>{{ $punch->time_start }} </td>
<td>{{ $punch->time_end }}</td>
<td>9 hrs</td>
<td>
<span class="badge bg-inverse-success">Perfect</span>
</td>
<td>0</td>
</tr>
#endforeach
</tbody>
I want to know how can I format the output from 2021-08-26 09:51:37 to something like 09:51:AM in Blade?
Thank you for your help.
There is a straight forward solution for this using the PHP date() method.
{{ date('h:m A', strtotime($punch->time_start)) }}
This will output 09:51 AM
If you want to output 09:51:AM then just add : after the m and remove the space, like so:
{{ date('h:m:A', strtotime($punch->time_start)) }}

Paginating in Laravel

I am working on an assignment for Uni however I've ran into a snag paginating my comments field. I have put my Controller, Model and View bellow(in that order)
<?php
namespace App\Http\Controllers;
use App\Comment;
use Illuminate\Http\Request;
class CommentController extends Controller
{
const COMMENTS_PER_PAGE = 5;
public function index () {
$comments = Comment::paginate (self::COMMENTS_PER_PAGE);
return view ('index') -> with (['comments' => $comments]);
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show(Comment $comment)
{
//
}
public function edit(Comment $comment)
{
//
}
public function update(Request $request, Comment $comment)
{
//
}
public function destroy(Comment $comment)
{
//
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public static function paginate(int $COMMENTS_PER_PAGE)
{
}
}
{{--#extends('layout.master)--}}
{{--#section('content')--}}
<div class="container main-table">
<div class="box">
<h1 class="title">Guestbook Comments</h1>
#if (count ($comments) > 0)
<table class="table is-striped is-hoverable">
<thead>
<tr>
<th>User</th>
<th>Comment</th>
<th>Date</th>
<th>Likes</th>
</tr>
</thead>
<tbody>
#foreach ($comments as $c)
{{--declaring the comments--}}
<tr>
<td>{{ $c -> name }}</td>
<td>{{ $c -> comment }}</td>
<td>{{ $c -> created_at -> format ('D jS F') }}</td>
<td>{{ $c -> likes }}</td>
</tr>
#endforeach
</tbody>
</table>
{{--looking at pagination--}}
{{ $comments -> links() }}
#else
<div class="notification is-info">
<p>
The Guestbook is empty. Why not add a comment?
</p>
</div>
#endif
</div>
</div>
{{--#endsection--}}
I managed to get it to display the comments before changing to pagination. After doing this I ran into issues getting it to display. I don't get an error I just get a blank page as if nothing is wrong. It doesn't show any of my headings or text that is not from my comments table.
Has anyone got any ideas about why this is?
Any help would be greatly appreciated :D
Update 17:44 13/11/19
Just realised that the link was going to the wrong page but just now figured out that the count function is having and issue as it says that there is no relevant ARRAY ("Facade\Ignition\Exceptions\ViewException
count(): Parameter must be an array or an object that implements Countable (View: C:\XAMPP\htdocs\Assignment\resources\views\comment\comments.blade.php) ")
Does anyone know why this is?
I thought it would just list comments 1 by 1 up until it reads out 5 comments.
Any ideas on how to fix this? :)
change {{ $comments -> links() }} & try this
{!! $comments->render() !!}
I would recommend to paginate like this.
public function index () {
const COMMENTS_PER_PAGE = 5;
$comments = Comment::paginate(self::COMMENTS_PER_PAGE);
return view('index', compact('comments'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
and also, change
{!! $comments->links() !!}
to
{!! $comments->render() !!}
{!! $comments->appends(Request::except('page'))->render() !!}

hasMany relationship Laravel 5.4 Trying to get property of non-object

I have a relationship in my models with foreing keys, protected table.
This is my models:
Lot.php:
<?php
namespace App;
class Lot extends Model
{
protected $table = 'auct_lots_full';
protected $primaryKey = 'lot_id';
public function comments()
{
return $this->hasMany(Comment::class,'lot_id', 'lot_id');
}
}
Than related model Comment.php:
<?php
namespace App;
class Comment extends Model
{
public function lot()
{
return $this->belongsTo(Lot::class, 'lot_id', 'lot_id');
}
}
In my view I try to do this:
#foreach ($comments as $comment)
<dt>{{ $comment->lot }}</td>
#endforeach
It result this:
{"lot_id":391959148,"date":"2017-05-21","company":"MITSUBISHI","model_year_en":2005"}
Than in view I need to get only company information and I do this:
#foreach ($comments as $comment)
<td> {{ $comment->lot->company }}</td>
#endforeach
this result Error:
Trying to get property of non-object
I try to do this:
#foreach ($comments as $comment)
<td>
#foreach($comment->lot as $lot)
{{ $lot->company }}
#endforeach
</td>
#endforeach
And it also gives error:
Trying to get property of non-object
How can I get $comment->lot->company work?
Thanks, to Snapey from laracasts
when you do this
#foreach ($comments as $comment)
<td> {{ $comment->lot->company }}</td>
#endforeach
you are relying on EVERY comment having a lot and every lot having a company
You can use this to protect against empty properties
#foreach ($comments as $comment)
<td> {{ $comment->lot->company or '---'}}</td>
#endforeach
Then the loop will continue even if the lot does not have company

Laravel: Eloquent returning an object I can't iterate over

I have a mySQL DB schema like this:
table RECIPES:
int id, PK;
int salesman_id;
int doctor_id;
date order_date;
int status;
table RECIPE_STATUS:
int id, PK;
int recipe_status_code;
varchar recipe_status_name;
In Laravel I have the following php classes:
AdminRecipesController.php
namespace RatCMS\Controllers;
use App\Http\Controllers\Controller;
use Auth;
use RatCMS\Models\Recipes;
use RatCMS\Models\RecipeStatus;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class AdminRecipesController extends Controller
{
public function index()
{
$recipes = Recipes::with('recipeStatus')->get();
return view('admin.recipes.news', compact('recipes'));
}
}
Recipes.php
<?php
namespace RatCMS\Models;
use Illuminate\Database\Eloquent\Model;
class Recipes extends Model
{
protected $table = 'RECIPES';
protected $fillable = ['salesman_id', 'doctor_id', 'order_date', 'status'];
public function recipeStatus() {
return $this->hasOne(\RatCMS\Models\RecipeStatus::class, 'recipe_status_code', 'status');
}
}
RecipeStatus.php
<?php
namespace RatCMS\Models;
use Illuminate\Database\Eloquent\Model;
class RecipeStatus extends Model
{
protected $table = 'RECIPE_STATUS';
protected $fillable = [];
public function recipes() {
return $this->belongsTo(\RatCMS\Models\Recipes::class);
}
}
blade view
<div class="box-body">
#include('admin.partials.session_success')
#if($recipes->isEmpty())
<p class="text-center">No recipes available.</p>
#else
<table class="table table-bordered">
<tr>
<th style="width: 10px">#</th>
<th>Salesman</th>
<th>Doctor</th>
<th>Date</th>
<th>Status</th>
<th style="width:100px">Actions</th>
</tr>
#foreach($recipes as $recipe)
{{ $recipe }}
<tr>
<td>{{ $recipe->id }}</td>
<td>{{ $recipe->salesman_id }}</td>
<td>{{ $recipe->doctor_id }}</td>
<td>{{ $recipe->order_date }}</td>
<td>{{ $recipe->recipe_status->recipe_status_name }}</td>
<td>
<form style="display: inline-block" action="{{ url('admin/recipes/' . $recipe->id) }}" method="POST">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-danger"><i class="fa fa-close" aria-hidden="true"></i> Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
#endif
</div>
When I do {{ $recipe }} in the view, I get the following result:
{"id":1,"salesman_id":"4","doctor_id":"3","order_date":null,"status":"0","recipe_status":{"id":1,"recipe_status_code":"0","recipe_status_name":"Not Recieved"}}
so there is some data being return by Eloquent regarding the recipe status (Not Recieved text), but when I do {{ $recipe->recipe_status }} it fails every time. Even $recipes['recipe_status'] is no help.
Could someone help? Thanks
Relationship should be belongsTo():
public function recipeStatus() {
return $this->belongsTo(\RatCMS\Models\RecipeStatus::class, 'status', 'recipe_status_code');
}
With correct relationship your code will add relationship data to collection, so you'll be able to get status:
$recipe->recipeStatus->recipe_status_name

Laravel routing returns error: "Class LoginController does not exist"

I'm attempting to create a "create user"-form using Laravel and Blade.
My form is as follows:
{{ Form::open(['route' => 'create_user']) }}
<table>
<tr>
<td>
{{ Form::label('email', 'Email:') }}
</td>
<td>
{{ Form::email('email') }}
</td>
</tr>
<tr>
<td>
{{ Form::label('password', 'Password:') }}
</td>
<td>
{{ Form::password('password') }}
</td>
</tr>
<tr>
<td colspan="2">
{{ Form::submit('Submit') }}
</td>
</tr>
</table>
{{ Form::close() }}
The 'create_user' name refers to the following route:
Route::post('user/new', array('as' => 'create_user', 'uses' => 'LoginController#store'));
I have a .php-file called 'LoginController.php' with the function:
public function store()
{
if (Auth::attempt(Input::only('email', 'password'))) {
return Auth::user();
}
return "Failed.";
}
My routing table looks like this:
But when I submit a form with valid data, I get the error message:
I've tried running "php artisan dump-autoload", but it didn't change anything.
EDIT: Adding full LoginController.php on request. (Pardon the lacking indentation.)
<?php
namespace app\controllers;
class LoginController extends \BaseController
{
public function create()
{
}
public function store()
{
if (Auth::attempt(Input::only('email', 'password'))) {
return Auth::user();
}
return "Failed.";
}
}
The problem is that you have put the controller into a namespace (app\controllers)
You either have to remove that or adjust your route:
Route::post('user/new', array(
'as' => 'create_user',
'uses' => 'app\controllers\LoginController#store'
));
In the routes i've always used resource instead. It handles the methods for you.
Route::resource('yourRoute', 'YourController');

Categories