Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,797,230 members, 6,683,160 topics. Date: Thursday, 20 January 2022 at 04:19 AM

Populating Select Menu From Model In User Registration Form In Laravel - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Populating Select Menu From Model In User Registration Form In Laravel (1756 Views)

Common Error In Laravel #1:solved File_put_contents Failed To Open Stream / Solution To Tokenmismatchexception Problem In Laravel 5.1 / Create A Nested Lists Of Categories In Laravel 5 (2) (3) (4)

(1) (Reply) (Go Down)

Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 3:47pm On Aug 28, 2015
Am trying to pass data from a model to be used to populate a select menu in my user registration form. What is the easy way to achieve this?
Re: Populating Select Menu From Model In User Registration Form In Laravel by jidez007: 4:15pm On Aug 28, 2015
You would probably get all the data in your controller, then pass it to your view.
eg. return view('signup')->with('schools', School::all());
Then in your view, just loop through and display it.
eg

<select name="school">
@foreach($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
@endforeach
</select>

1 Like

Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 4:20pm On Aug 28, 2015
Actually, I want to customize my users registration form and am using Laravel default auth controller and there is no option to pass data to the register view.
Re: Populating Select Menu From Model In User Registration Form In Laravel by jidez007: 4:31pm On Aug 28, 2015
maekhel:
Actually, I want to customize my users registration form and am using Laravel default auth controller and there is no option to pass data to the register view.

yeah from your AuthController notice laravel is using AuthenticatesAndRegistersUsers trait. you can just override the getRegister method in the Controller.

So you will add this method in your AuthController to override the default.

public function getRegister()
{
$data = [
'schools' => School::all();
];

return view('auth.register', $data);
}


Also get in the habit of looking at some methods in the source codes sometimes, It really helps.
Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 5:10pm On Aug 28, 2015
jidez007:


yeah from your AuthController notice laravel is using AuthenticatesAndRegistersUsers trait. you can just override the getRegister method in the Controller.

So you will add this method in your AuthController to override the default.

public function getRegister()
{
$data = [
'schools' => School::all();
];

return view('auth.register', $data);
}


Also get in the habit of looking at some methods in the source codes sometimes, It really helps.
This worked but I didn't know could do such in Laravel 5.
Re: Populating Select Menu From Model In User Registration Form In Laravel by jidez007: 5:46pm On Aug 28, 2015
maekhel:

This worked but I didn't know could do such in Laravel 5.
You are welcome
Re: Populating Select Menu From Model In User Registration Form In Laravel by micodon(m): 10:07pm On Aug 28, 2015
maekhel:
Am trying to pass data from a model to be used to populate a select menu in my user registration form. What is the easy way to achieve this?

The whole point of MVC is "separation of concerns". Your Model should fetch your data. Your views should display info to the users. The controller ties the model and the view together. It should control the process.

To answer you question: if your model is named User, call it in your controller like so

$this->data['users'] = User::get();

Then you can call your view like this

return View::make('user', $this->data);


Your data will be extracted and made available in the $users variable
Re: Populating Select Menu From Model In User Registration Form In Laravel by dplumptre(m): 2:17am On Aug 29, 2015
maekhel:

This worked but I didn't know could do such in Laravel 5.

Yeah you can easily override the methods in the AuthenticatesAndRegistersUsers trait

by copying the methods to your AuthController Class, easy navigation if you are using netbean as I am , just press ctrl and click on

AuthenticatesAndRegistersUsers from the AuthController Class , so you can just easily modify both methods below to suit your purpose

public function getRegister()
{
//code
}

public function postRegister(Request $request)
{
//code here
}
Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 2:36am On Aug 29, 2015
dplumptre:


Yeah you can easily override the methods in the AuthenticatesAndRegistersUsers trait

by copying the methods to your AuthController Class, easy navigation if you are using netbean as I am , just press ctrl and click on

AuthenticatesAndRegistersUsers from the AuthController Class , so you can just easily modify both methods below to suit your purpose

public function getRegister()
{
//code
}

public function postRegister(Request $request)
{
//code here
}
Thanks for the insight
Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 9:17pm On Aug 29, 2015
How do I populate a select menu based on a previous select menu option in laravel.
Re: Populating Select Menu From Model In User Registration Form In Laravel by jacob05(m): 9:41pm On Aug 29, 2015
maekhel:
How do I populate a select menu based on a previous select menu option in laravel.
One thesame page
Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 9:45pm On Aug 29, 2015
jacob05:

One thesame page
yes
Re: Populating Select Menu From Model In User Registration Form In Laravel by jacob05(m): 10:12pm On Aug 29, 2015
maekhel:

yes

use Javascript .... You'd need an API call too.. to fetch the result based on the selected select option
Re: Populating Select Menu From Model In User Registration Form In Laravel by maekhel(m): 10:27pm On Aug 29, 2015
jacob05:


use Javascript .... You'd need an API call too.. to fetch the result based on the selected select option
How do create an api route
Re: Populating Select Menu From Model In User Registration Form In Laravel by jacob05(m): 11:19pm On Aug 29, 2015
maekhel:

How do create an api route

It's just a normal route.. But instead of returning a view you return a "TYPE"... The "TYPE" can be a string, XML, and most preferably JSON

eg
return Response::json(array('name' => 'Steve', 'state' => 'CA'));

(1) (Reply)

I Need To Learn Programming Seriously / Why It's Important To Be Very Well-rounded Today / IoT (internet Of Things) With ESP8266

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2022 Oluwaseun Osewa. All rights reserved. See How To Advertise. 62
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.