So I have this Editform in my office page the problem is when I press the edit button it says this
Missing required parameters for [Route: editoffice] [URI: building/{id}/offices/{office_id}/edit]. (View: C:\xampp\htdocs\Eguide\resources\views\editoffice.blade.php)
Routes
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController#edit')->name('editofficeform');
Route::post('building/{id}/offices/{office_id}/edit', 'OfficeController#update')->name('editoffice');
Building.blade.php
This is the code for the edit button
Edit
OfficeController.php
public function edit(Request $request, $id)
{
$office_id = $request->get('office_id');
$office = Office::find($office_id);
return view('editoffice')->withOffice($office)->with('id',$id);
}
public function update(Request $request, $id)
{
$office = Office::find($id);
$office->name =$request->officename;
$office->floor = $request->floor;
$office->update();
\Session::flash('building_flash', 'Updated successfully!');
return redirect()->back();
}
editoffice.blade.php
#extends('layouts.main')
#section('title', 'Create an Office')
#section('content')
{!! Form::open(array('route' => ['editoffice', $id], 'class' => 'form')) !!}
<div class="container">
<div class="form-group">
{!! Form::label('Office Name') !!}
{!! Form::text('officename', $office->name, array('required',
'class'=>'form-control',
'placeholder'=>'Office Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Office Floor') !!}
{!! Form::text('floor', $office->floor, array('required',
'class'=>'form-control',
'placeholder'=>'Office Floor')) !!}
</div>
<div class="form-group">
{!! Form::submit('Update Office',
array('class'=>'btn btn-primary')) !!}
Back
</div>
{!! Form::close() !!}
#endsection
Whats wrong with my code?
Change the form to:
{!! Form::open(array('route' => ['editoffice', [$id, $office->id]], 'class' => 'form')) !!}
Also, change the edit and update methods to:
public function edit($id, $office_id) {
$office = Office::find($office_id);
return view('editoffice', compact('office', 'id'));
}
public function update(Request $request, $id, $office_id)
Related
Hey guys how do u pass a variable between a function within the same controller? I have tried making a global variable and using Session:: to set and get the values but neither of the method works. I am getting the values of the start_date and end_date from my generate.blade.php and pass the value to my downloadPDF function to filter the data based on the date range. Anyone able to enlighten me how can i accomplish this?
GenerateReportController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Attendance;
use App\Subject;
use PDF;
use Session;
use View;
class GenerateReportController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public $start_date;
public $end_date;
public function index()
{
$this->start_date = Input::get('startDate');
$this->end_date = Input::get('endDate');
$subjects = Subject::all();
return View::make('generate', compact('subjects',$subjects));
}
public function downloadPDF()
{
$dateBetween = Attendance::whereBetween('date',array($this->start_date, $this->end_date))->get();
//dd($dateBetween);
$pdf = PDF::loadView('pdf',compact('dateBetween'));
$name = "Attendance Report";
return $pdf->stream($name.'.pdf');
}
}
generate.blade.php
#extends('master')
#section('page_header')
<div class="container-fluid">
<h1 class="page-title">Attendance Records</h1>
<a href="/dashboard/attendance/report/" target="_blank" class="btn btn-primary">
<i class="voyager-list" style="font-size:15px;"></i>
<span>Generate Report</span>
</a>
</div>
#endsection
#section('content')
<div class="page-content browse container-fluid">
<div class="row">
<div class="col-md-12">
<div class="panel panel-bordered">
<div class="panel-body">
{!! Form::Label('subject', 'Subject:') !!}
<select class="form-control" name="s_name">
#foreach($subjects as $subject)
<option value="{{$subject->s_name}}">{{$subject->s_name}}</option>
#endforeach
</select>
<br>
{!! Form::Label('startDate', 'Start Date:') !!}<br>
{!! Form::input('date', 'startDate', null,['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::input('date', 'endDate', null, ['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
</div>
</div>
</div>
</div>
</div>
#endsection
web.php
Route::get('dashboard/attendance/generate','GenerateReportController#index');
Route::get('dashboard/attendance/report','GenerateReportController#downloadPDF');
SOLVED
Instead of passing the variable one a function to another. Use a post method to send the $request to the second controller and use $request->name to get the value.
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Attendance;
use App\Subject;
use Session;
use PDF;
use View;
class GenerateReportController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$subjects = Subject::all();
return View::make('generate', compact('subjects'));
}
public function downloadPDF(Request $request)
{
$dateBetween = Attendance::whereBetween('date',array($request->startDate,$request->endDate))->where('s_code',$request->id)->get();
$pdf = PDF::loadView('pdf',compact('dateBetween'));
$name = "Attendance Report";
return $pdf->stream($name.'.pdf');
}
Blade View
{!! Form::open(['action'=>'GenerateReportController#downloadPDF','target' => '_blank']) !!}
{!! Form::Label('subject', 'Subject:') !!}
<select class="form-control" name="id">
#foreach($subjects as $subject)
<option value="{{$subject->id}}">{{$subject->s_name}}</option>
#endforeach
</select>
<br>
{!! Form::Label('startDate', 'Start Date:') !!}<br>
{!! Form::date('startDate', 'startDate',['id' => 'datetimepicker','class' => 'datepicker']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::date('endDate', 'endDate',['id' => 'datetimepicker','class' => 'datepicker']) !!}
<br>
<br>
{!!Form::submit('Generate PDF',['class' => 'btn btn-primary'])!!}
{!! Form::close() !!}
web.php
Route::get('dashboard/attendance/generate','GenerateReportController#index');
Route::post('dashboard/attendance/report','GenerateReportController#downloadPDF');
Try like below
In Route
Route::get('dashboard/attendance/report','GenerateReportController#downloadPDF');
First add this facade in your controller
use Illuminate\Http\Request;
After convert your function to
public function downloadPDF($request)
{
//try to print first dd($request->all())
$dateBetween = Attendance::whereBetween('date',array($request->start_date, $request->end_date))->get();
//dd($dateBetween);
$pdf = PDF::loadView('pdf',compact('dateBetween'));
$name = "Attendance Report";
return $pdf->stream($name.'.pdf');
}
Form
{!! Form::open(['action'=>'GenerateReportController#downloadPDF']) !!}
{!! Form::Label('subject', 'Subject:') !!}
<select class="form-control" name="s_name">
#foreach($subjects as $subject)
<option value="{{$subject->s_name}}">{{$subject->s_name}}</option>
#endforeach
</select>
<br>
{!! Form::Label('startDate', 'Start Date:') !!}<br>
{!! Form::input('date', 'startDate', null,['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
<br>
<br>
{!! Form::Label('endDate', 'End Date:') !!}<br>
{!! Form::input('date', 'endDate', null, ['id' => 'datetimepicker','class' => 'datepicker', 'data-date-format' => 'yy/mm/dd']) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
There is a bug in the code somewhere and I cant seem to find it , any help would be great. Its a simple form that stores to a DB.
The app is using laravel 5.2 and all that is needed is to collect data. when the submit button is hit on the form nothing!
route
Route::resource('/form' , 'PagesController');
controller only needs to indes, create and store , thats all the app needs to do.
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use Data;
use App\Http\Requests\DataRequest;
use Carbon\Carbon;
class PagesController extends Controller
{
//Display Index
public function index()
{
return view ('welcome');
}
public function create()
{
return view ('create');
}
//Store Articles from form
public function store(DataRequest $request)
{
Data::create($request->all());
return redirect('create')->with('message' , 'Form submitted');
}
}
model uses a simple protected fields list and carbon to store timestamps
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests\DataRequest;
use Carbon\Carbon;
class Data extends Model
{
protected $fillable = [
'name',
'email',
'phone',
'company',
'addcomments',
'published_at',
];
protected $dates = ['published_at'];
//Get all published articles by date
public function scopePublished($query)
{
$query->where('published_at' , '<=' , Carbon::now());
}
//Get all unpublished or future articles
public function scopeUnpublished($query)
{
$query->where('published_at' , '>=' , Carbon::now());
}
// Set form to publish articles with a time and date in the Published_at field
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d' , $date);
}
}
form
{!! Form::open(['url' => 'form']) !!}
<div class="form-group">
{!! Form::label('name' , 'Name:') !!}
{!! Form::text('name', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email' , 'EMail:') !!}
{!! Form::text('email', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone' , 'Phone Number:') !!}
{!! Form::text('phone', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('company' , 'Company:') !!}
{!! Form::text('company', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('addcomments' , 'Additional Comments:') !!}
{!! Form::textarea('addcomments', null , ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Submit Now', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
So the issue was what was mentioned in the comments
use DATA;
to
use App\DATA;
and a mixture of a few little front end tweaks
all sorted
I created a form for myself and it was working perfect. I did some rules like: if the user haven't wrote anything in the form or haven't wrote enough, give me some error message. This was working while used the 'former' package instead of the normally used 'form' package. Now I changed my form from "Former" to "Form" and now my error message are gone.. Well my form works and the rules too. If I don't write anything in my form or not at least 3 characters in the title/content section, it should redirect me with the errors I need. This works, but without the error message.
Here is my form:
#extends('master')
#section('content')
{!! Form::open(array('action' => 'Test\\TestController#store')) !!}
<div class="form-group">
{!! Form::label('thread', 'Title:') !!}
{!! Form::text('thread', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Body:') !!}
{!! Form::textarea('content', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add Thread', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#stop
My Controller#store function:
public function store(StoreRequest $request){
Thread::create($request->all());
return redirect(action('Test\\TestController#index'));
}
my Model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
and now the rules ( StoreRequest.php ) :
<?php
namespace App\Http\Requests\Store;
use App\Http\Requests\Request;
class StoreRequest extends Request {
public function rules() {
return [
'thread' => 'required|min:3',
'content' => 'required|min:3'
];
}
public function authorize() {
return true;
}
}
The error message was getting automaticly generated. Laravel did that for me. But not anymore.
Does anybody see why?
You can check if the form returned any error with something along these line:
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
Or you could use BootForm and let it handle everything for you
i try to set rule to validate post request for laravel form to store data but if i set any validation rule ( either pass or fail ) it will show just blank empty page .
i using laravel 5
the form ( view ):
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::open(['action' => 'RentalAgreementController#store', 'method' => 'post']) !!}
{!! Form::label('dateOfAgreement', 'Date of Agreement', ['class' => 'control-label']) !!}
{!! Form::input('date', 'dateOfAgreement' , date('Y-m-d') , ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'button']) !!}
{!! Form::close() !!}
Controller
public function store(StoreAgreementPostRequest $request)
{
$agreement= new RentalAgreement(array(
'dateOfAgreement' => $request->dateOfAgreement,
));
$agreement->save();
Session::flash('flash_message', 'Agreement successfully added! ');
return view('home');
}
StoreAgreementPostRequest
class StoreAgreementPostRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
'premiseUse'=>'required|max:5'
];
}
}
just shows the black page but if i remove the rules its works . but for all other forms in this application validation way works ,( all other forms and controllers works perfectly).
When a user is created a random password (and a auth code) will be created and send with an email to the user.
When clicked on the link in the email the user status will be 1 (so active) and the user will be able to change his password right away.
Now it doesn't work as I want to.
UserController:
public function store(CreateUserRequest $request, User $user, Attribute $attribute)
// some unnecessary code
if ((Input::get('usertype_id')) > 1) {
$randomPassword = str_random(8);
$user->password = Hash::make($randomPassword);
$authentication_code = str_random(12);
$user->authentication_code = $authentication_code;
$user->active = 0;
};
$user->save();
if ((Input::get('usertype_id')) > 1) {
// Email sturen met verficatie code
$email = Input::get('email');
Mail::send('emails.user', ['user' => $user, 'password' => $randomPassword, 'authentication_code' => $authentication_code], function ($message) use ($email) {
$message->to($email, 'Lilopel')->subject('Lilopel: Verify your account!');
});
};
public function confirmUser($authentication_code)
{
if (!$authentication_code)
{
return 'auth code not found!';
}
$user = User::where('authentication_code', '=', $authentication_code)->first();
if (!$user)
{
return 'user not found!';
}
$user->active = 1;
$user->save();
Session::put('user_id', $user->id);
return view('user.setpassword', ['user' => $user]);
//return redirect()->route('user.setPassword', [$user_id]);
}
public function setPassword(SetPasswordRequest $request)
{
$user_id = Session::get('user_id');
$user = $this->user->find($user_id);
$user->fill($request->only('password'));
$user->save();
}
Route:
Route::get('user/verify/{authenticationCode}', 'UserController#confirmUser');
Route::get('user/password', 'UserController#setPassword');
View:
{!! Form::model($user, ["route"=>['user.setPassword', $user->id] , "method" => 'PATCH']) !!}
<div class="form-group {{ $errors->has('password') ? 'has-error' : '' }}">
{!! Form::label('password', trans('common.password'), ['class' => 'form-label col-sm-3 control-label
text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password', ['name' => 'password', "class"=>"form-control","placeholder" =>
trans('common.password') ]) !!}
{!! $errors->first('password', '<span class="help-block">:message</span>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? 'has-error' : '' }}">
{!! Form::label('password_confirmation', trans('common.confirmpassword'), ['class' => 'form-label
col-sm-3 control-label text-capitalize']) !!}
<div class="col-sm-6">
{!! Form::password('password_confirmation', ['name' => 'password_confirmation',
"class"=>"form-control","placeholder" => trans('common.confirmpassword') ]) !!}
{!! $errors->first('password_confirmation', '<span class="help-block">:message</span>') !!}
</div>
</div>
{!! Form::submit( trans('common.edit'), ["class"=>"btn btn-primary text-capitalize center-block "]) !!}
{!! Form::close() !!}
Email links works, the status of the user gets active, but then the .blade will give a Route [user.setPassword] not defined. (View: public_html/server2/resources/views/user/setpassword.blade.php) error.
work togetherTo use the route as you do, you need a named route.
Change this
Route::get('user/password', 'UserController#setPassword');
to this
Route::get('user/password', [
'as' => 'user.setPassword',
'uses' => 'UserController#showProfile'
]);
Also, make sure the HTTP verbs of the route and your form's method work together.