Norbertho
1 year ago

Append to and modify users collection in controller before pass it to view

Posted 1 year ago by Norbertho

I walk trough below on an "example" and i have commented what i would like to achieve.

Table Name: users

id   name    competition1  competition2 competition3   location

1   Adam        yes         no          yes                    null
2   Peter       yes         yes         no            null

in my controller would be something like this:

$competitionlocation1 = 'foo';
$competitionlocation2 = 'bar';
$competitionlocation3 = 'some';


---->> I would like to do something like this:

$users = User::where('competition1',  '==', 'yes')->get();   ----->and then i would like to update location field for them with $competitionlocation1

---->>  so i would get like this:

  $users =
 [
        ['name' => 'Adam', 'location' => 'foo'],
        ['name' => 'Peter', 'location' => 'foo'],
    ]

---->  Then I would like to run it again:

$users = User::where('competition2',  '==', 'yes')->get();   ----->and then i would like to update location field for them with $competitionlocation2 

----> I would like to append this new collection to to the previous one like this:

  $users =
 [
        ['name' => 'Adam', 'location' => 'foo'],
        ['name' => 'Peter', 'location' => 'foo'],
     ['name' => 'Peter', 'location' => 'bar'],
    ]

-----> Then I would like to run it again:

$users = User::where('competition3',  '==', 'yes')->get();   ----->and then i would like to update location field for them with $competitionlocation3 

------> I would like to append this new collection to to the previous one like this:

  $users =
 [
        ['name' => 'Adam', 'location' => 'foo'],
        ['name' => 'Peter', 'location' => 'foo'],
    ['name' => 'Peter', 'location' => 'bar'],
    ['name' => 'Adam', 'location' => 'some'],
    ]

-----> Then pass it to the view:

return view('welcome')->with('users', $users);

Please sign in or create an account to participate in this conversation.