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>
Related
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
I have a controller which gets an array of a User's diaries from my database and passes them to my view:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
In my view, I am looping through the diaries using a #foreach loop.
<div id="diaries" class="card-columns">
#if (count($diaries) > 0)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
#endif
</div>
But I am getting the following undefined variable error...
Undefined variable: diary (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
Why is my $diary variable undefined inside the #foreach loop?
You have a typo
change #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
and it should work!
There is some typo in var_dump use {{ var_dump($dairy) }}
Try to use compact method for pass data to view as shown below
//return view('app.diary.readDiaries', compact('diaries'));
public function readDiaries($hash)
{
$user = User::where('hash', $hash)
->first();
$diaries = Diary::where('user_id', $user->id)
->get();
return view('app.diary.readDiaries', compact('diaries'));
}
It is just a simple spelling mistake,
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
in your foreach you are passing $dairy and dumping var name is $diary
chane it to
#foreach ($diaries as $dairy)
{{ var_dump($dairy) }}
#endforeach
Habbit of copy paste is sometimes good for us...
:)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
you used ($diaries as $dairy) in foreach
but in foreach you used ($diary)
you want edit this {{ var_dump($diary) }} to {{ var_dump($dairy) }}
or you want edit #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
I'm having issues on displaying my data from foreach loop. I have 400+ thumbs on my database but laravel doesn't not work correctly, and my footer template didn't display too. I will put my code below.
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
myfooter code goes here.
from my controllers
$data['thumbs'] = thumb::all();
return View('tubetour/home',$data);
but when I tried to var_dump or return the value of thumbs on my controller
it displays all my 400+ data.
$data['thumbs'] = thumb::all();
return $data['thumbs'];
You need to pass an associative array as a second argument to the view function:
// Controller
$thumbs = Thumb::all();
return \View::make('tubetour.home', ['thumbs' => $thumbs]);
// View
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Edit: if you are using laravel 4 you will need to use View::make()
If Thumb is an Eloquent model, then the Thumb::all() will return an Eloquent collection, not an array. In that case you have to update your blade template like so:
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Hope this solve your issue.
UPDATE
Pass the $thumbs as an array and display it as table.
Update your controller like this:
$data['thumbs'] = thumb::all()->toArray();
return View('tubetour/home', $data);
And your view like this, see how many rows being displayed.
<table>
<thead>
<tr>Id</tr>
<tr>Name</tr>
<td>Desc</td>
<td>Place</td>
</thead>
<tbody>
#for ($i = 0; $i < count($thumbs); $i++)
<tr>
<td>{{ $i }}</td>
<td>{{ $thumbs[$i]['name'] }}</td>
<td>{{ $thumbs[$i]['desc'] }}</td>
<td>{{ $thumbs[$i]['place'] }}</td>
</tr>
#endfor
</tbody>
</table>
UPDATE 2
Blade template example with bootstrap grid:
<div class="row">
#for ($i = 0; $i < count($thumbs); $i++)
<div class="col-md-4">
<img src="img/sample.jpg">
<h3>{{ $thumbs[$i]['name'] }}</h3>
{{ $thumbs[$i]['desc'] }}
</div>
#endfor
</div>
try this
#foreach($data as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
Update your controller
$data = Thumb::all();
return View::make('tubetour/home')->with("thumbs",$data)
->render();
Then your view with this:
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
#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
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 ) }}