CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

23 March, 2021

Laravel Model

 Programing Coderfunda     March 23, 2021     No comments   

 

Table of Contents

  • Basic Usage
  • More
  • Soft Delete
  • Events
  • Eloquent Configuration

Basic Usage

Defining An Eloquent Model

class User extends Model {}

generate Eloquent models

php artisan make:model User

specify a custom table name

class User extends Model {
    protected $table = 'my_users';
}

More

Model::create(array('key' => 'value'));

Find first matching record by attributes or create

Model::firstOrCreate(array('key' => 'value'));

Find first record by attributes or instantiate

Model::firstOrNew(array('key' => 'value'));

Create or update a record matching attibutes, and fill with values

Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value'));

Fill a model with an array of attributes, beware of mass assignment!

Model::fill($attributes);
Model::destroy(1);
Model::all();
Model::find(1);

Find using dual primary key

Model::find(array('first', 'last'));

Throw an exception if the lookup fails

Model::findOrFail(1);

Find using dual primary key and throw exception if the lookup fails

Model::findOrFail(array('first', 'last'));
Model::where('foo', '=', 'bar')->get();
Model::where('foo', '=', 'bar')->first();

dynamic

Model::whereFoo('bar')->first();

Throw an exception if the lookup fails

Model::where('foo', '=', 'bar')->firstOrFail();
Model::where('foo', '=', 'bar')->count();
Model::where('foo', '=', 'bar')->delete();

Output raw query

Model::where('foo', '=', 'bar')->toSql();
Model::whereRaw('foo = bar and cars = 2', array(20))->get();
Model::remember(5)->get();
Model::remember(5, 'cache-key-name')->get();
Model::cacheTags('my-tag')->remember(5)->get();
Model::cacheTags(array('my-first-key','my-second-key'))->remember(5)->get();
Model::on('connection-name')->find(1);
Model::with('relation')->get();
Model::all()->take(10);
Model::all()->skip(10);

Default Eloquent sort is ascendant

Model::all()->orderBy('column');
Model::all()->orderBy('column','desc');

Soft Delete

Model::withTrashed()->where('cars', 2)->get();

Include the soft deleted models in the results

Model::withTrashed()->where('cars', 2)->restore();
Model::where('cars', 2)->forceDelete();

Force the result set to only included soft deletes

Model::onlyTrashed()->where('cars', 2)->get();

Events

