route/web.php
<?php
use Illuminate\Support\Facades\Route;
use Laravel\Fortify\Fortify;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('json',function() {
return response()->json(['name' => 'Virat Gandhi', 'state' => 'Gujarat']);
});
Route::get('json1',function() {
return response()->json(User::all());
});
You can also put in route/api.php
this will prepend api to the url
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('api')->get('/user', function (Request $request) {
return response()->json(User::all());
});
If you use the auth api middleware it will require an api token
Route::middleware('auth:api')->get('/user', function (Request $request) {
return response()->json(User::all());
});
Column not found: 1054 Unknown column 'api_token' in 'where clause' (SQL: select * from `users` where `api_token` =
You will need to create a migration to add a api token column to users table
php artisan make:migration add_api_token_to_users_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddApiTokenToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 64);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
Schema::dropIfExists('api_token');
});
}
}
- Log in to post comments