laravel 5.2 Restfull Api with admin panel stop working suddenly - laravel-5

I developed admin panel to add,edit,delete users in my website with Restfull Api. edit and delete work fine but add not.
when I add user it may work or this error may appear
The localhost page isn’t working
localhost is currently unable to handle this request.
This is my routes
Route::resource('admin/users','AdminUser');
This is store function in Admin user resource
public function store(Request $request)
{
//rules
$rules = array(
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6'
);
/*
validate the data user
**/
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return view('admin.User.create_user')
->withErrors($validator)
->withInput(['page' => 'home']);
}
/*
Store the data user in the database
**/
$user = new User;
$user->name =$request->input('name');
$user->email = $request->input('email');
$user->password=bcrypt($request->input('password'));
$user->role='user';
$user->save(); //error here
//redirect
return redirect('admin/users')->with('message', 'Successfully added user!');
}
And this is create_user.blade.php
#extends('layouts.layout')
#section('content')
<section id="advertisement">
<div class="container">
<img src="{{asset('images/shop/advertisement.jpg')}}" alt="" />
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="left-sidebar">
#include('shared.sidebaradmin')
</div>
</div>
<div class="features_items"><!--features_items-->
<h2 class="title text-center">Add New Product</h2>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('url' => 'admin/users')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Add a new user !', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div><!--features_items-->
</div>
</div>
</section>
#endsection

Related

Laravel view load twice to display comment

