mindau
2/13/2017 - 12:44 PM

Laravel cheatsheet

Laravel cheatsheet

<?

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'));
<?
#routes.php

Route::get('routes', function() {
$routeCollection = Route::getRoutes();

echo "<table style='width:100%'>";
    echo "<tr>";
        echo "<td width='10%'><h4>HTTP Method</h4></td>";
        echo "<td width='10%'><h4>Route</h4></td>";
        echo "<td width='80%'><h4>Corresponding Action</h4></td>";
    echo "</tr>";
    foreach ($routeCollection as $value) {
        echo "<tr>";
            echo "<td>" . $value->getMethods()[0] . "</td>";
            echo "<td>" . $value->getPath() . "</td>";
            echo "<td>" . $value->getActionName() . "</td>";
        echo "</tr>";
    }
echo "</table>";
});



Route::get('cookies', function() {
$routeCollection = $_COOKIE;

echo "<table style='width:100%'>";
    echo "<tr>";
        echo "<td width='10%'><h4>KEY</h4></td>";
        echo "<td width='10%'><h4>VAL</h4></td>";
    echo "</tr>";
    foreach ($routeCollection as $key=>$val) {
        echo "<tr>";
            echo "<td>" . $key . "</td>";
            echo "<td>" . $val. "</td>";
        echo "</tr>";
    }
echo "</table>";
});
<?

#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.
<?

'email => "required|unique:database_table_name,database_field,$this->id"
{!! Form::hidden('id', old('id',$model->id)) !!}
<?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', '.*');