Model::creating(function($model){});
Model::created(function($model){});
Model::updating(function($model){});
Model::updated(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
Model::deleting(function($model){});
Model::deleted(function($model){});
Model::observe(new FooObserver);

Eloquent Configuration

Disables mass assignment exceptions from being thrown from model inserts and updates

Eloquent::unguard();

Renables any ability to throw mass assignment exceptions

Eloquent::reguard();
  • Tags
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel HTML

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 HTML::macro('name', function(){});

Convert an HTML string to entities

HTML::entities($value);

Convert entities to HTML characters

HTML::decode($value);

Generate a link to a JavaScript file

HTML::script($url, $attributes);

Generate a link to a CSS file

HTML::style($url, $attributes);

Generate an HTML image element

HTML::image($url, $alt, $attributes);

Generate a HTML link

HTML::link($url, 'title', $attributes, $secure);

Generate a HTTPS HTML link

HTML::secureLink($url, 'title', $attributes);

Generate a HTML link to an asset

HTML::linkAsset($url, 'title', $attributes, $secure);

Generate a HTTPS HTML link to an asset

HTML::linkSecureAsset($url, 'title', $attributes);

Generate a HTML link to a named route

HTML::linkRoute($name, 'title', $parameters, $attributes);

Generate a HTML link to a controller action

HTML::linkAction($action, 'title', $parameters, $attributes);

Generate a HTML link to an email address

HTML::mailto($email, 'title', $attributes);

Obfuscate an e-mail address to prevent spam-bots from sniffing it

HTML::email($email);

Generate an ordered list of items

HTML::ol($list, $attributes);

Generate an un-ordered list of items

HTML::ul($list, $attributes);

Create a listing HTML element

HTML::listing($type, $list, $attributes);

Create the HTML for a listing element

HTML::listingElement($key, $type, $value);

Create the HTML for a nested listing attribute

HTML::nestedListing($key, $type, $value);

Build an HTML attribute string from an array

HTML::attributes($attributes);

Build a single attribute element

HTML::attributeElement($key, $value);

Obfuscate a string to prevent spam-bots from sniffing it

HTML::obfuscate($value);
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Cache

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 Cache::put('key', 'value', $minutes);

Cache::add('key', 'value', $minutes);
Cache::forever('key', 'value');
Cache::remember('key', $minutes, function(){ return 'value' });
Cache::rememberForever('key', function(){ return 'value' });
Cache::forget('key');
Cache::has('key');
Cache::get('key');
Cache::get('key', 'default');
Cache::get('key', function(){ return 'default'; });
Cache::tags('my-tag')->put('key','value', $minutes);
Cache::tags('my-tag')->has('key');
Cache::tags('my-tag')->get('key');
Cache::tags('my-tag')->forget('key');
Cache::tags('my-tag')->flush();
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
Cache::section('group')->put('key', $value);
Cache::section('group')->get('key');
Cache::section('group')->flush();
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel SSH

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 Executing Commands

SSH::run(array $commands);
SSH::into($remote)->run(array $commands); 

specify remote, otherwise assumes default

SSH::run(array $commands, function($line)
{
  echo $line.PHP_EOL;
});

Tasks

define

SSH::define($taskName, array $commands);

execute

SSH::task($taskName, function($line)
{
  echo $line.PHP_EOL;
});

SFTP Uploads

SSH::put($localFile, $remotePath);
SSH::putString($string, $remotePath);
  • Tags
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Lang

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 App::setLocale('en');

Lang::get('messages.welcome');
Lang::get('messages.welcome', array('foo' => 'Bar'));
Lang::has('messages.welcome');
Lang::choice('messages.apples', 10);

Lang::get alias

trans('messages.welcome');
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel File

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 File::exists('path');

File::get('path');
File::getRemote('path');

Get a file’s contents by requiring it

File::getRequire('path');

Require the given file once

File::requireOnce('path');

Write the contents of a file

File::put('path', 'contents');

Append to a file

File::append('path', 'data');

Delete the file at a given path

File::delete('path');

Move a file to a new location

File::move('path', 'target');

Copy a file to a new location

File::copy('path', 'target');

Extract the file extension from a file path

File::extension('path');

Get the file type of a given file

File::type('path');

Get the file size of a given file

File::size('path');

Get the file’s last modification time

File::lastModified('path');

Determine if the given path is a directory

File::isDirectory('directory');

Determine if the given path is writable

File::isWritable('path');

Determine if the given path is a file

File::isFile('file');

Find path names matching a given pattern.

File::glob($patterns, $flag);

Get an array of all files in a directory.

File::files('directory');

Get all of the files from the given directory (recursive).

File::allFiles('directory');

Get all of the directories within a given directory.

File::directories('directory');

Create a directory

File::makeDirectory('path', $mode = 0777, $recursive = false);

Copy a directory from one location to another

File::copyDirectory('directory', 'destination', $options = null);

Recursively delete a directory

File::deleteDirectory('directory', $preserve = false);

Empty the specified directory of all files and folders

File::cleanDirectory('directory');
  • Tags
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Config

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 Config::get('app.timezone');

get with Default value

Config::get('app.timezone', 'UTC');

set Configuration

Config::set('database.default', 'sqlite');
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Route

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 Route::get('foo', function(){});

Route::get('foo', 'ControllerName@function');
Route::controller('foo', 'FooController');

Table of Contents

  • RESTful Controllers
  • Triggering Errors
  • Route Parameters
  • HTTP Verbs
  • Secure Routes(TBD)
  • Route Constraints
  • HTTP Middleware
  • Named Routes
  • Route Prefixing
  • Route Namespacing
  • Sub-Domain Routing

RESTful Controllers

Route::resource('posts','PostsController');

Specify a subset of actions to handle on the route

Route::resource('photo', 'PhotoController',['only' => ['index', 'show']]);
Route::resource('photo', 'PhotoController',['except' => ['update', 'destroy']]);

Triggering Errors

App::abort(404);
$handler->missing(...) in ErrorServiceProvider::boot();
throw new NotFoundHttpException;

Route Parameters

Route::get('foo/{bar}', function($bar){});
Route::get('foo/{bar?}', function($bar = 'bar'){});

HTTP Verbs

Route::any('foo', function(){});
Route::post('foo', function(){});
Route::put('foo', function(){});
Route::patch('foo', function(){});
Route::delete('foo', function(){});

RESTful actions

Route::resource('foo', 'FooController');

Registering A Route For Multiple Verbs

Route::match(['get', 'post'], '/', function(){});

Secure Routes(TBD)

Route::get('foo', array('https', function(){}));

Route Constraints

Route::get('foo/{bar}', function($bar){})
->where('bar', '[0-9]+');
Route::get('foo/{bar}/{baz}', function($bar, $baz){})
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]'))

