last day (14 days later) » 

3:52 AM
0
Q: How to pass variable from foreach to view?

FrancisunoxxI'm trying to pass a variable from foreach to my view. So I can access this using in my select form. Because I have two tables M:M relationship between departments and user. I need to get all the department_name where the user_id belong. For me able to send a data via department_name Here what I ...

 
@mmmm I updated my post please see :)
@KmasterYC I thought I can pass $department from my foreach loop to my view. Any tips how can I solve this?
@KmasterYC Why I can only get the last $department value? I updated my post please see my diagram.
@KmasterYC Please click the chat link thanks!!
 
@Francisunoxx sorry, I couldn't understand your problem , could you explain what you want to achieve ?
 
@jaysingkar I wanted to get all the department with user_id so I can send data by department. I already close to my solution! :)
 
@Francisunoxx you got the solution ?
 
@jaysingkar Not yet. I'm still thinking can I passed the $department variable in my view. Can you help me?
 
3:52 AM
sure... btw don't you think you should pass departmentRecipient instead of department in your ? since you have mentioned that departmentRecipient has the data you wanted
$departmentRecipient = DB::table('departments')->with('users')->get();
return view('document.create')->with('department', $departmentRecipient );
something like this
 
@jaysingkar If I pass departmentRecipient I'm just only getting the id of the department. I wanted to return my view like this. <option value = "{{ $list->id }}">{{ $list->department_name }}</option> But when I choose one of the option. It should have a user_id inside.
@jaysingkar I tried your solution but it says Call to undefined method Illuminate\Database\Query\Builder::with()
 
Sorry my bad
try this
 
You again! Hahaha
 
yes lol
 
Your so good. Sorry im just a student
 
3:53 AM
nothing like that..
Department::with('users')->all()->get();
$departmentRecipient = Department::with('users')->get();
return view('document.create')->with('department', $departmentRecipient );

and in your view
 
Did you get what I'm trying to achieve? Lets say I have department named Information Technology. This department has registered users named A, B, C. So If I choose IT in my dropdown list I will get also the users named A, B C that why I use pluck
Wait bro I'm gonna try this :)
 
so does that means, you have two selects ?
where do you want to show user names ? is there any div where you would populate the user's names
If this is the case.. I would suggest you not to include user ids in the Drop down... Instead fetch the users related to the department_id using ajax
 
Yes as you can see in my solution I pluck every user_id. So when I return this in my view. I will get all the departments where the user belong.
I just wanted to show department_name in my dropdown list. Yes I have please see.
Yes I wanted to fetch all the related users with department_id. But bro I still dont have any experience using ajax
 
In my view in going to be like this <option value = "{{ $list->department_id }}">{{ $a->department_name }}</option>
 
4:08 AM
now.. here's the one way to do it.. let me know if I understood correctly
you will generate the department list with department_id
in your page, there will be another Dropdown list whill show the user's belonging to the selected department
right ?
you can achieve this by using ajax on change event of department's dropdown
 
Yes yes correctly right.
 
Can I just use event for this? I still dont have any experience using ajax :(
 
I will explain the ajax part.. you know jQuery right ?
 
Yes a little bit only. I still don't have any huge experience on that because I'm still learning
 
4:12 AM
that's enogh
:)
 
But I understood the syntax of ajax. I still didn't use it they say you need to be good first a javascript
 
I don't think so.. jQuery has made it quite simple to use ajax
So, first step is create your view as you are doing now.. without the user ids
and keep your user's dropdown list blank
 
Now I will create now a separate option for the user. So this will be a department?
Sure bro im working on it thanks!
 
yes
you will also need to add a route to your application, which will accept the department id and return the users
 
I have already route for this bro. I just added a department drop down list in my view.
 
4:33 AM
add one more drop down list for users
<select name="user_id" id="user_id">

</select>
now ajax part
 
Under this div I will add another drop down list for users?
 
yes
 
Already done! :)
 
also change your department's name to department instead of department []
no
sorry
it's a multiple select right ?
 
Yes I wanted send multiple :)
 
4:39 AM
ok
 
So it will be more flexible.
 
so your view is ready.....
I will get you the ajax part
 
