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 ) }}
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 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>
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 wish to make a select box containing some categories of items. This select box should have the data from database.
so first i created my blade
admin.blade.php :
<h2>Add Category</h2>
{{ HTML::ul($errors->all(), array('class'=>'errors')) }}
{{ Form::open(array( 'url'=>'admin/category/add', $title="Admin Control Panel")) }}
<p>
{{ Form::label('Category Name:') }}
{{ Form::text('category_name', Input::old('category_name')) }}
</p>
<p>
{{ Form::select('parent_category', array('' => 'Select Category', '0 ' => 'Main Category',)) }}
{{ Form::label('Parent Category:') }}
</p>
<p>
{{ Form::submit('Submit', array('name' => 'save')) }}
</p>
{{ Form::close() }}
and then i created my controller:
class CategoryController extends BaseController
{
public function add()
{
$category_list = CategoryModel::select();
return View::make('admin')->with('category_list', $category_list);
}
i need to display this array in my select box. but i don't know how to do that. please suggest a proper way!
thanks!
return View::make('admin')->with('category_list', $category_list);
What this is saying is create a response and return it to the client. The response is going to be generated using the blade template admin, and we're going to pass the contents of the variable $category_list to the view which will be able to reference it internally as $category_list (the first string parameter to the with function).
Therefore, inside your view if you want to access your category list you can do this:
{{ $category_list->title }}
Assuming, of course, that $category_list here is a single model with an attribute title. If $category_list is a collection of models (or an array), you can just use foreach:
#foreach($category_list as $category)
{{ $category->title }}
#endforeach
If what you're doing is trying to build a select list based off of the entries in your database for a given model, you can simply do this:
{{ Form::select('category', CategoryModel::list('title', 'id')) }}
Of course, you can generate that array in your controller and pass it down as you are doing now, too.
I've got my relationships setup where my projects are related to clients, what I want to do is to be able to select a select in my create project view via drop down list.
This can be achieved as so:
{{ Form::select('client', array('s' => 's', 'm' => 'm'), 's');}}
However I'm not sure how I would do this dynamically from the data I am retrieving from the database, the code I have so far is:
{{ Form::open(array('action' => 'ProjectController#store', 'id' => 'createproject')) }}
<div class="form-group">
<? $projects = Auth::user()->clients; ?>
#if (Auth::check())
#if (count($projects) > 0)
#foreach ($projects as $client)
{{ Form::select('client', array('client_one' => 'client_one', 'client_two' => 'client_two'), 'client_one');}}
#endforeach
#else
<h3>You have no projects click here to create a project</h3>
#endif
#endif
</div>
<div class="form-group">
{{ Form::label('project_name', 'Project Name') }}
{{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}
</div>
Can anyone point me into the direction of how I can populate this select field dynamically?
Thanks in advance!
Doing the data processing in the view is not a good idea. What you should do is prepare the array in the controller and use that array in the view.
In your controller.
$clients = Auth::user()->clients;
$client_selector = array();
foreach($clients as $client) {
$client_selector[$client->name] = $client->name; // I assume name attribute contains client name here
}
return View::make('your.view', array(.., 'client_selector' => $client_selector, ...));
In your view.
#if(count($client_selector)>0)
{{Form::select('client', $client_selector, array_values($client_selector)[0])}}
#endif
I think this is a be better solution:
In your controller:
$clients = Auth::user()->clients->lists('name', 'name');
return View::make('your.view', array(.., 'clients' => $clients, ...));
In your view:
{{ Form::select('client', $clients) }}