-
Notifications
You must be signed in to change notification settings - Fork 0
Laravel Conventions
Frank Siderio edited this page Mar 22, 2018
·
1 revision
What is a model? A model is what we use to interact with our database. It represents a single row in a table. So if we have a table called students or users the model would be called Student or User.
The following code snippets are examples. This is the required way of using models to create or update the database.
User::create([
'firstName' => $request->firstName,
'lastName' => $request->lastName,
'email' => $request->email,
]);
$user = User::find($request->userId);
$user->firstName = $request->firstName;
$user->lastName = $request->lastName;
$user->email = $request->email;
$user->save();
Laravel provides a validator we can use to ensure the type of data being stored in our database is accurate.
The following snippets are just examples. This is the required way of implementing validation.
First include this at the top of the class:
use Illuminate\Support\Facades\Validator;
Create the validator as it's own function. This way it can be used throughout for different purposes (storing and updating).
protected function validator(array $data) {
return Validator::make($data, [
'title' => 'required|string|max:50',
'preview' => 'required|string|max:100',
'content' => 'required|string',
]);
}
$this->validator($request->all())->validate();
This is how it would be used on the frontend side:
<div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
<label for="name">Title</label>
<input class="form-control input-box" id="title" name="title" placeholder="Title" value="{{ old('title') }}" required>
@if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
- Home
- CIO
- Words of Wisdom
- Developers
- Designers
- Web Applications
- Making Changes
- Infrastructure
- Resources
- Key Contacts
- Future Plans