public function getNameAttribute($value)
{
return ucfirst($value);
}
|
// app/Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
|
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
|
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* Get the post title.
*
* @param string $value
* @return string
*/
public function getNameAttribute($value)
{
return ucfirst($value);
}
}
|
// app/Http/Controllers/AccessorController.php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AccessorController extends Controller
{
public function index(Request $request)
{
// get the post-id from request params
$post_id = $request->get("id", 0);
// load the requested post
$post = Post::find($post_id);
// check the name property
return $post->name;
}
}
|
Route::get('accessor/index', 'AccessorController@index');
|
// tests/Unit/AccessorTest.php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AccessorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
|
// tests/Unit/AccessorTest.php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AccessorTest extends TestCase
{
/**
* Test accessor method
*
* @return void
*/
public function testAccessorTest()
{
$db_post = DB::select('select * from posts where id = 1');
$db_post_title = ucfirst($db_post[0]->title);
$model_post = Post::find(1);
$model_post_title = $model_post->title;
$this->assertEquals($db_post_title, $model_post_title);
}
}
|
// tests/Feature/AccessorTest.php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AccessorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
|
// tests/Feature/AccessorTest.php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AccessorTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$db_post = DB::select('select * from posts where id = 1');
$db_post_title = ucfirst($db_post[0]->name);
$response = $this->get('/accessor/index?id=1');
$response->assertStatus(200);
$response->assertSeeText($db_post_title);
}
}
|