Last active
January 21, 2019 18:58
-
-
Save grambas/8e29fa8810f6cd294055d589f20a1fa0 to your computer and use it in GitHub Desktop.
[LARAVEL 5 snippets]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
'email => "required|unique:database_table_name,database_field,$this->id" | |
{!! Form::hidden('id', old('id',$model->id)) !!} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
#HASONE + BELONGSTO | |
class Client extends Model | |
{ | |
public function phone() | |
{ | |
return $this->hasOne('App\Phone'); | |
} | |
} | |
#Searching client_id into phones table and returns one row from table. | |
class Phone extends Model | |
{ | |
public function client() | |
{ | |
return $this->belongsTo('App\Client'); | |
} | |
} | |
#Also searching client_id into phones table and returns one row from table. | |
#HASMANY + BELONGSTO | |
class Client extends Model | |
{ | |
public function emails() | |
{ | |
return $this->hasMany('App\Email'); | |
} | |
} | |
#Searching client_id into emails table and returns all matched rows from table. | |
class Email extends Model | |
{ | |
public function client() | |
{ | |
return $this->belongsTo('App\Client'); | |
} | |
} | |
#Searching client_id into emails table and returns one row from table. | |
#BELONGSTOMANY + BELONGSTOMANY | |
class Discount extends Model | |
{ | |
public function clients() | |
{ | |
return $this->belongsToMany('App\Client'); | |
} | |
} | |
#Searching client_id and discount_id into clients_discounts table and returns all matched rows from table. | |
class Client extends Model | |
{ | |
public function discounts() | |
{ | |
return $this->belongsToMany('App\Discount'); | |
} | |
} | |
#Searching client_id and discount_id into clients_discounts table and returns all matched rows from table. | |
#HASMANYTHROUGH | |
class Poll extends Model | |
{ | |
public function answers() | |
{ | |
return $this->hasManyThrough('App\Question', 'App\Answer'); | |
} | |
} | |
#Searching question_id into answers table and then poll_id into questions table. Returns all rows from answers table which belongs to this poll. | |
#MORPHTO + MORPHMANY | |
class Note extends Model | |
{ | |
public function notable() | |
{ | |
return $this->morphTo(); | |
} | |
} | |
#Mark model as polymorphic. Returns all models which have notes. | |
class Invoice extends Model | |
{ | |
public function notes() | |
{ | |
return $this->morphMany('App\Note', 'notable'); | |
} | |
} | |
#Searches for 'App\Invoice' string into notable_type column and invoice id into notable_id and returns all rows (notes) which belongs to that invoice. | |
class Conversation extends Model | |
{ | |
public function notes() | |
{ | |
return $this->morphMany('App\Note', 'notable'); | |
} | |
} | |
#Searches for 'App\Conversation' string into notable_type column and conversation id into notable_id and returns all rows (notes) which belongs to that conversation. | |
#MORPHTOMANY + MORPHEDBYMANY | |
class Item extends Model | |
{ | |
public function categories() | |
{ | |
return $this->morphToMany('App\Category', 'categorizable'); | |
} | |
} | |
#Searches for 'App\Item' string into categorizable_type column and item id into categorizable_id column and returns all rows (categories) which belongs to that item. | |
class Post extends Model | |
{ | |
public function categories() | |
{ | |
return $this->morphToMany('App\Category', 'categorizable'); | |
} | |
} | |
#Searches for 'App\Post' string into categorizable_type column and post id into categorizable_id column and returns all rows (categories) which belongs to that post. | |
class Image extends Model | |
{ | |
public function categories() | |
{ | |
return $this->morphToMany('App\Category', 'categorizable'); | |
} | |
} | |
#Searches for 'App\Image' string into categorizable_type column and image id into categorizable_id column and returns all rows (categories) which belongs to that image. | |
class Category extends Model | |
{ | |
public function items() | |
{ | |
return $this->morphedByMany('App\Item', 'categorizable'); | |
} | |
public function posts() | |
{ | |
return $this->morphedByMany('App\Post', 'categorizable'); | |
} | |
public function images() | |
{ | |
return $this->morphedByMany('App\Image', 'categorizable'); | |
} | |
} | |
#Searches for 'App\Item'/'App\Post'/'App\Image' string into categorizable_type column and item/post/image id into categorizable_id column and returns all rows (items/posts/images) which belongs to that category. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#PageController.php | |
public function show($slugs_string){ | |
$slugs = explode('/', $slugs_string); | |
$page = null; | |
foreach($slugs as $key => $slug){ | |
$page = Page::where('slug', $slug) | |
->where('parent_id', '=', is_null($page)?null:$page->id) | |
->firstOrFail(); | |
// Another solution: | |
// ->first() | |
// If $page === null return 404 error | |
if($key == 0 && $page->parent){ | |
return redirect()->route('front.main.home'); | |
} | |
} | |
return view('front.page.show', compact('page')); | |
} | |
#routes.php | |
//Should be last in routes list | |
Route::get('{slugs_string}', [ | |
'as' => 'front.page.show', 'uses' => 'PageController@show' | |
])->where('slugs_string', '.*'); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
View::make('foo/bar', array('key' => 'value')); | |
Route::get('foo/{bar}/{baz}', function($bar, $baz){}) | |
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]')) | |
Route::get('foo/bar', array('as' => 'foobar', function(){})); | |
URL::current(); | |
URL::previous(); | |
URL::to('foo/bar', $parameters, $secure); | |
URL::action('FooController@method', $parameters, $absolute); | |
URL::route('foo', $parameters, $absolute); | |
URL::asset('css/foo.css', $secure); | |
URLs and Links | |
action('FooController@method', $parameters); | |
link_to('foo/bar', $title, $attributes, $secure); | |
link_to_asset('img/foo.jpg', $title, $attributes, $secure); | |
link_to_route('route.name', $title, $parameters, $attributes); | |
link_to_action('FooController@method', $title, $params, $attrs); | |
// HTML Link | |
asset('img/photo.jpg', $title, $attributes); | |
// HTTPS link | |
secure_asset('img/photo.jpg', $title, $attributes); | |
route($route, $parameters, $absolute = true); | |
url('path', $parameters = array(), $secure = null); | |
return Response::json(array('key' => 'value')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment