Ivan Lebanov Full-stack web developer

Ivan lebanov's signature

Laravel cheat sheet

I find myself searching for a lot of things I keep forgetting related to Laravel. They sometimes overlap so I decided to create a cheat sheet for my own sake/& hopefully helping out 1 or 2 people/.

Commands/all ran from the root folder of the project/:

  • composer install” – installs all dependencies /uses composer.json/
  • composer update” – updates dependencies
  • php artisan serve” – serves your project on port 8000
  • php artisan serve –port=8080” – changes the default port/use in case that part is being used by something else/
  • php artisan migrate” – check for pending migrations & tries to update the database
  • php artisan make:migration create_tests_table –create=tests” – creates a migration which also creates a table
  • php artisan make:migration add_answers_to_tests_table –table=tests” – creates a migration and preselects the table you’ve chosen
  • php artisan make:request StoreBlogPost” – creates the boilerplate for a request validation
  • composer require “laravelcollective/html” – install the missing laravel forms & html elements. After running it you have to add a few lines in your app.php
    ” Collective\Html\HtmlServiceProvider::class, ” inside providers key of the array

    ‘Form’ => Collective\Html\FormFacade::class,’Html’ => Collective\Html\HtmlFacade::class,
    ” – those two lines inside aliases

Forms:

  • {{ Form::open([ ‘route’ => [‘admin.types.update’, ‘id’ => $page[‘id’] ] , ‘method’ => ‘PUT’, ‘files’ => true]) }}
    Opens a form with a route name ‘admin.types.update’, sets the id parameter in the url to a variable $page[‘id’], sets the method to put & makes it possible to send files. The only required parametere is the routes array/first element only/.
  • {{ Form::close() }} – closes a form
  • {{ Form::text(‘page_name’, $page[‘name’], [‘placeholder’ => “Page name*”, “id”=> “name-field”] ) }}
    Create a text field/input type=”text”/ with name – page_name, value – $page[‘name’] /can be set to null/, with placeholder and id. You can set any kind of attributes in the last array.
  • {{ Form::textarea(‘page_name’, $page[‘name’], [‘placeholder’ => “Page name*”, “id”=> “name-field”] ) }}
    Same as previous – it will just render a textarea
  • {{ Form::file(‘image’, null, [‘id’ => ‘image_field’] ) }} – creates a file input with the name input  and id image field
  • {{ Form::checkbox(‘is_past’, 1, $page[‘is_past’], [‘class’ => ‘field’]) }}
    creates a checkbox with name is_past. It will have value of 1 and it will be determined if it has to be checked based on the variable $page[‘is_past’]. Same syntax is used for radio buttons

Controllers

  • use App\Http\Requests\AddSubscriberRequest; – put that on the top of your controller so the request validator is available to the methods
  • use App\Page; – put that on top of the controller if you want to use any of the models you created/Page in my case/.
  • return view(‘blog.single’, $data); – returns the view – recourcses/views/blog/single.blade.php and passes the data array.
  • return redirect()->route(‘admin.screenings.get’)->with(‘status’, $status ); – redirect to a route

Routes

  • Route::get(‘/admin/page/{id}’, [‘as’ => ‘admin.pages.editPage’, ‘uses’ => ‘Admin\AdminPageController@editPage’]); – create a route which will be used on get request on the /admin/page/{id} with route name – ‘admin.pages.editPage’, using the editPage method of AdminPageController inside the Admin folder of all controllers.

There’s a lot more to Laravel. But that’s a nice starter cheat sheet. Have a great & productive day.