Laravel Migrations

Laravel needs an empty database created either through command line or PHPMySQL.

Check .env file that MySQL database name matches with correct username and password.

Open terminal and type:

php artisan migrate

The information for these tables are kept in the database/migrations folder.

Here is a list of all types of migrations that can be done:

  migrate:fresh          Drop all tables and re-run all migrations
  migrate:install        Create the migration repository
  migrate:refresh        Reset and re-run all migrations
  migrate:reset          Rollback all database migrations
  migrate:rollback       Rollback the last database migration
  migrate:status         Show the status of each migration

Passing data to views

One data variable to view

Create a custom route:

Route::get('/post/{id}',[PostsController::class, 'show_post']);

Create custom controller function that passes url data plus a string to the view

public function show_post($id){
    return view('post')->with('id',$id);
}

Create a post.blade.php file and add this line to it:

<h1>Post {{$id}}</h1>

You’ll see the url folder in the page now

Sending multiple data to view

Update your controller function to use compact like so:

public function show_post($id, $name, $another){
    return view('post', compact('id','name','another'));
}

Update route like this:

Route::get('/post/{id}/{name}/{another}',[PostsController::class, 'show_post']);

Update view:

<h1>Post {{$id}} {{$name}} {{$another}}</h1>

Passing data to controller

First Method

In your controller (PostsController), edit one of the functions to pass a variable like this:

public function index($id)
{
    return "its working ".$id;
}

Edit your routes to accept the variable like so:

Route::get('/post/{id}', [PostsController::class, 'index']);

Second Method

Add your controller as a resource like this:

Route::resource('posts', PostsController::class);

Then type this into terminal to find which method accepts variables like this:

php artisan route:list

GET|HEAD        posts ........................................... posts.index › PostsController@index  
  POST            posts ........................................... posts.store › PostsController@store  
  GET|HEAD        posts/create .................................. posts.create › PostsController@create  
  GET|HEAD        posts/{post} ...................................... posts.show › PostsController@show  
  PUT|PATCH       posts/{post} .................................. posts.update › PostsController@update  
  DELETE          posts/{post} ................................ posts.destroy › PostsController@destroy  
  GET|HEAD        posts/{post}/edit ................................. posts.edit › PostsController@edit

Method show accepts a variable. So edit the show function in PostsController to display the variable on screen:

public function show($id)
{
    return "hi ".$id;
}

Target class controller does not exist – Laravel 8

In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property’s value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel.

Laravel 8.x Docs – Release Notes

You need to use the Fully Qualified Class Name for your Controllers when referring to them in your routes when not using the namespace prefixing.

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']); 
// or 
Route::get('/users', 'App\Http\Controllers\UserController@index');

Create new Laravel project

Create the project

Typical way

Open cmd or git bash and find the directory you want to install the Laravel project into. Then just add this line:

composer create-project laravel/laravel example-app

This will create a new project that’s inside folder example-app. When its done installing you will go into the folder to access the project

Quicker Way

Add this to git bash:

composer global require laravel/installer

To update this after its installed do this:

composer global update laravel/installer

Now write the name of your new project:

laravel new example-app

Create Database

Create a new database in PHPMySQL

Add that info into the .env file so project can access the database.

Open in Visual Studio Code

Right click on project folder and choose Open with Code

Type this in the terminal:

php artisan serve

How to start, stop, and restart a cron job

Start a cron job

A cron job is started the moment it is added to the crontab. Note that the task may fail to run if the cron daemon isn’t started. To start the cron service on your Linux machine, run one of the following commands, depending on your Linux distro.

sudo /etc/init.d/cron start

Stop a cron job

You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.

To stop all cron jobs at once and maybe resume them later, you can stop the cron daemon using the following commands:

sudo /etc/init.d/cron stop

Restart a cron job

To restart the cron daemon, run the following commands:

sudo /etc/init.d/cron restart