Yes yes :)
Sure bro. Thanks!!
 
can you get me the Route we were talking about ?
 
This is my route bro for get and post.
Route::get('/create',
[
'uses' => '\App\Http\Controllers\DocumentController@getDocuments',
'as' => 'document.create',
'middleware' => 'auth',
]);

Route::post('/create',
[
'uses' => '\App\Http\Controllers\DocumentController@postDocuments',
]);
I think I'm gonna adjust in my foreach request haha
 
4:51 AM
yup
 
It's okay bro haha
 
5:28 AM
hi
I will post the code in 30 mins.. on customer call :(
will get to you as soon as I'm done with customer
 
5:42 AM
Sure I'm still working on it thanks!
 
6:09 AM
You would need to add one more rote .. explanation inline
Route::post('/get_users',function(Request $request){
//we will send the department ids in department variable via ajax to this route

//Then we will use following query to get the departments with the passed ids and return its users
$users = Department::whereIn('id',$request->get(department))
->with('users')
->get()
->pluck('users');
return Response::json([
'success'=>'true',
'data' => $users
]);
});
 
Wait bro I'm gonna try this. So what my Controller looks like?
 
you don't need to change any controller
 
I'm little new to your syntax but hoping this will work hehe.
What should I change in my drop down list under foreach loop?
This is my view looks like
<select name = "department[]" multiple class = "form-control select2-multi">

@foreach ($departmentRecipient as $list)
<option value = "{{ $list->id }}">{{ $list->department_name }}</option>
@endforeach

</select>
 
all good just add id to your select tag, any id would do
 
You said that I will add more select tag right?
<select name="user_id" id="user_id">

</select>
And in my controller should look like this?
$departmentRecipient = Department::with('users')->get();
 
6:20 AM
yup
 
I got a error it says SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (webdev.document_user, CONSTRAINT document_user_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE) (SQL: insert into document_user (document_id, sender_id, user_id) values (117, 6, 1))
It's getting the id of departments table.
As you can see the third values. The 1 is the document id
 
$departmentRecipient = Department::with('users')->get(); is the select statement and not the insert statement
are u sure u r getting it for $departmentRecipient = Department::with('users')->get();
 
I dd the $departmentRecipient it looks like there's no user_id at all have a look
And this is the foreach where I insert the department
foreach($request->department as $departmentId)
{
$document->sentToUsers()->sync([ $departmentId => ['sender_id' => $user->id]],false );
}
This is the first foreach that I do where I send a data per user.
foreach($request->recipient as $recipientId)
{
$document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false );
}
 
6:39 AM
while doing var_dump try this
var_dump($departmentRecipient->toArray());
 
I get this please have a look.
As you can see in the end of the array it gets the user_id which is 8
{ ["department_id"]=> int(2) ["user_id"]=> int(8) } } } } }
 
yes
 
My head is already spinning haha
 
but you don't need user ids here right ?
cause we are going to get user's on the fly
 
I need user_id here for able to insert this into pivot table
 
6:52 AM
ohhk...
 
You can see here that user_id column is where I insert the id of the user
 
yup.. while inserting right ?
I'm talking about while rendering the view.. you don't need them
your getDocuments method
 
Yes yes I don't need them anymore because what should i show in my view is the department_name right?
Yes your right.
 
ok
$departmentRecipient = Department::all()->get();
only this will do
 
But this will only get all the department_name but there's no user_id
 
6:55 AM
we don't need them while rendering
 
I got Missing argument 1 for Illuminate\Support\Collection::get()
 
remove get() keep only all
$departmentRecipient = Department::all();
we are doing all this in getDocuments method
 
Yes already did that. But when I tried to insert it just insert the id of department_name
 
can you post your getDocuments method
 
public function getDocuments()
{
$resultRecipient = DB::table('users')->where('id', '!=', Auth::id())->get();

$departmentRecipient = Department::all();

$approver = DB::table('users')->get();

$resultCategory = DB::table('categories')->get();


//VIEW
return view ('document.create')
->with('resultRecipient', $resultRecipient)
->with('resultCategory', $resultCategory)
->with('approver', $approver)
->with('departmentRecipient', $departmentRecipient);
}
don't mind the $resultRecipient, $approver and $resultCategory
It's only a extra fields
 
