Skip to content

Instantly share code, notes, and snippets.

@manojchowrasiya
Created June 30, 2017 18:00
Show Gist options
  • Select an option

  • Save manojchowrasiya/14200558585afc973acf3e0fbed029a7 to your computer and use it in GitHub Desktop.

Select an option

Save manojchowrasiya/14200558585afc973acf3e0fbed029a7 to your computer and use it in GitHub Desktop.

Make Controller with default functions

php artisan make:controller PhotoController --resource

Make Model

php artisan make:model User

php artisan make:model User --migration

php artisan make:model User -m

Make Console

php artisan list

php artisan make:command SendEmails

Create Migration

php artisan make:migration create_users_table

Rollback Migration

php artisan migrate:rollback

php artisan migrate:rollback --step=5

-- roll back all of your application's migrations

php artisan migrate:reset

-- rollback entire migartion and than migrate

php artisan migrate:refresh

Migrate

php artisan migrate

Define CRUD Routes

Route::get('photos/popular', 'PhotoController@method'); Route::any('photos/popular', 'PhotoController@method'); Route::put('user/{id}', 'UserController@update'); Route::get('profile', 'UserController@show')->middleware('auth');

Route::resource('photos', 'PhotoController');

Request Param

public function store(Request $request) { $all = $request->all(); // all attributes $name = $request->input('name'); // specific attribute dd($all); // dump and display }

HTTP Requests

Cookies

$value = $request->cookie('name'); $cookie = cookie('name', 'value', $minutes);

Image Upload

Server Side Validation

$rules = [ 'name' => 'required', 'operating_system' => 'required', 'model' => 'required', ];

    $messages = [
        'operating_system.required' => 'We need to know your e-mail address!',
    ];

    $validator = Validator::make($request->all(),$rules,$messages);
    
    if ($validator->fails()) {
        return redirect('device/create')->withErrors($validator)
                    ->withInput();
    }else{

        $input = $request->all();
        unset($input['_token']);
        Device::create($input);
        \Session::flash('success_message','data added successfully.');
        return redirect('device'); 
    }

Session Error

// session store from controller

\Session::flash('success_message','Ticket Successfully Created');

@if(Session::has('success_message'))

{!! session('success_message') !!}
@endif

@if(Session::has('error_message'))

{!! session('error_message') !!}
@endif

@if(Session::has('notice_message'))

{!! session('notice_message') !!}
@endif

@if(Session::has('info_message'))

{!! session('info_message') !!}
@endif

JSON Response

return response()->json([ 'name' => 'Abigail', 'state' => 'CA' ]);

File Downloads

return response()->download($pathToFile);

return response()->download($pathToFile, $name, $headers);

return response()->download($pathToFile)->deleteFileAfterSend(true);

File Responses

return response()->file($pathToFile);

return response()->file($pathToFile, $headers);

Retrieving All Session Data

$data = $request->session()->all(); $value = $request->session()->get('key');

Storing Data

$request->session()->put('key', 'value');

Flash Data

$request->session()->flash('status', 'Task was successful!');

Localization

Redirects

return redirect()->route('login');

return redirect()->route('profile', ['id' => 1]);

return redirect('dashboard')->with('status', 'Profile updated!');

CRUD

cron setup

multiple login setup

Quries#

form elements

echo Form::open(['route' => ['route.name', $user->id]]) echo Form::open(['action' => ['Controller@method', $user->id]]) echo Form::open(['url' => 'foo/bar', 'files' => true])

echo Form::label('email', 'E-Mail Address', ['class' => 'awesome']); echo Form::text('email', '[email protected]'); echo Form::password('password', ['class' => 'awesome']);

echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S'); echo Form::select('size', ['L' => 'L', 'S' => 'S'], null, ['placeholder' => 'Pick a size.']); echo Form::submit('Click Me!');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment