PHP Artisan

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the list command. It will give you a result a bit like this:

Laravel - aha moments 1
PHP Artisan List command

Every command also includes a “help” screen which displays and describes the command’s available arguments and options. To view a help screen, precede the name of the command with help. This gives a quick overview on what commands you can fire in the terminal for the specific command.

A command like php artisan route:list gives a quick overview of available routes in the application. This is a great feature when it comes to handling actions for the controller of the specific application.

Laravel - aha moments 2

Another great and really usefull feature is the option to create a controller and model at the same time. Simply fire a command like php artisan make:controller <controllerName> -r -m – This will generate both the model and controller. Also it makes the dependency injection right away with CRUD functionality.

Laravel and PHP

When programming with PHP using the Laravel framework it’s easy to use the data from the controller in the view.

THE CONTROLLER CLASS

public function show($id)
    {
    	$project = Project::findOrFail($id);

    	return view('projects.show', compact('project'));
    }

THE VIEW FILE

<h1 class="title">{{ $project->title }}</h1>

	<div class="content">{{ $project->description }}</div>

	<p>
		<a href="/projects/{{ $project->id }}/edit">Edit</a>
	</p>

Also when working with forms in the views it’s easy to specify POST-method for the form and include CSRF for security.

<form method="POST" action="/projects/{{ $project->id }}" style="margin-bottom: 1em;">
		{{ method_field('PATCH') }}
		{{ csrf_field() }}

		<div class="field">
			<label class="label" for="title">Title</label>

			<div class="control">
				<input type="text" class="input" name="title" placeholder="Title" value="{{ $project->title }}">
			</div>
		</div>

Controller optimization

public function show($id)
    {
    	$project = Project::findOrFail($id);

    	return view('projects.show', compact('project'));
    }

Whenever i show a project in this example i have to accept the id as a wildcard. I then perform a query to find it and save it as a variable. However it turns out that Laravel has a feature called route model binding that will do this work for me – very easily.

public function show(Project project)
    {
    	return view('projects.show', compact('project'));
    }

This gives me the exact same result in the frontend when i parse it. What happens behind the scene here is that Laravel is picking up, that i am trying to load a project and it also notes that there is a wildcard called project. What it is doing is simply putting 2 and 2 together.

I have this wildcard i a route called project. When i the type in the Project for the controller i probably wanna get that project with the given ID. Laravel dows this behind the scenes and passes it to the controller and automatically get’s resolved.

Optimizing storing procedure

Normally i wrote the code like in the example below. It turn out that Laravel has a create method built ind, which means i can simple re-write the code like in the example below:

public function store()
    {
    	$project = new Project();

    	$project->title = request('title');
    	$project->description = request('description');

    	$project->save();

    	return redirect('/projects');
    }

After the re-write to the new create method

public function store()
    {
    	Project::create([
    		'title' => request('title'),
    		'description' => request('description')
    	]);

    	return redirect('/projects');
    }

Because Laravel has a built in way to protect us from hackers trying to manipulate fields that we don’t populate. We simply have to enter the title and description as a fillable field for the database. Like this in the model for Project:

class Project extends Model
{
    protected $fillable = [
    	'title',
    	'description'
    ];
}

We can also do it the other way around and say accept everything except these fields. This property is the inverted way of using fillable:

class Project extends Model
{
    protected $guarded= [
    	
    ];
}

The above code snippet would allow me to create a new project without specifying the fields for mass assignment.

Leave a Reply