7:01 AM
yes
I'm just trying to check the insert statement
its in post right ?
 
Yes this is the post method that I did
public function postDocuments(Request $request)
{

$this->validate($request,
[
'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
'content' => 'required',
'category' => 'required',
'recipient' => 'required',
'approver' => 'required',
]);


$document = new Document();
$user = Auth::user();
//Request in the form
$document->title = $request->title;
$document->content = $request->content;
$document->category_id = $request->category;
$document->save();


foreach($request->department as $departmentId)
Dont mind also the $request->recipient as $recipientId that's a insert statement per user. :D
 
ohh
but we didn't get the users yet
we didn't implement the ajax part yet
['sender_id' => $user->id]
 
I'm little bit confuse why we need to use ajax here?
 
this is supposed to be the user dropdown iin the view right ?
ok
 
['sender_id' => $user->id] is the current user which gonna insert into my pivot table.
Yes bro! :D
 
7:05 AM
ohh sorry
 
I'm just having a hard time inserting user_id :p
 
BTW, you should use attach and not sync
$document->sentToUsers()->attach([ $recipientId => ['sender_id' => $user->id]],false );
 
It's work well when using sync method. I still didn't try using attach method :D
I'm little bit confuse using attach method
 
7:29 AM
ok
one more thing
 
Sure sure what's that?
 
sentToUsers this is your table
right ?
I mean what table does the above function represents
 
No it's a function in my Document model
public function sentToUsers()
{
return $this->belongsToMany('App\Models\User', 'document_user');
}
It represent documents table
 
ok.. the one you sent as acreenshot
user_id | document_id| sender_id
 
Yes yes :)
You wanted to see my relation diagram?
So you will not get confuse
 
7:37 AM
sure
foreach($request->department as $departmentId)
{
$document->sentToUsers()->sync([ $departmentId => ['sender_id' => $user->id]],false );
}
in the above function, you are trying to set the department id int the table ?
*in the above function, you are trying to set the department id the table ?
 
ok.. got it.. then why are you trying to set department id in the above function ?
 
I'm confuse in this part. foreach($request->department as $departmentId) should contain in my view the id of the user.
You got it?
 
exactly
so it would be $request->user as $userId
 
So everytime I choose a department in my select form. When I hit the submit button what the Department choosen by the user should contain an id of the users.
 
7:43 AM
xyes
*yes
or what you can do is...
 
That's why I foreach here. What ever the request it will insert the id of the user.
Yes same as that but in my view should be by department not by users :)
 
what's the realtion between user and department
*relation
 
It many to many :)
 
1:m ?
ok
 
It's many to many
Have a look please
 
7:46 AM
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId)->users()->get() as $user)
{
$document->sentToUsers()->sync([ $user => ['sender_id' => $user->id]],false );
}
}
use this function
sorry, it has some mistakes
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId->id)->users()->get() as $user1) //find the users belonging to the current department
{
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false );
}
}
this one is correct
I have used $user1 because $user is already used up
you don't need this function
foreach($request->recipient as $recipientId)
{
$document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false );
}
as I understand this is the list of users belonging to selected department
right ?
 
So in my getDocuments method should still look like these?
$departmentRecipient = Department::all();
 
yes
 
Yes yes your correctly right!
 
no need of users at all
 
But I get an error Trying to get property of non-object
 
7:50 AM
not even in your view
where ?
 
My view still look like these :D
 
this is correct
 
One more try wait :D
 
can you once check if you are getting departmentRecipient in your view ?
 
Yes bro I'm getting the all the department in my view.
 
7:54 AM
so where did you get "Trying to get property of non-object" this error ?
 
I still got the error "Trying to get property of non-object"
 
in postmethod ?
 
Yes I just follow want you said
 
ya my bad
replace "$departmentId->id" with $departmentId
sorry
in find method
 
Can I asked a question? How long youve been programming?
 
7:58 AM
4-5 years
its my hobby
 
So youve been web programming for 4-5 years? How old are you?
I'm just 20 haha
 
I'm 22
not only web... all kind of mix
I love web more though
 
