I am still new in Laravel. I'm trying to create search box for searching users by their username.
What is the best way to create controller for Laravel search box?
The view that I have look as follows:
{{ Form::search_open('/users/search') }}
{{ Form::search_box('search','admin', array('class' => 'input-medium')) }}
{{ Form::submit('Search'); }}
{{ Form::close() }}
I have the controller as below:
class Users_Controller extends Base_Controller {
public function action_search() {
$userdetail = Input::get("username");
$details = User::where('username', '=', Input::get('username')) - > first();
return Redirect::to_route("users");
}
}
You may try something like this :
Route :
Route::get('/search', array('as' => 'user.search', 'uses' => 'user#search'));
View : (search/index.blade.php)
{{ Form::open(URL::to_route('user.search')) }}
{{ $errors->has('username') ? $errors->first('username','<span class="error">:message</span>') : '' }}
{{ Form::text('username', Input::old('username', $username), array('class' => 'input-medium')) }}
{{ Form::submit('Search'); }}
{{ Form::close() }}
#if ( isset($user) )
#foreach ($user->results as $user)
{{ $user->first_name }}
{{ $user->last_name }}
#endforeach
#endif
Controller : (controllers/user.php)
class User_Controller extends Base_Controller
{
public function action_search()
{
$data['username'] = Input::get('username');
if(Input::get())
{
$rules=array( 'username' => 'required' );
$validation = Validator::make(Input::all(), $rules);
if($validation->fails())
{
return Redirect::back()->with_errors($validation)->with_input();
}
else {
data['user'] = User::where('username', '=', Input::get('username'));
}
}
return View::make('search.index', $data);
}
}
Model : (models/user.php)
class User extends Eloquent
{
// ...
}
Related
I would like to display the user role in Symfony with the FOSUserBundle.
public function showAction() {
$model = new User();
$role = $model->getRoles();
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('#FOSUser/Profile/show.html.twig', array(
'user' => $user,
'role' => $role,
));
}
This my code in my ProfileController put that I can not call in Twig. Here is my Twig file:
<div class="fos_user_user_show">
<p><b>{{ 'Username'|trans }}</b>: {{ user.username }}</p>
<p><b>{{ 'Email'|trans }}</b>: {{ user.email }}</p>
<p><b>{{ 'Role'|trans }}</b>: {{ role.user }}</p>
try only with this:
<p><b>{{ 'Role'|trans }}</b>: {{ role }}</p>
This should also work
<p><b>{{ 'Role'|trans }}</b>: {{ user.roles }}</p>
I'm learning Laravel. I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc).
This form sends the data to a "orders" table. With the code that I have now, I'm not able to post the order on the order table. it gets the message: "MethodNotAllowedHttpException in RouteCollection.php line 233:"
This is the code
Web.php
Route::get('catalog', 'carController#catalog');
Route::get('orders/', 'CarController#orders')->name('orders');
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders(Request $request) {
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
#foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
{{ $car->Model }}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
#endforeach
The table orders has following structure:
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('Model');
$table->date('Fabrication_date');
$table->integer('Colour_id')->unsigned();
$table->foreign('Colour_id')->references('id')->on('colours')->onDelete('cascade')->onUpdate('cascade');
$table->integer('Order_status_id')->unsigned();
$table->foreign('Order_status_id')->references('id')->on('order_status')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
Update your orders function
public function orders(Request $request)
{
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
Catalog.blade.php
Form::hidden('modelesc', $car->Model)
The value of your hidden input will be read by request->modelesc and you will endup with a URL that looks like this http://mysite.dev/orders?modelesc=toyota.
Some advice
You using a form to only submit a hidden input. No user input. It seems to me it would be a easier to use simple anchors <a></a>.
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
Same result.
Make it nice with pretty URLs. Change your route definition
Route::get('orders/{modelesc}', 'CarController#orders')->name('orders');
And the anchor
{{ $car->Model }}
With named route
{{ $car->Model }}
And the function
public function orders($modelesc)
{
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
change your route to:
Route::get('orders/{model}', 'CarController#orders')->name('orders');
after this change your Controller to:
public function orders($model) {
$cars = DB::table('cars')->where('Model', $model)->get();
if(!count($cars)){
abort(404);
}
$colours = DB::table('colours')->get()->pluck('Colour');
//$status = DB::select('select * from order_status where id = ?', [1])->get(); //this variable is never used
return view('orders', compact('cars', 'colours'));
}
and now change your Catalog.blade.php
in your case you don't need a form for go in your order page
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
I am using Laravel 5.2 and trying to make a password change form with its controller. I have added the following routes:
Route::get('changepassword', array('as' => 'reset.password', 'uses' => 'PasswordController#edit'));
Route::post('resetpasswordcomplete', array('as' => 'reset.password.complete', 'uses' => 'PasswordController#update'));
The Http\Controllers\Auth\PasswordController has the following methods:
public function edit() {
return View::make('auth/passwords/change');
}
public function update() {
$hasher = Sentinel::getHasher();
$oldPassword = Input::get('old_password');
$password = Input::get('password');
$passwordConf = Input::get('password_confirmation');
$user = Sentinel::getUser();
if (!$hasher->check($oldPassword, $user->password) || $password != $passwordConf) {
Session::flash('error', 'Check input is correct.');
return View::make('auth/passwords/change');
}
Sentinel::update($user, array('password' => $password));
return Redirect::to('/');
}
The view is as such:
#if (Session::get('error'))
<div class="alert alert-error">
{{ Session::get('error') }}
</div>
#endif
{{ Form::open(array('route' => array('reset.password.complete'))) }}
{{ Form::password('old_password', array('placeholder'=>'current password', 'required'=>'required')) }}
{{ Form::password('password', array('placeholder'=>'new password', 'required'=>'required')) }}
{{ Form::password('password_confirmation', array('placeholder'=>'new password confirmation', 'required'=>'required')) }}
{{ Form::submit('Reset Password', array('class' => 'btn')) }}
{{ Form::close() }}
I get the ReflectionException error because I think the PasswordController is inside of the Auth folder and thus is only accessible to guests who want to reset their forgotten password using the auth scaffold. I would like to know how I could allow a logged in user to access this controller so that they could change their passwords if they wished?
EDIT: I tried doing the following after Alexy's solution:
public function __construct()
{
$this->middleware('guest', ['except' => ['resetpasswordcomplete', 'changepassword']]);
}
It still brings me back to home page.
Change controller path in routes.php to:
Route::get('changepassword', array('as' => 'reset.password', 'uses' => 'Auth\PasswordController#edit'));
Route::post('resetpasswordcomplete', array('as' => 'reset.password.complete', 'uses' => 'Auth\PasswordController#update'));
I have an issue with my the update method in my controller.
Normally everything works and it takes care of itself, but this time its not working.
My controller:
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, Vehicle::$rules, Vehicle::$messages);
if ($validation->passes())
{
$this->vehicle->update($id, $input);
return Redirect::route('admin.vehicles.index')->with('success', 'Car Updated');
}
return Redirect::back()
->withInput()
->withErrors($validation);
}
Repository:
public function update($id, $input)
{
print_r($id);
die();
}
This prints out:
{vehicle}
from the URI:
http://localhost/admin/vehicles/1/edit
My form:
{{ Form::open(['route' => 'admin.vehicles.update', 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}
// inputs
<div class="form-group">
{{ Form::submit('Update Vehicle', ['class' => 'btn btn-success']) }}
</div>
{{ Form::close() }}
Route:
Route::resource('/admin/vehicles', 'VehiclesController');
Where is this going wrong? How can I get this form to send the ID not the word vehicle?
I have a simple task list project under Laravel.
When I click a checkbox it does not show a checked condition. (The second item is in the true condition in the database and thus shows checked. I cannot uncheck this item) I have searched for an answer to why on the net but cannot find a solution or reason.
Code:
home.blade.php (in views folder) -
#extends('layouts.main')
#section('content')
<h1>Tasks</h1>
<ul>
#foreach ($items as $item)
<li>
{{ Form::open() }}
<input type="checkbox" onClick="this.form.submit()" {{ $item->done ? 'checked' : '' }}>
<input type="hidden" name="id" value="{{ $item->id }}">
{{ $item->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
#stop
HomeController.php (inControllers folder) -
<?php
class HomeController extends BaseController {
public function getIndex() {
$items = Auth::user()->items;
return View::make('home', array(
'items' => $items
));
}
public function postIndex() {
$id = Input::get('id');
$user_id = Auth::user()->id;
$item = Item::findOrFail($id);
if($item->owner_id == $userId) {
$item->mark();
}
return Redirect::route('home');
}
}
Item.php (in models folder) -
<?php
class Item extends Eloquent {
public function mark() {
$this->$done = $this->done ? false : true;
$this->save();
}
}
routes.php -
<?php
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#getIndex'))->before('auth');
Route::post('/', array('uses' => 'HomeController#postIndex'))->before('csrf');
Route::get('/login', array('as' => 'login', 'uses' => 'AuthController#getLogin'))->before('guest');
Route::post('login', array('uses' => 'AuthController#postLogin'))->before('csrf');
in your code, you never update the model's done value. i assume, you want to change it with the post method. so you'd need to take the value from the checkbox name (e.g. Input::get('box-ID'))
you could also create a checkbox using the form class:
// public function checkbox($name, $value = 1, $checked = null, $options = array())
{{ Form::checkbox('name', 'value', true, ['onClick' => 'alert(123)']) }}
reference: Formbuilder -> checkbox
You should modify your form like this. It works me I hope will work for you also.
{{ Form::open(['route' => ['items.update', $items->id], 'class' => 'form-inline', 'method' => 'put']) }}
Thanks