Tinker is the tool for command line debugging in Laravel.
To start the tool, type
php artisan tinker
Create an object from Model
In tinker command line, objects can be created and saved to database. E.g.
$user = new App\Model\User
$user->name = "Tboxmy"
$user->email = "tboxmy@yahoo.com"
$user->save()
Then to display the content of object;
$user
Finding data from model
The eloquent function find( ) allows retrieving from the database, given its default index id. e.g.
$user = App\Model\User::find(2)
Then to display the content of object;
$user
Another approach is to use the function where( ) to retrieve based on the table column names. e.g.
$user = App\Model\User::where('name','Tboxmy')->first( )
Or to retrieve all those related values
$user = App\Model\User::where('id', '>', 2)->first( )
Then to display the content of object;
$user
Introduction to app() and how to call a method in a controller?
A helper object app( ) allows access to the different functions to access a model or controller. The call( ) function is one of the app( ) functions, it is used to access a method inside a controller. e.g. in the controller have a method declared as following;
public function methodName($id=0, $name=null){
. . .
}
The function call( ) can then be used this way;
app()->call(' App\Http\Controllers\HomeController@methodName');
When working with different Controllers, its useful to declare these as variables using the make( ) function. Here is how its done;
$controller = app()->make('App\Http\Controllers\HomeController');
app()->call([$controller, 'methodName'] );
How to pass parameters to methods in a controller?
This is done with the function call( ) where parameter 2 contains the method parameters. Using the method above, here is how its done;
app()->call([$controller, 'methodName'], [ 'id' => 2, 'name' => 'Tboxmy'] );
No comments:
Post a Comment