I am trying to create edit profile, but when I click on edit profile button I'm getting below error:
Illuminate \ Database \ Eloquent \ MassAssignmentException Add [title]
to fillable property to allow mass assignment on [App\Profile]
show.blade.php :
<#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-4">
<img src="https://scontent-cdt1-1.cdninstagram.com/vp/dcca3b442819fc8b9b63f09b2ebde320/5DA9E3CB/t51.2885-19/s150x150/40101184_290824334847414_1758201800999043072_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com" class="rounded-circle">
</div>
<div class="col-8">
<div class="d-flex align-items-baseline">
<div class="h4 mr-3 pt-2">{{ $user->username }}</div>
<button class="btn btn-primary">S'abonner</button>
</div>
<div class="d-flex">
<div class="mr-3">{{ $user->posts->count() }} article(s) en vente
</div>
Modifier Profile
<div class="mt-3">
<div class="font-weight-bold">
{{ $user->profile->title }}
</div>
<div class="font-weight-bold">
{{ $user->profile->description }}
</div>
</div>
</div>
</div>
<div class="row mt-5">
#foreach ($user->posts as $post)
<div class="col-4">
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100">
</div>
#endforeach
</div>
</div>
#endsection
ProfileController :
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function show(User $user)
{
return view('profile.show', compact('user'));
}
public function edit(User $user)
{
return view('profile.edit', compact('user'));
}
public function update(User $user)
{
$data = request()->validate([
'title' => 'required',
'description' => 'required'
]);
$user->profile->update($data);
return redirect()->route('profile.show', ['user' => $user]);
}
}
edit.blade.php :
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier profile</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.update', ['user' => $user]) }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Titre</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') ?? $user->profile->title }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<div class="col-md-6">
<textarea id="description" type="text" class="form-control #error('description') is-invalid #enderror" name="description" autocomplete="description" autofocus>{{ old('description') ?? $user->profile->description }}</textarea>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input #error('image') is-invalid #enderror" id="validatedCustomFile" >
<label class="custom-file-label" for="validatedCustomFile">Choisir une image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Modifier profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarder = [];
public function user()
{
return $this->belongsTo('App\User');
}
}
What am I doing wrong here and how can I get rid of this error?
You have a spelling error, instead of $guarder add this in your model:
protected $guarded = [];
I won't advise using empty guarded but use $fillable instead.
In the same controller model, free access to database fields with
protected $guarded = [];
or
protected $fillable = [];
Related
I am insert data product with images in dashboard, when I try to order the product I have error 404 not found and the URL showing value database in table like http://127.0.0.1:8000/shops/order/[%7B%22id%22:2,%22category_id%22:1,%22name_product%22:%22asdasd%22,%22harga%22:123123123,%22image%22:%22product-images//HapOzHmJPim1kIfmIeCM21xesCnIbR5Z7lpzcO8M.jpg%22,%22published_at%22:null,%22created_at%22:%222022-05-23T16:25:00.000000Z%22,%22updated_at%22:%222022-05-23T16:25:10.000000Z%22,%22kategori%22:%7B%22id%22:1,%22name_category%22:%22Top%22,%22created_at%22:%222022-05-23T16:22:40.000000Z%22,%22updated_at%22:%222022-05-23T16:22:40.000000Z%22%7D%7D].
But when I am insert data product without images in dashboard, and then I try to order the product is successful.
This is my route
Route::post('/shops/order/{shop:id}', [OrderController::class, 'store']);
This is OrderController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\User;
use App\Models\Shop;
class OrderController extends Controller
{
public function store(Request $request)
{
$createOrder = $request->validate([
'user_id' => 'required',
'name_product_id' => 'required',
'size' => 'required',
'no' => 'required',
'address' => 'required'
]);
Order::create($createOrder);
return redirect('shops/order/{shop:id}')->with('success', 'Order is successfully!');
}
}
This is my views
#extends('layouts.main')
#section('container')
<div class="container">
<div class="row mt-5">
#foreach ($shops as $shop)
<div class="card mb-3">
<div class="row g-0">
<div class="col-md-4">
<img src="{{ asset('storage/' . $shop->image) }}" alt="{{ $shop->name_product }}" class="img-fluid rounded-start" style="max-height: 400px; overflow:hidden">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{ $shop->name_product }}</h5>
<p>Jenis {{ $shop->category->name_category }}</p>
<p class="card-text fw-bold">Rp.{{ $shop->price }}</p>
</div>
</div>
</div>
</div>
#endforeach
</div>
<div class="row mt-5">
<div class="col align-self-center">
#if (session()->has('success'))
<div class="alert alert-success text-center" role="alert">
{{ session('success') }}
</div>
#endif
</div>
</div>
<form method="POST" action="/shops/order/{{ $shops }}" class="row g-2" enctype="multipart/form-data">
#csrf
<h3 class="text-center mt-2">Detail Delivery</h3>
<div class="col-md-6">
<div class="mb-3">
<select class="form-select" name="user_id" hidden>
#foreach ($users as $name)
<option value="{{ $name->id }}">{{ $name->id }}</option>
#endforeach
</select>
</div>
<div class="mb-3">
<select class="form-select" name="name_produk_id" hidden>
#foreach ($shops as $shops_id)
<option value="{{ $shops_id->id }}">{{ $shops_id->id }}</option>
#endforeach
</select>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" #error('address') is-invalid #enderror id="address" name="address" required>
</textarea>
</div>
</div>
<div class="col-md-4">
<label for="size" class="form-label mt-3">Select size :</label>
<select class="form-select" name="size">
<option selected>S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<div class="mt-3">
<label for="no" class="form-label">Nomor Whatsapp</label>
<input class="form-control" #error('no') is-invalid #enderror id="no" name="no" required autofocus">
</div>
<button type="submit" class="btn btn-primary mt-5">Buy Now</button>
</div>
</form>
</div>
#endsection
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');
}
I am using session flash in my Laravel-5.8 project.
Controller
<?php
namespace App\Http\Controllers\Appraisal;
use App\Http\Controllers\Controller;
use App\Models\Appraisal\AppraisalSkill;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Exception;
use Illuminate\Support\Facades\Validator;
use Session;
class AppraisalSkillsController extends Controller
{
public function create()
{
abort_unless(\Gate::allows('skill_create'), 403);
return view('appraisal.skills.create');
}
public function store(Request $request)
{
abort_unless(\Gate::allows('skill_create'), 403);
$this->validate($request, [
'skill_name' => 'required|unique:appraisal_skills,company_id',
]);
$skill = AppraisalSkill::create([
'skill_name' => $request->skill_name,
'description' => $request->description,
'company_id' => Auth::user()->company_id,
'created_by' => Auth::user()->id,
'created_at' => date("Y-m-d H:i:s"),
'is_active' => 1,
]);
Session::flash('success', 'Appraisal Skill is created successfully');
return redirect()->route('appraisal.skills.index');
}
}
view/partials/_messages.blade.php
#if (count($errors) > 0)
<div class="alert alert-danger alert-block" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Errors: </strong>
<ul>
#foreach ($errors as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Session::has('success'))
<div class="alert alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Success: </strong>{{Session::get('success')}}
</div>
#endif
view
<div class="container-fluid">
<div class="panel-heading clearfix">
<div class="float-right">
<div class="btn-group btn-group-sm" role="group">
<a href="{{ route("appraisal.skills.index") }}" class="btn bg-navy margin" title=" Back">
<span> Back to List</span>
</a>
</div>
</div>
</div>
<br>
#include('partials._messages')
<br>
<div class="card">
<div class="card-header">
Create Skill
</div>
<div class="card-body">
<form action="{{route('appraisal.skills.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Skill Name<span style="color:red;">*</span></label>
<div class="col-md-9 controls">
<input type="text" name="skill_name" placeholder="Enter skill name here" class="form-control" value="{{old('skill_name')}}">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Description</label>
<div class="col-md-9">
<textarea rows="2" name="description" class="form-control" placeholder="Enter Description here" value="{{old('description')}}"></textarea>
</div>
</div>
</div>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.skills.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
</div>
</div>
</div>
When I click on save submit button, I expect that if there is any error it should display the detail of the error. But, rather it only display Error: without the details.
The success message is working, but the error message is not working as expected
How do I get this resolved?
Thank you.
There is a little mistake when looping through the errors. Change line #foreach ($errors as $error) to #foreach ($errors->all() as $error) . It should work now!
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>
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;
}
}