Laravel pass id from blade template to store controller - laravel

I am trying to pass id to store controller as I need to save business_id that is being retrieved from another table. However I get:
Missing argument 2 for App\Http\Controllers\EventController::store()
Here's my view:
#extends('master') #section('title', 'Live Oldham')
#section('content')
<div class="col-lg-y col-lg-offset-3">
<ul class="list-group-list">
#foreach ($business->businesses as $business)
<li class="list-group-item">
<a target="_blank" href="{{ url('business/' . $business->id) }}"> {{($business->name) }}</a>
</li>
#endforeach
</ul>
</div>
#endsection
Controller:
class EventController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$id = Auth::id();
$business = User::where('id', $id)
->with('businesses')
->first();
return view('events.viewEvent', compact('business'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($id)
{
return view('events.addEvent')
->with('Business', Business::find($id));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request, $id)
{
$event = new Event;
$event->startdate = $request->input('startdate');
$event->enddate = $request->input('enddate');
$event->title = $request->input('title');
$event->frequency = $request->input('frequency');
$event->description = $request->input('description');
$event->business_id = $id;
$event->save();
}
form:
#extends('master') #section('title', 'Live Oldham')
#section('content')
<div class="container">
<!-- Alert Messages -->
#if (session('message'))
#if (session('message')=="success")
<div class="alert alert-success">
Event Created
</div>
#else
<div class="alert alert-danger">
There has been a fatal error! Apologies, we are working to fix it!
</div>
#endif
#endif
<!-- JQuery UI init -->
<script>
$( function() {
$( document ).tooltip();
} );
</script>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Create Event</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ action('EventController#store') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<div style="display:none;" class="title_message form-control alert-warning"></div>
<label for="title" class="col-md-4 control-label">Event Title</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control" placeholder="Event title"
title="What is the event title?" name="title" value="{{ old('title') }}">
#if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('frequency') ? ' has-error' : '' }}">
<div style="display:none ;" class="frequency_message form-control alert-warning"></div>
<label id="frequency2" for="frequency" class="col-md-4 control-label">Frequency</label>
<div class="col-md-6">
<select class="form-control" name="frequency" id="frequency">
<option selected disabled>Choose event frequency...</option>
<option value="One">One-time Event</option>
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Yearly">Yearly</option>
</select>
#if ($errors->has('frequency'))
<span class="help-block">
<strong>{{ $errors->first('frequency') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('startdate') ? ' has-error' : '' }}">
<div style="display:none ;" class="startdate_message form-control alert-warning"></div>
<label id="startdate2" for="startdate" class="col-md-4 control-label">Event Start Date</label>
<div class="col-md-6">
<input id="startdate" type="date" class="form-control" placeholder="Start Date"
title="When does the event start?" name="startdate" value="{{ old('startdate') }}">
#if ($errors->has('startdate'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('enddate') ? ' has-error' : '' }}">
<div style="display:none ;" class="enddate_message form-control alert-warning"></div>
<label id="address3" for="enddate" class="col-md-4 control-label">Event End Date</label>
<div class="col-md-6">
<input id="enddate" type="date" class="form-control" placeholder="End Date"
title="When does the event end?" name="enddate" value="{{ old('enddate') }}">
#if ($errors->has('enddate'))
<span class="help-block">
<strong>{{ $errors->first('enddate') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<div style="display:none ;" class="description_message form-control alert-warning"></div>
<label id="description2" for="description" class="col-md-4 control-label">Event Description</label>
<div class="col-md-6">
<textarea id="description" type="text" class="form-control" placeholder="Event description"
title="Here goes event description" name="description" value="{{ old('description') }}">
</textarea>
#if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" id="submit" class="btn btn-success">
Add Event
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection

You can pass the id to the form action
<form method="POST" action="{{ action('EventController#store', $business->id) }}" class="form-horizontal" role="form">
...
</form>

Related

Laravel Excel import using Maatwebsite Excel package with additional columns from View

I am trying to import data into MySQL from an Excel file. My table has 2 foreign keys project_id and site_id when importing I am selecting these 2 fields from dropdowns in my View. Is there a way I can map these 2 fields to my import collection? Mind you, the 2 fields do not exist in the import file (for integrity reasons) but they do exist in the table.
Collection
namespace App\Imports;
use App\Proposal;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ProposalsImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
return new Proposal([
'building' => $row['building'],
'floor' => $row['floor'],
'area' => $row['area'],
'room' => $row['room'],
'luminaire' => $row['luminaire'],
'actual_qty' => $row['actual_qty'],
'installed_qty' => $row['installed_qty'],
]);
}
}
Controller
public function import()
{
Excel::import(new ProposalsImport, 'proposals.xlsx');
}
View
#extends('projectmanagement/proposals.base')
#section('action-content')
<!-- Main content -->
<section class="content">
<div class="container">
<div class="box">
<div class="box-header">
</div>
<!-- /.box-header -->
<div class="box-body" data-widget="box-refresh">
#if (session('status'))
<div style="padding-top: 0px;padding-bottom: 0px;"
class="alert alert_cust alert-success alert-dismissable fade in">{{ session('status') }}×</div>
#endif
</div>
<div class="row">
<div class="col-lg-12">
<form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="{{ route('proposals.store') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12">
<div class="form-group{{ $errors->has('project_id') ? ' has-error' : '' }}">
<label for="project_id" class="col-md-4 control-label">Project Name</label>
<div class="col-md-6">
<select id="project_id" class="form-control select2" style="width: 100%;"
name="project_id">
<option value="0" disabled selected>Select Project</option>
#foreach ($projects as $project)
<option value="{{$project->id}}">{{ $project->project_name }}</option>
#endforeach
</select>
#if ($errors->has('project_id'))
<span class="help-block">
<strong>{{ $errors->first('project_id') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group{{ $errors->has('site_id') ? ' has-error' : '' }}">
<label for="site_id" class="col-md-4 control-label">Site Name</label>
<div class="col-md-6">
<select id="site_id" class="form-control select2" style="width: 100%;"
name="site_id">
<option value="0" disabled selected>Select Site</option>
#foreach ($sites as $site)
<option value="{{$site->id}}">{{ $site->site_name }}</option>
#endforeach
</select>
#if ($errors->has('site_id'))
<span class="help-block">
<strong>{{ $errors->first('site_id') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group{{ $errors->has('proposal_file') ? ' has-error' : '' }}">
<label for="proposal_file" class="col-md-4 control-label">Proposal File</label>
<div class="col-md-6">
<input id="proposal_file" type="file" class="form-control" name="proposal_file" required autofocus>
#if ($errors->has('proposal_file'))
<span class="help-block">
<strong>{{ $errors->first('proposal_file') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group">
<div class="col-md-4">
</div>
<div class="col-md-6">
<button type="submit" class="btn col-sm-3 col-xs-5 btn-primary">Upload Proposal</button>
</div>
</div>
<br/>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
#push('custom_scripts')
<script>
</script>
#endpush
#endsection
Override import controller's construct method, sending all the parameters you need, like this:
class ProposalsImport implements ToModel, WithHeadingRow
{
protected $project_id;
protected $site_id;
public function __construct($project_id, $site_id)
{
$this->project_id = $project_id;
$this->site_id = $site_id;
}
public function model(array $row)
{
return new Proposal([
'building' => $row['building'],
'project_id' => $this->project_id,
'site_id' => $this->site_id
]);
}
}
And then, call it from your Controller like this:
public function import(Request $request)
{
Excel::import(new ProposalsImport($request->project_id, $request->site_id), 'proposals.xlsx');
}

Catch ONLY the invalid credentials error in a login.blade Laravel

I am using the custom login controller from Laravel. I have the validation messages in a below the input fields.
Everything works fine.
What I want now is to show a message “incorrect credentials” ONLY when user or password are incorrect and in a different div. I mean, if other validation error triggers, this message should not be visible.
The errors->has(‘email’) array catches this error but also the rest, for instance, ‘the field is required’.
Does anybody know how to write a condition that only catches this ‘invalid credentials’ error message?
Below the template.
Thanks in advance for your help!
#extends ('layouts.default')
#section('content')
#if ($errors->has('email')) {{-- I want the credential error here, but only
for credential error is triggered --}}
<div class="warning">
<div class="input-icon">
<i style="font-size:1.5em; color:Tomato; margin-right:5px;" class="fas
fa-exclamation-triangle"></i>
</div>
<p>Usuario o contraseña incorrecta</p>
</div>
#endif
<main class="login-page">
<div class="contact login">
<div class="titulos">
<p>Ingresar</p>
<p>Soy nuevo</p>
</div>
<form method="post">
#csrf
<div class="input-group input-group-icon">
<input type="email" name="email" placeholder="Correo electrónico"
value="{{ old('email') }}" autofocus/>
<div class="input-icon">
<i class="fas fa-envelope"></i>
</div>
<span class="obligatorio" > {{ $errors->first('email') }}</span>
</div>
<div class="input-group input-group-icon">
<input type="password" name="contraseña"
placeholder="Contraseña"/>
<div class="input-icon">
<i class="fas fa-lock"></i>
</div>
<span class="obligatorio" >{{ $errors->first('contraseña') }}
<div class="input-group">
<input type="submit" value="Ingresar" />
<a href="{{ route('password.request') }}">Olvidé mi
contraseña</a>
</div>
<div>
<label>
<input type="checkbox" name="recordar" id="cbox1"
value="recordar" {{ old('recordar') ? 'checked' : '' }}>
<span>Recordar mi usuario</span>
</label>
</div>
</form>
</div>
</main>
#endsection
In an old beginner Project of mine I did it this way:
<div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">#lang('views/auth/login.username')</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name">
#if ($errors->has('text'))
<span class="help-block">
<strong>{{ $errors->first('text') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">#lang('views/auth/login.password')</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
And inside the Controller I just catched the Exceptions:
public function authenticate(Request $request)
{
try {
$this->ldapHelper->checkCredentials($request->name, $request->password);
$ldapUserData = $this->ldapHelper->getFormattedUserData(request('name'));
$this->sessionService->login($ldapUserData);
return redirect()->route('form.formular');
} catch (\Exception $e) {
return back()->withInput()->withErrors([
'message' => $e->getMessage(),
]);
}
}
Maybe it helps!

Missing required parameters for Route with custom password reset

I'm trying to add new type of users "Client" into existing project.
It already had "Admin" user, which does not have email password reset function.
I duplicated and modified native Laravel out of the box Auth controllers, blade forms and model and now I can login and logout as Client (guard:client).
My problem is in password reset function.
"Forgot Your Password?" button redirects to the right url (http://127.0.0.1:8000/client/password/reset).
"Send Password Reset Link" button generates email with link (http://127.0.0.1:8000/client/password/reset/a7558b0f294af7cdaeafd73617f664e1d76ed27d567d648e8b468eb9edcc9c2d)
But when push that link I get following error:
Missing required parameters for [Route: client.password.reset] [URI:
client/password/reset/{token}]. (View:
C:\wamp64\www\testkz\resources\views\index\client\reset.blade.php)
Where reset.blade.php is a copy of resources\views\auth\passwords\reset.blade.php
My routes
Route::post('/password/email','Client\ClientForgotPasswordController#sendResetLinkEmail')->name('client.password.email');
Route::get('/password/reset','Client\ClientForgotPasswordController#showLinkRequestForm')->name('client.password.request');
Route::post('/password/reset','Client\ClientResetPasswordController#reset');
Route::get('/password/reset/{token}','Client\ClientResetPasswordController#showResetForm')->name('client.password.reset');
ClientResetPasswordController
<?php
namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Password;
use Auth;
class ClientResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = '/client';
public function __construct()
{
$this->middleware('guest:client');
}
protected function guard()
{
return Auth::guard('client');
}
protected function broker()
{
return Password::broker('clients');
}
public function showResetForm(Request $request, $token = null)
{
return view('index.client.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
}
blade
#extends('index.layout.layout')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Client Password</div>
<div class="panel-body">
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('client.password.reset') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
#if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Make sure form method id POST
<form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
</form>
I upgraded my project from Laravel 5.3 to 5.5, but used old version of reset.blade.php

Laravel 5.3 - 500 internal error when saving a new Eloquent model

When trying to create a new instance of my model Apartment I get a 500 (Internal Server Error). I have other /create/ routes that I use to create new instance of my models and they work properly and they're set up same as ApartmentController#create, but for some weird reason it doesn't work.
ApartmentController#create
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$addresses = Address::all();
return view('address/create')->with('addresses', $addresses);
}
ApartmentController#store
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$address = new Address();
$this->authorize('create', $address);
$validator = Validator::make($request->all(), [
'building_number' => 'required|integer',
'street' => 'required|string',
]);
if ($validator->fails()) {
return redirect('addresses/create')
->withErrors($validator)
->withInput();
}
$address->building_number = $request->building_number;
$address->street = $request->street;
$address->save();
return redirect('addresses/create');
}
View apartment/create
<div class="col-md-6">
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" method="POST" action="{{ action("ApartmentController#store") }}">
<fieldset>
<!-- Form Name -->
<legend>Register a new apartment</legend>
{{ csrf_field() }}
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="address_id">Address</label>
<div class="col-md-4">
<select id="address_id" name="address_id" class="form-control">
#foreach($addresses as $address)
<option value="{{$address->id}}">{{$address->street}} {{$address->building_number}}</option>
#endforeach
</select>
<span class="help-block">Apartments building number</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="apartment_number">Apartment Number</label>
<div class="col-md-4">
<input id="apartment_number" name="apartment_number" type="text" placeholder="2402" class="form-control input-md" required="">
<span class="help-block">The apartment number</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="size">Apartment Size</label>
<div class="col-md-4">
<input id="size" name="size" type="text" placeholder="56qm" class="form-control input-md" required="">
<span class="help-block">The size of the apartment in qm</span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="living_area_size">Living Area Size</label>
<div class="col-md-4">
<input id="living_area_size" name="living_area_size" type="text" placeholder="23qm" class="form-control input-md" required="">
<span class="help-block">The size of the living area in qm</span>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="suitable_for">Suitable For</label>
<div class="col-md-4">
<select id="suitable_for" name="suitable_for" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<span class="help-block">Number of people the apartment is suitable for.</span>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="furnished">Furnished</label>
<div class="col-md-4">
<select id="furnished" name="furnished" class="form-control">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="balcony_or_terrace">Balcony or Terrace</label>
<div class="col-md-4">
<select id="balcony_or_terrace" name="balcony_or_terrace" class="form-control">
<option value="none">None</option>
<option value="balcony">Balcony</option>
<option value="terrace">Terrace</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="floor">Floor</label>
<div class="col-md-4">
<select id="floor" name="floor" class="form-control">
<option value="ground">Ground</option>
<option value="first">First</option>
<option value="second">Second</option>
<option value="third">Third</option>
</select>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="description">Description</label>
<div class="col-md-6">
<textarea class="form-control" id="description" name="description"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<div class="col-md-4">
<legend>Registered Apartments</legend>
#foreach($apartments as $apartment)
<div class="address">
Apartment {{ $apartment->apartment_number }}
<a href="{{ url('/apartments/' . $apartment->id . '/edit') }}"
onclick="event.preventDefault();
document.getElementById('edit-form-{{$apartment->id}}').submit();">
<button class="btn btn-default">Edit</button>
</a>
<form id="edit-form-{{$apartment->id}}" action="{{ url('/apartments/' . $apartment->id . '/edit') }}" method="GET" style="display: none;">
{{ csrf_field() }}
<input type="hidden" name="address_id" value="{{$apartment->id}}">
</form>
<a href="{{ url('/apartments/' . $apartment->id) }}"
onclick="event.preventDefault();
document.getElementById('delete-form-{{$apartment->id}}').submit();">
<button class="btn btn-warning">Delete</button>
</a>
<form id="delete-form-{{$apartment->id}}" method="POST" action="/apartments/{{$apartment->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
</div>
<br>
#endforeach
</div>
Route web.php
Route::resource('apartments', 'ApartmentController');
apartments table for Apartment model
I tried to catch the exception on the save() method, but nothing is being caught and I still get redirected to /apartments without any Laravel error just Chroms 500 error page.
This is how I tried to catch the error using dwightwatson/validating:
try{
$apartment = new Apartment();
$apartment->apartment_number = $request->apartment_number;
$apartment->size = $request->size;
$apartment->living_area_size = $request->living_area_size;
$apartment->suitable_for = $request->suitable_for;
$apartment->furnished = $request->furnished;
$apartment->balcony_or_terrace = $request->balcony_or_terrace;
$apartment->floor = $request->floor;
$apartment->description = $request->description;
$apartment->address_id = $request->address_id;
$apartment->saveOrFail();
} catch (ValidationException $e) {
$errors = $e->getErrors();
return redirect()->route('apartments.create')
->withErrors($errors)
->withInput();
}
First of all, you Download Fiddler if you haven't already. Fire it up and then trigger the store method again. Fiddler should tell you a great deal about your request and response:
On the left side, you have a list of requests and on the right, you have statistics and inspectors. Under inspectors tab you will see WebView. Click it and you will see the error generated by Laravel.
As for why it might be failing, check if the references are included properly. e.g:
use App\Address;

Store and update method together using modal view laravel

I've a store and update method that I would like to use the same modal box to prompt, however I notice that the store method takes the syntax of
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
whereas my update method,
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
Is there a way that I can specify which method to use depending on the option chosen, I have two buttons created, each respectively.
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
Is there a way I can call separately using the same modal box or I have to create two duplicate modal box, one for store, the other for update?
My partial code is shown below ..
blade.php
<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
<div class="row user-row">
<div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
<h5 style="font-weight: bold">{{ $user->name }}</h5>
</div>
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
<h5 class="glyphicon glyphicon-chevron-down text-muted pull-right"> </h5>
</div>
</div>
<div class="row user-infos {{ $user->id }}">
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">User Information</h2>
</div>
<div class="panel-body">
<div class="row">
<div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
<div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
<div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
<div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
<div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-sm btn-warning btn--edit" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
<span class="pull-right">
<button class="btn btn-sm btn-danger" type="button">Inactive <i class="glyphicon glyphicon-remove"></i></button>
</span>
</div>
</div>
</div>
</div>
<input type="hidden" name="user_id" value="{{ $user->id }}" />
#endforeach
</div>
#if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
{{ Session::get('flash_message') }}
</div>
#endif
<div class="col-sm-offset-1 col-sm-2">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 #if ($errors->has('name')) has-error #endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password:</label>
<div class="col-sm-5 #if ($errors->has('password')) has-error #endif">
<input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
</div>
...
controller.php
class ManageAccountsController extends Controller
{
public $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();
return view('manage_accounts', compact('users', 'roles'));
}
public function store(StoreUserRequest $request)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request);
Session::flash('flash_message', 'User successfully added!');
return redirect()->back();
}
public function update(StoreUserRequest $request, $id)
{
// validation already handled using this: http://laravel.com/docs/5.0/validation#form-request-validation
$this->userRepository->upsert($request, $id);
Session::flash('flash_message', 'User successfully updated!');
return redirect()->back();
}
}
class UserRepository {
public function upsert($data, $id)
{
// You will also need something like this
if(isset($id))
{
$user = User::find($id);
}
else {
$user = new User;
}
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = Hash::make($data['password']);
$user->mobile = $data['mobile'];
$user->role_id = $data['role_id'];
// save our user
$user->save();
return $user;
}
}

Resources