链接Mongodb和Laravel的问题,在尝试我的API时没有回应
我正在使用Laravel和Homestead托管我的应用程序,因此我遵循MongoDB教程在Laravel中集成,因为我同时使用MySQL和MongoDB数据库,但是我可能会很简单,因为我保留,所以我无法抓住在尝试通过Postman在我的数据库中添加新条目时会遇到错误。
。
DB_CONNECTION=mysql
DB_HOST=192.168.56.56
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
<?php
namespace App\Models;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;
class File extends Model
{
protected $connection = 'mongodb';
protected $collection = 'files';
use HasFactory, Uuid;
public function ticket()
{
return $this->belongsToMany(Ticket::class);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
class FileController extends Controller
{
public function show($name)
{
return view('file', [
'file' => File::where('name', '=', $name)->first()
]);
}
public function store(Request $request)
{
$file = new File;
$file->name = $request->name;
$file->save();
return response()->json(["result" => "ok"], 201);
}
}
<?php
use App\Http\Controllers\FileController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('/file/{name}', [FileController::class, 'show']);
<?php
use App\Http\Controllers\FileController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::resource('files', FileController::class)->only([
'destroy', 'show', 'store', 'update'
]);
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mongodb'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', 'mongodb+srv://username:password@<atlas-cluster-uri>/myappdb?retryWrites=true&w=majority'),
'database' => 'myappdb',
],
], ...
这是我在Postman/Web浏览器上输入的查询,以读取或添加一个名为doc1的新文档:
and:
我是否在配置文件中设置了任何错误?还是URI错了?
编辑:我尝试将这些更改放在文件中:
.env:
MONGO_DB_HOST=192.168.56.56
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongocrud (I don't really know what name to put here to be honest)
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=
database.php:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', '192.168.56.56'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => []
],
我现在唯一的区别是我的获取方法: “尝试读取属性”名称“ null”
I'm using Laravel and homestead to host my application, so I followed the MongoDB tutorial for its integration in Laravel since I'm using both a MySQL and A MongoDB database but there's probably something pretty simple that I can't catch since i keep getting errors while trying to add a new entry in my database through PostMan.
.env :
DB_CONNECTION=mysql
DB_HOST=192.168.56.56
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
File.php:
<?php
namespace App\Models;
use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;
class File extends Model
{
protected $connection = 'mongodb';
protected $collection = 'files';
use HasFactory, Uuid;
public function ticket()
{
return $this->belongsToMany(Ticket::class);
}
}
FileController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
class FileController extends Controller
{
public function show($name)
{
return view('file', [
'file' => File::where('name', '=', $name)->first()
]);
}
public function store(Request $request)
{
$file = new File;
$file->name = $request->name;
$file->save();
return response()->json(["result" => "ok"], 201);
}
}
web.php :
<?php
use App\Http\Controllers\FileController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('/file/{name}', [FileController::class, 'show']);
api.php:
<?php
use App\Http\Controllers\FileController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::resource('files', FileController::class)->only([
'destroy', 'show', 'store', 'update'
]);
database.php :
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mongodb'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', 'mongodb+srv://username:password@<atlas-cluster-uri>/myappdb?retryWrites=true&w=majority'),
'database' => 'myappdb',
],
], ...
This is how my MongoDB database looks like:
And this is the queries I'm entering on PostMan/web browser to both read or add a new document called Doc1:
And:
Have I set anything wrong in my config files? or is the URI wrong?
EDIT: I've tried to put this changes in my files:
.env:
MONGO_DB_HOST=192.168.56.56
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongocrud (I don't really know what name to put here to be honest)
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=
database.php:
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', '192.168.56.56'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => []
],
The only difference I havbe now is for my GET method:
"Attempt to read property "name" on null"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论