First, enable soft deletes in your model. I’ll use the users
table as an example.
Continue reading “Adding Soft Deletes to Existing Database Table in Laravel”
Dev Notebook
First, enable soft deletes in your model. I’ll use the users
table as an example.
Continue reading “Adding Soft Deletes to Existing Database Table in Laravel”
Here’s how to get the Form and Html Builder package back when using Laravel 5.
composer require illuminate/html
Then, in /config/app.php add the following to the providers and aliases arrays:
'providers' => [ ... 'Illuminate\Html\HtmlServiceProvider', ], 'aliases' => [ ... 'Form'=> 'Illuminate\Html\FormFacade', 'HTML'=> 'Illuminate\Html\HtmlFacade', ],
A new Laravel 5 installation comes with user registration out of the box. If you don’t want to use this feature, here is a clean way to disable it. Open up app\Http\Controllers\Auth\AuthController.php and add the following methods:
public function getRegister() { return redirect('auth/login'); // Or wherever } public function postRegister() { }
These will override the parent methods in the AuthenticatesAndRegistersUsers trait.
To restrict pages in your app to authentication status, add the middleware to the controller’s constructor.
To restrict a page to guests only (not signed in):
public function __construct() { $this->middleware('guest'); }
For signed in users:
public function __construct() { $this->middleware('auth'); }
If you want to make exceptions for certain methods, just pass those parameters in as an array for the second argument.
$this->middleware('auth', ['except' => 'show']);
To have laravel automatically generate all the REST routes for a controller, use Route::resource()
Route::resource('users', 'UsersController');
First, make sure you have gulp installed.
npm install --global gulp
Then run npm install
in order to install Elixir.
Here’s an example of how to use form-model binding with FormBuilder.
{!! Form::model($user, ['route' => ['users.update', $user->id], 'method' => 'PATCH']) !!}
Here’s a little snippet for generating an excerpt without breaking up a word:
function excerpt($string='', $maxChar=50, $uri='#') { $length = strlen($string); if ($length < $maxChar) { return $string; } $trimmedString = substr($string, 0, $maxChar); $choppedString = substr($trimmedString, 0, strrpos($trimmedString, strrchr($trimmedString, ' '))); $newString = $choppedString . ' <a href="' . $uri . '">more</a>'; return $newString; }
One of the biggest pains about developing on a local server setup like WAMP is that the root filepaths are different when you go live. To get around this I always set up a virtual host so that my development url can be something like, http://myapplication.dev/. Here’s how to do it on Wampserver 2.5. This is tailored for Laravel but you can adjust it as needed.
Sometimes I write a function that I only want to apply to certain pages. Say, a form. I may need some javascript to apply to the form when I’m inserting and different javascript to apply when I’m editing. Since I’m reusing the form, I just change the id
of the form and use this check:
if ($('#insert-form').length > 0) { // do stuff that only applies to the insert form }