$mongo = new MongoClient();
$db = $mongo->mydb1;
/* Note: Here we are using the update() method. The update() method update values in the existing document */
$collection = $db->createCollection("emp_details");
$newdata = array('$set' => array("age" => "55", "salary" => "320000"));
// specify the column name whose value is to be updated. If no such column than a new column is created with the same name.
$condition = array("emp_id" => "1");
// specify the condition with column name. If no such column exist than no record will update
if($collection->update($condition, $newdata))
{
echo '<p style="color:green;">Record updated successfully</p>';
}
else
{
echo '<p style="color:red;">Error in update</p>';
}
删除:
$mongo = new MongoClient();
// name of database which is to be created
$db_name = 'local';
// get the list of database and check if DB exist, if not than create it.
$dblists = $mongo->listDBs();
if(count($dblists) > 0)
{
$count = 0;
$exist = false;
foreach($dblists['databases'] as $databases)
{
if($databases['name'] == $db_name)
{
$exist = true;
break;
}
}
}
if($exist)
{
$db = $mongo->db_name; // select the db which is to be deleted
if($db)
{
if($db->drop())
{
echo '<p style="color:green;">Database deleted successfully</p>';
}
}
}
else
{
echo '<p style="color:red;">No such database exist</p>';
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Role;
use Validate;
use Collective\Html\FormFacade;
class RoleController extends Controller
{
public function index(){
$roles = Role::all();
return view('role.index',compact('roles'));
}
public function create(){
return view('role.create');
}
public function store(Request $request)
{
request()->validate([
'name' => 'required',
]);
Role::create($request->all());
return redirect()->route('role')
->with('success','Role created successfully');
}
public function edit($id){
$roles = Role::find($id);
return view('role.edit',compact('roles'));
}
public function update(Request $request, $id){
request()->validate([
'name' => 'required',
]);
Role::find($id)->update($request->all());
return redirect()->route('role')
->with('success','Role updated successfully');
}
public function destroy($id)
{
Role::find($id)->delete($id);
return redirect()->route('role')
->with('success','Role updated successfully');
}
}
创建模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = [ 'name' ];
}
5条答案
按热度按时间fslejnso1#
似乎有一个包可以让你使用MongoDB和Eloquent。我不喜欢在这里不引用信息就链接外部资源,但是复制他们的自述文件听起来也会适得其反。说明看起来很简单,所以我希望这可以帮助你:Laravel MongoDB。
yruzcnhs2#
示例代码MongoDb + Php:
**$mongo = new MongoClient();$db = $mongo-〉mydb1;
更新:
删除:
在Laravel的情况下,你必须学习基本的CRUD操作,然后你会很好地使用它。
icnyk63a3#
ktca8awb4#
创建路由
创建控制器
创建模型
在布局文件夹中创建布局文件
创建index.blade.php
创建create.blade.php
创建edit.blade.php
bqucvtff5#
在routes/web.php中添加路由
在控制器中添加方法
创建日志模型文件路径:App/Models/mongo/Log.php
在config/app.php中添加下面的行
在config/database.php中添加下面一行
运行低于API curl 请求
使用Laravel在MongoDB中存储日志数据Create Below File app/Http/Middleware/ApiLogs.php
在Http/Kernel.php中添加下划线