Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance - php

I have a Many to Many relationship between Wallet & User Models:
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Now I need to get the name of wallets and the balance of that wallet in a table at Blade.
So at the Controller, I added this:
public function index($id)
{
// $id is the user id
$userWallet = Wallet::with("users")->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();
}
And then at the Blade:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users()->balance }}
</div>
#empty
Empty
#endforelse
But I get this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$balance
So what's going wrong here? How can I properly get the balance at the pivot table?

You have to loop user
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
#if(isset($wallet->users)&&count((array)$wallet->users))
#foreach($wallet->users as $user)
{{$user->pivot->balance }}
#endforeach
#endif
</div>
#empty
Empty
#endforelse
Your query should be
$userWallet = Wallet::with(["users"=>function ($query)use($id){
$query->where('user_id',$id);
}])->whereHas('users',function ($query)use($id){
$query->where('user_id',$id);
})->get();

That's because you use the relation's query builder object with users() but you need user property to get the collection:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->balance }}
</div>
#empty
Empty
#endforelse
But even the collection instance doesn't have a balance property as long as it is not a custom collection. You probably need to loop over the collection or use pluck to get the model's balance property:
#forelse($userWallet as $wallet)
<div class="message_fullName">
{{ $wallet->name }}
</div>
<div class="message_tag">
{{ $wallet->users->pluck('balance')->join(', ') }}
</div>
#empty
Empty
#endforelse

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>

Call to undefined method error when I try to use links()

When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}

Laravel records not geting as dynamic

views/show.blade.php
#extends('layouts.app')
#section('content')
<h5>Showing Task {{ $task->title }}</h5>
<div class="jumbotron text-center">
<p>
<strong>Task Title:</strong> {{ $task->title }}<br>
<strong>Description:</strong> {{ $task->description }}
</p>
</div>
#endsection
Controllers/HomeController.php
public function show(Task $task)
{
return view('show', compact('task', $task));
}
routes/web.php
Route::get('show/{id}', 'HomeController#show')->name('show');
views/viewalltask.blade.php
<td>{{$data->title}}</td>
No error / No Record / instead of particulr record display blank page
You need to change the route configuration to:
Route::get('show/{task}', 'HomeController#show')->name('show');
This way Laravel's IOC container knows how to resolve/bind it.
Must match the variable name used in the method definition:
public function show(Task $task)

Laravel - Call to a member function get() on a non-object

#foreach ($wishItems as $wishItem)
<?php $products = Product::find($wishItem->product_id)->get(); ?>
#foreach($products as $product)
#if($product->id == $wishItem->product_id)
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endif
#endforeach
#endforeach
$wishItems is var that is sent to this view and it is ok... $products = Product::find($wishItem->product_id)->get(); here I have problem.. I got error: Call to a member function get() on a non-object .... I tried to replace $wishItem->product_id with number for example 3 [it exists in DB] but again same...
I assume what you really want is:
#foreach ($wishItems as $wishItem)
<?php $product = Product::find($wishItem->product_id); ?>
Name: {{ $product->title }} <br>
Price: {{ $product->price }} €<br>
<hr>
#endforeach
Your $wishItem really just refer to a single product, thus eliminating the need for you to query a collection, and do a loop.
Assuming you have a WishItem and Product model
class WishItem extends Eloquent {
public function product()
{
return $this->hasOne('Product');
}
}
In your view
$product = $wishItem->product;
More info:
http://laravel.com/docs/eloquent#basic-usage

Returning checked checkboxes based on pivot table

This question is regarding Laravel and PHP.
I am building a form that allows users (players) to be assigned to / de-assigned from a team using checkboxes.
Many users can belong to many teams. I have a pivot table team_user to manage the many-to-many relationship.
I am having trouble populating the checkboxes based on whether the association exists in the pivot table.
So far I have them in two separate 'bits', which you can see below:
My Controller:
$users_checked = $team->users()->get();
$users = User::all();
return View::make('team/addplayers', compact('users', 'team', 'users_checked'));
My View:
#foreach ($users_checked as $user_checked)
<p>
{{ Form::checkbox('player[]', $user_checked->id, true) }}
{{ Form::label('email', $user_checked->email) }}
</p>
#endforeach
#foreach($users as $user)
<p>
{{ Form::checkbox('player[]', $user->id ) }}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
What is the most sensible way to 'combine' these into one list, where the checkbox is ticked if the association exists in the pivot, or not ticked if it doesn't?
Many thanks for any help!
The answers put me on the right track, thank you. My actual solution is below:
In my controller:
public function showAddPlayer(Team $team)
{
$users = User::all();
return View::make('team/addplayers', compact('users', 'team'));
}
In my view:
#foreach ($users as $user)
<p>
{{ Form::checkbox('player[]', $user->id, $team->users->contains($user->id)) }}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
I think you may try something like this:
$teams = Team::with('users')->get();
return View::make('team/addplayers', compact('teams'));
In your view:
#foreach ($teams as $team)
#foreach ($team->users as $user)
<p>
{{ Form::checkbox(
'player[]',
$user->id,
(in_array($user->id, $team->users->fetch('id')) ? 1 : 0)
)
}}
{{ Form::label('email', $user->email) }}
</p>
#endforeach
#endforeach
{{ Form::checkbox('player[]', $user->id,in_array($user->id, $users_checked->lists('id')) ? 1 : 0 ) }}

Categories