Set a pattern to be used across routes

Route::pattern('bar', '[0-9]+')

HTTP Middleware

Assigning Middleware To Routes

Route::get('admin/profile', ['middleware' => 'auth', function(){}]);

Named Routes

Route::currentRouteName();
Route::get('foo/bar', array('as' => 'foobar', function(){}));
Route::get('user/profile', [
  'as' => 'profile', 'uses' => 'UserController@showProfile'
]);
$url = route('profile');
$redirect = redirect()->route('profile');

Route Prefixing

Route::group(['prefix' => 'admin'], function()
{
  Route::get('users', function(){
      return 'Matches The "/admin/users" URL';
  });
});

Route Namespacing

This route group will carry the namespace ‘Foo\Bar’

Route::group(array('namespace' => 'Foo\Bar'), function(){})

Sub-Domain Routing

{sub} will be passed to the closure

Route::group(array('domain' => '{sub}.example.com'), function(){});
  • Tags
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Redirect

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 return Redirect::to('foo/bar');

return Redirect::to('foo/bar')->with('key', 'value');
return Redirect::to('foo/bar')->withInput(Input::get());
return Redirect::to('foo/bar')->withInput(Input::except('password'));
return Redirect::to('foo/bar')->withErrors($validator);

Create a new redirect response to the previous location

return Redirect::back();

Create a new redirect response to a named route

return Redirect::route('foobar');
return Redirect::route('foobar', array('value'));
return Redirect::route('foobar', array('key' => 'value'));

Create a new redirect response to a controller action

return Redirect::action('FooController@index');
return Redirect::action('FooController@baz', array('value'));
return Redirect::action('FooController@baz', array('key' => 'value'));

If intended redirect is not defined, defaults to foo/bar.

return Redirect::intended('foo/bar');
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Queue

 Programing Coderfunda     March 23, 2021     Laravel, php     No comments   

 Queue::push('SendMail', array('message' => $message));

Queue::push('SendEmail@send', array('message' => $message));
Queue::push(function($job) use $id {});

Same payload to multiple workers

Queue::bulk(array('SendEmail', 'NotifyUser'), $payload);

Starting the queue listener

php artisan queue:listen
php artisan queue:listen connection
php artisan queue:listen --timeout=60

Process only the first job on the queue

php artisan queue:work

Start a queue worker in daemon mode

php artisan queue:work --daemon

Create migration file for failed jobs

php artisan queue:failed-table

Listing failed jobs

php artisan queue:failed

Delete failed job by id

php artisan queue:forget 5

Delete all failed jobs

php artisan queue:flush
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Meta

Popular Posts

  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • iOS 17 Force Screen Rotation not working on iPAD only
    I have followed all the links on Google and StackOverFlow, unfortunately, I could not find any reliable solution Specifically for iPad devic...
  • C++ in Hindi Introduction
    C ++ का परिचय C ++ एक ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग लैंग्वेज है। C ++ को Bjarne Stroustrup द्वारा विकसित किया गया था। C ++ में आने से पह...
  • Python AttributeError: 'str' has no attribute glob
    I am trying to look for a folder in a directory but I am getting the error.AttributeError: 'str' has no attribute glob Here's ...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (2)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda