PHP code example of jedrzej / withable

1. Go to this page and download the library: Download jedrzej/withable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jedrzej / withable example snippets


use Jedrzej\Withable\WithableTrait;

class Post extends Eloquent
{
	use WithableTrait;
	
	// either a property holding a list of loadable relations...
	protected $withable = ['owner', 'forum'];
	
	// ...or a method that returns a list of loadable relations
	protected function getWithableRelations()
	{
		return ['owner', 'forum'];
	}
}

protected $withable = ['*'];

// return all posts with the user who created them and forum where they were posted
Post::withRelations(['owner', 'forum'])->get();
// return all posts with the user who created them
Post::withRelations('owner')->get();
    
// return all posts with the user who created them and forum where they were posted by appending to the URL
?with[]=owner&with[]=forum
// return all posts with the user who created them
?with=owner
//and then calling
Post::withRelations()->get();

// define local scope in Comment model
public function scopeApproved($query) {
  $query->whereIsApproved(true);
}

// append the following to the URL in order to load "comments" relation with "approved" local scope applied
?with=comments:approved

// load the posts in your controller
Post::withRelations()->get();

protected $withParameterName = 'relations';

public function getWithRelationsList() {
  return Request::get('relations');
}