Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails - php

what is my mistake?
my controller :
public function index()
{
$employees = Employee::all();
return view('employee.view_all_employee_details', compact('employees'));
}
My Employee Model function :
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id' , 'emp_id');
}
My EmployeeAdditionalDetail model :
public function employeeDetails()
{
return $this->belongsTo('App\Employee' , 'emp_id' , 'emp_id');
}
My View
#if(count($employees) > 0)
#foreach($employees as $employee)
<tr>
<td> {{ $employee->first_name }} </td>
<td> {{ $employee->manager_id }} </td>
<td>
{{ $employee->basicdetails->personal_email }} </p> **Error showing here**
</td>
</tr>
#endforeach
#else
{!! "<tr><td>No Recod Found</td></tr>" !!}
#endif
My Error:
ErrorException in
C:\xampp\htdocs\socialhub\app\Http\Controllers\EmployeeController.php
line 22: Undefined property:
Illuminate\Database\Eloquent\Collection::$basicdetails

I suspect the issue is with the third parameter of $this->hasOne(..), it should be 'local_key', normally this would be 'id'.
Try removing the third parameter, or put the correct local_key:
public function basicdetails()
{
return $this->hasOne('App/EmployeeAdditionalDetail' , 'emp_id');
}

Related

How to add two numbers in the blade Laravel

I need to add two numbers/amount displayed in my blade view, as shown in the image below
I need to display as 666.68 / 1000 AUD
Here's my blade codes
<td>
#foreach ($team->competitionPayments as $payment)
{{ $payment->amount }}
#endforeach
<br>
{{ $team->competitionPayments }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
Here's the competitionPayments relationship in the Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Please give me an idea of how to make this work
here competitionPayments is a collection so you can apply sum() function on it like
<td>
{{ $payment->competitionPayments->sum('amount') }}
<br>
ref link https://laravel.com/docs/8.x/collections#method-sum
This works
<td>
{{ $team->competitionPayments->sum('amount') }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>

Laravel one to many relationship doesn't work anymore

This relationship of mine was working last week, but it stopped working.
When i use it in my blade template it says: Trying to get property 'name' of non-object.
The weird thing is, when i use it in dd() it perfectly shows, but when i try to print it in blade, it doesn't.
Code PaymentsController
<?php
namespace App\Http\Controllers;
use App\Betaling;
use App\Settings;
use Illuminate\Http\Request;
class PaymentsController extends Controller
{
public function index(){
$user_id = auth()->user('id');
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id->id)->first();
if ($role === 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id->id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
}
Code index.blade.php
#foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id }}</th>
#if($role === 1)
<td>{{ $betaling->user->name }} </td>
#endif
<td>{{ $betaling->customer->name }}</td>
<td>{{ $betaling->reference}}</td>
<td>€ {{ $betaling->amount}}</td>
<td>{{ $betaling->time_of_invoice }}</td>
<td>{{ $betaling->time_of_payment }}</td>
<td></td>
<td>{{ $betaling->user->location }}</td>
</tr>
#endforeach
code User model:
public function betalingen(){
return $this->hasMany(Betaling::class);
}
code Betaling model:
public function customer(){
return $this->belongsTo(User::class);
}
public function user(){
return $this->belongsTo(User::class);
}
code Customer model:
public function betaling(){
return $this->hasMany(Betaling::class);
}
pass auth()->user()->id and in blade file check !empty($totaalaantalbetalingen)
code User model:
public function betalingen(){
return $this->hasMany('App\Betaling','betalingenid','id'); // pass foreign key for ex.
}
code Betaling model:
public function customer(){
return $this->belongsTo('App\User','customer_id','id');// pass foreign key for ex.
}
public function user(){
return $this->belongsTo('App\User','user_id','id');// pass foreign key for ex.
}
code Customer model:
public function betaling(){
return $this->hasMany('App\Betaling','customer_id','id');// pass foreign key for ex.
}
Controller
public function index(){
$user_id = auth()->user()->id;
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id)->first();
if ($role == 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
blade.php
#if(!empty($totaalaantalbetalingen) && isset($totaalaantalbetalingen))
#foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id ?? '' }}</th>
#if($role === 1)
<td> {{ $betaling->user->name ?? '' }} </td>
#endif
<td> {{ $betaling->customer->name ?? '' }} </td>
<td> {{ $betaling->reference ?? '' }} </td>
<td> € {{ $betaling->amount ?? '' }} </td>
<td> {{ $betaling->time_of_invoice ?? '' }} </td>
<td> {{ $betaling->time_of_payment ?? '' }} </td>
<td> </td>
<td> {{ $betaling->user->location ?? '' }} </td>
</tr>
#endforeach
#endif

Paginate eager loaded relationships results

i am trying to paginate a relationship result set by using a code very similar to this
<?php
class MainController extends Controller {
public function show(Main $main)
{
$main = $main->with([
'secondaryMorph' => function ($query) {
$query->orderBy('ocurred_at');
$query->paginate(50);
}
])->first();
return view('main.show')->with(compact('main'));
}
}
But when this part of the view's code run a exception is thrown:
<td colspan="3">Mostrando {{ $secondary->count() }} de {{ $secondary->total() }}</td>
<td colspan="3">{{ $secondary->links() }}</td>
ErrorException (E_ERROR)
Method Illuminate\Database\Eloquent\Collection::total does not exist.
How can i use eager loading with pagination?
How about :
$main = $main->first();
$secondary = $main->secondaryMorph()->paginate();
return view('main.show')->with(compact('main','secondary'));
And then in blade :
<td colspan="3">Mostrando {{ $secondary->count() }} de {{ $secondary->total() }}</td>
<td colspan="3">{{ $secondary->links() }}</td>

Trying to get property of non-object in Laravel 5.1

Firstly it works fine But I don't know how to come this error in this page but this problem can be solve when i changed the code in view {{$training->first()->sectionsCount}} but my specification has been incorrect.
My complete error is:-
ErrorException in Training.php line 51: Trying to get property of non-object (View:resources/views/Training/index.blade.php)
My code in model is:-
public function sectionsCountRelation()
{
return $this->hasOne('App\Schedule')->selectRaw('training_id, count(*) as count')->groupBy('training_id')->where('training_end_date','<',carbon::now());
}
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation->count;<!--This is line 51 -->
}
In controller is
public function index()
{
$training = Training::with('sectionsCountRelation')->get();
return view('Training.index',compact('training'));
}
In View:-
#foreach ($training as $training)
<tr>
<td>{{$i}}</td>
<td>{{ $training->category }}</td>
<td>{{ $training->topic }}</td>
<td>{{$training->sectionsCount}}</td>
<td>Update</td>
<td>Schedule</td>
<td>
{!! Form::open(['method' => 'DELETE', 'route'=>['training.destroy', $training->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
<?php $i++;?>
{!! Form::close() !!}
</td>
</tr>
#endforeach
Looks like you have some Trainings that have no Schedule assigned. That means, $this->sectionsCountRelation returns null and you can't access ->count on it. Try checking for null:
public function getSectionsCountAttribute()
{
return $this->sectionsCountRelation === null ? 0 : $this->sectionsCountRelation->count;
}

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