I don't know why the page has to load twice to display comments.
Here is my route: Route::post('/addComment', 'CommentsController#addComment');
Here is my controller:
public function addComment(Request $request)
{
$this->validate($request, [
'name' => 'required',
'body' => 'required',
]);
$lesson_id = $request->lesson_id;
$comment = new Comment;
$comment->name = $request->input('name');
$comment->body = $request->input('body');
$comment->parrent_id = '0';
$comment->lesson_id = $request->lesson_id;
$comment->save();
return back();
}
Here is my view:
<div class="leave_review">
<h3 class="blog_heading_border"> コメント </h3>
{!! Form::open(['action' => ['CommentsController#addComment'], 'method' => 'POST', 'id' => 'postForm' ]) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" id ="lesson_id" name="lesson_id" value="{{$lesson->id}}" />
</div>
<div class="row">
<div class="col-sm-6">
#error('name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('name','名前')}}
{{Form::text('name', '', ['class' => 'form-group', 'id' => 'name' ]) }}
</div>
<div class="col-sm-12">
#error('body')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('body','メッセージ')}}
{{Form::textarea('body', '', ['class' => 'form-group', 'id' => 'body']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
{{Form::submit('Submit', ['class' => 'send mt_btn_yellow pull-right', 'id' => 'submit'])}}
{!! Form::close() !!}
{{-- End add comment --}}
{{--Display comment--}}
<ol class="review-lists">
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
you don't have a #endforeach

Several updates on the same view Laravel

In a setup process I want to:
select langs of the application (1 or more) ...
Update the DB
Select the default language
Update again the DB...
For this i created 3 routes.
Route::get('/home/setup', 'BackOffice\FirstconnectionController#initLang');
Route::patch('/home/setup', 'BackOffice\FirstconnectionController#initLangUpdate')->name('setup.setLang');
Route::patch('/home/setup', 'BackOffice\FirstconnectionController#setDefaultLang')->name('setup.setDefaultLang');
The first is the home page where i make eloquent requests
The second route display the list of languages
The third route displays the list of languages which are published ...
Here is my view :
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
{{-- IF NO LANGS ARE PUBLISHED I CAN CHOOSE HERE --}}
#if ($langsCount == 0)
{!! Form::model($langs, [
'method' => 'PATCH',
'route' => 'setup.setLang'
])
!!}
#foreach($langs as $lang)
<div class="form-group">
{{--<label class="col-md-4"> {{ $lang->langname }} </label>--}}
{{--<input id="{{ $lang->langisocode }}" type="checkbox">--}}
{!! Form::label($lang->langname, $lang->langname ) !!}
{!! Form::checkbox( 'lang[]', $lang->id ) !!}
</div>
#endforeach
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Valider</button>
</div>
{!! Form::close() !!}
{{-- NOW I SELECT DEFAULT LANGUAGE... --}}
#else
{!! Form::model($langs, [
'method' => 'PATCH',
'route' => 'setup.setDefaultLang'
])
!!}
#foreach($pubLangs as $pubLang)
{!! Form::label($pubLang->langname, $pubLang->langname ) !!}
{!! Form::radio( 'lang', $pubLang->id ) !!}
<br>
#endforeach
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Valider</button>
</div>
{!! Form::close() !!}
#endif
Here is my controller :
// I display the info here
public function initLang()
{
$langs = Lang::onlyTrashed()->get();
$langsCount = Lang::count();
$pubLangs = Lang::all();
return view('admin.firstConnection', compact('langs', 'langsCount', 'pubLangs'));
}
public function initLangUpdate(Request $request) {
$request = $request->input('lang');
foreach ($request as $entry) {
Lang::withTrashed()->find($entry)->restore();
}
return redirect('admin/home/setup')->with('success', 'OK');
}
public function setDefaultLang(Request $request) {
$request = $request->input('lang');
return $request;
}
I will update the setDefaultLang after ...
I have this error message :
Route [setup.setLang] not defined

displaying specific error messages in laravel 4.2

hello i have this form and i have validations for it. I have already finished doing the validations for in inside my controller and i can already display the error messages in my view but i wanted the error message to be beside the input area where it came from
here is my code in the view
{{ Form::open(array('url' => 'addParentAccnt')) }}
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
</div>
<div class="form-group">
{{ Form::label('lName', 'Last Name') }}
{{ Form::text('lName', Input::old('lName'), array('class' => 'form-control','placeholder' => 'Insert Last Name')) }}
</div> {{ Form::submit('Proceed to Next Step', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
in the bottom of my view i added this code to display the error messages
#if ($errors->any())
<ul>
{{ implode('', $errors->all('<p style="color:red" class="error">:message</p>')) }}
</ul>
#endif
the code inside my controller is this
$rules = array
(
'username' => 'required|min:10|max:50',
'fName' => 'required|alpha|min:1|max:80',
'lName' => 'required|alpha|min:1|max:80',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('createPa')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//do something
}
Change your view as following:
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
{{ $errors->first('username', ':message') }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
{{ $errors->first('fName', ':message') }}
</div>

Creating a task with the task list id as foreign

I've created a task list which displays several tasks. I can create lists in my page and the task itself, but, I'm receiving an error on the constraint as it has to come with the ID of the task that it's being created.
Currently the task is created under a loop in the view on laravel 4 (tasks/index.blade.php).
#foreach ($tasks_lists as $task_list)
<div class="col-lg-3 col-md-4 container_tasks">
<h4>{{ $task_list->title }} <small>(Nueva tarea)</small><button class="close" type="button" data-toggle="modal" data-target=".delete-task-list-modal{{ $task_list->id }}">×</button></h4>
<ul>
#foreach ($task_list->tasks as $task)
<li>
{{ Form::open(array('url' => 'tareas.marcar', 'class' => 'createTaskList')) }}
<input
type="checkbox"
onclick="this.form.submit()"`
{{ $task->done ? 'checked' : '' }}
/>
<input type="hidden" name="id" value="{{ $task->id }}" />
{{ $task->name }}
{{ Form::close() }}
</li>
#endforeach
</ul>
</div>
<div class="modal fade create-task-modal{{ $task_list->id }}" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Crear lista de tareas</h4>
</div>
<div class="modal-body">
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
<div class="form-group">
{{ Form::label('name', 'Nombre') }}
{{ Form::text('name', Input::old('name'), array('class' => 'form-control')) }}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
{{ Form::submit('Crear tarea', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
And this is my controller (TaskController.php):
public function store()
{
$task_list =
// Reglas de validación
$rules = array(
'name' => 'required|min:3|max:30'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('tareas')
->withErrors($validator)
->with('notice_type', 'danger')
->with('notice', 'Los siguientes errores ocurrieron:')
->withInput();
} else {
// store
$task = new Task;
$task->name = Input::get('name');
$task->save();
return Redirect::to('tareas')
->with('notice_type', 'success')
->with('notice', 'La tarea ha sido agregado satisfactoriamente.');
}
}
How would I go about adding the ID of the current task list when creating the task, so that the task is added properly to its list when clicking the .create-task-modal
Lucus
The easist way would be to add a hidden field after
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
so it's like
{{ Form::open(array('url' => 'tareas', 'class' => 'createTask')) }}
{{ Form::hidden('task_list_id', $task_list->id) }}
then, in your controller do
$rules = array(
'task_list_id' => 'required|exists:task_lists,id', //check the docs for exists
'name' => 'required|min:3|max:30'
);
...
// option 1
$task = new Task;
$task->task_list_id = Input::get('task_list_id');
$task->name = Input::get('name');
$task->save();
// option 2 (depending on your fillable
$task = new Task(Input::only('task_list_id', 'name');
$task->save();

alpha and alpha_num validation not working in laravel 4?

I am developing a registration form. Everything is working fine but I am having problem with alpha and alpha_num validation.
Here is my Model(employee.php)
<?php
class Employee extends Eloquent
{
protected $table='employee';
public static $rules = array(
'first_name' => 'required|min:2',
'last_name' => 'required|min:2',
'username' => 'required|min:3|unique:employee',
'password' => 'required|alpha_num|between:6,12|confirmed',
'confirm_password' => 'required|aplha_num|between:6,12',
'department' => 'required',
'post' => 'required'
);
}
?>
Here is my controller (EmployeeController.php)
public function store()
{
//validate the user input
$validator = Validator::make(Input::all(),Employee::$rules);
//process the login
if ($validator->fails())
{
return Redirect::to('employee/create')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//store
$employee = new Employee;
$employee->first_name = Input::get('first_name');
$employee->last_name = Input::get('last_name');
$employee->username = Input::get('username');
$employee->password = Hash::make(Input::get('password'));
$employee->department = Input::get('department');
$employee->post = Input::get('post');
$employee->save();
//redirect to the main page
Session::flash('message','Successfully added employee');
return Redirect::to('employee');
}
}
My create.blade.php file
#extends('layouts.master')
#section('navigation')
#parent
#stop
#section('content')
<h1>Add New Employee</h1>
<!--if there are any errors,display here-->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url'=>'employee','class'=>'form-horizontal')) }}
<div class="control-group">
{{ Form::label('first_name','Enter First Name',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('first_name',Input::old('first_name'),array('placeholder'=>'First Name')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('last_name','Enter Last Name',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('last_name',Input::old('last_name'),array('placeholder'=>'Last Name')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('username','Enter Username',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::text('username',Input::old('username'),array('placeholder'=>'Username')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('password','Enter Password',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::password('password',array('placeholder'=>'Password')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('confirm_password','Confirm Password',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::password('confirm_password',array('placeholder'=>'Password')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('department','Select Department',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::select('department',array('it'=>'IT','finance'=>'finance','management'=>'Management','marketing'=>'Marketing')) }}
</div>
</div>
<div class="control-group">
{{ Form::label('post','Enter Post',array('class'=>'control-label')) }}
<div class="controls">
{{ Form::select('post',array('E'=>'Employee','PM'=>'Project Manager','CEO'=>'Executive CEO','MD'=>'Managing Director')) }}
</div>
</div>
<div class="control-group">
<div class="controls">
{{ Form::submit('Register',array('class'=>'btn btn-success')) }}
</div>
</div>
{{ Form::close() }}
#stop
I am getting BadMethodCallException
Method [validateAplhaNum] does not exist.
Method [validateAplha] does not exist.
It's because you've spelt alpha_num wrong in your confirm_password validation.

Resources