Oh I see. I serious programming for one year. I'm just new to web programming because I used java.
Yeah I like more web programming.
Thanks for your help!!
It's working!
 
any time bro....
where are you from ?
 
I'm from philippines you? India? Why indian are so good in programming?
 
8:00 AM
lol...
Bill gates is not indian
not even Mark
 
Yes your true. But indian still great. I don't know why.
 
me too don't know lol
btw, did you issue resolved ?
*your
 
Because they start early into programming that's why!
Yes bro thanks!
 
welcome
 
I'm still confuse here.
Department::find($departmentId)->users()
what is the users() here?
 
8:02 AM
ok..
here, I have its the function in your Department table
isn't it ?
*its the function in your Department table
so basically.. I'm finding the "department" with id "departmentId" using "Department::find($departmentId)"
And then retrieving all the users of this department using "users()" method
 
Oh I see. So laravel will know automatically that this is user_id?
 
yes
ok.. I will post the modified method as a answer.. select it as answer .. is it okay with you ?
 
Thanks a lot!! Anytips how to be good more on web programming? I like to asked question from a people with many have experience :)
Oh sure nothing wrong with that!
I'll accept your answer and vote it up
 
thanks...
tips.. i'm not sure...
I started laravel in june this year
 
It's okay I just wanted to get an idea.
 
8:08 AM
I would suggest to try to post answers on laravel
 
Yeah me too I started laravel this june and find this more easy than plain php
 
exactly
 
Do you mean laracast?
 
oh sorry .. no I mean SAO
*SO
Would get to know many things tgis way
*things
 
Oh this is only my source of answers.
Yes your really right. You'll get a lot of styles
 
8:10 AM
yup
are you pursuing your degree ?
I mean college ?
 
Yes Im taking Information Technology here in the Philippines and find out the fundamentals here is very low
How about you?
 
Completed it last year.. in Information Technology only
:P
 
Wow nice! So your already working?
 
yes
 
How many years takes you to finish that degree?
Is it hard to work outside?
You used laravel in your work
?
 
8:15 AM
1) 4 years for college
2)Its easy, provided you get the job you love
3)Nope, I'm using it for my startup
 
Oh here it's 3 years. It depends if it's semestral.
That's true you should love what your doing.
I think I'm gonna stick here that php plain what do you think?
 
sorry I didn't get that ..last one
:(
 
I think I'm gonna used laravel than php plain :D
 
ohh
yes absolutely..
 
I'm still learning javascript and jquery because I'm just new :D
 
8:18 AM
you can't make scalabel applications using plain PHP.. that would be huge work
ohh.. great...
they are quite easy to learn , you will get them quickly
 
What I didn't get the first part.
Yes your right. I also have experience using php plain
 
sclabel application means which can be expanded according to your business's growth
it's not the perfect definition.. just trying to explain
 
Yes that's what other people says. So you use framework in your projects??
 
This is the first one,in which I'm using the framework ...
 
Oh nice! Find out your too fast solving those problems.
 
8:24 AM
My job profile is into support so no development there
 
I see in your bio youre a Developer at Tata Consultancy Services
What language you used here?
 
J2EE and Android developement
 
You used java also? Nice!
 
for front-end HTML CSS(Bootstrap) jQuery
yes
 
Wow nice. I still need to learn a lot.
 
8:26 AM
you will .. you still got lot of time...
gtg buddy.. boss here, lol
he will kill me otherwise
lol
 
I can't give you a bounty. 7 hours after I can accept your answer.
Okay bro thanks again!
 
yup that's fine
 
Do you have facebook?
Hahaha
 
yup
fb.com/jaysingkar
 
Can I add you?
 
8:27 AM
sure
I was going to ask the same
lol
 
Whahahaha because it's so hard to talk here. Thanks for your time bro. Your a good friend. Thanks for your time! :D
is this you?
 
no..
go to above link
 
Already sent you a friend request :D
 
my name is Kundan Jaysingkar
ok
 
Thanks bro! Really appreciated your time!
My name is John Francis nice to know you!
:D
 
8:29 AM
anytime.. ping me whenever you need any help :)
same her
*here
 
Sure. Once you accept my request. Thanks!!
 

last day (14 days later) »