public function index(){
DB::collection('users') //选择使用users集合
->insert([ //插入数据
'name' => 'wjb',
'age' => 29
]);
$res = DB::collection('users')->get(); //查询所有数据
dd($res);
}
namespace App;
use Moloquent;
use DB;
class User extends Moloquent {
protected $connection = 'mongodb'; //库名
protected $collection = 'users'; //文档名
protected $primaryKey = '_id'; //设置id
protected $fillable = ['id', 'name', 'phone']; //设置字段白名单 }
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;
class UserController extends Controller
{
public function index(){
User::create([ //插入数据
'_id' =>1,
'name' =>'boss',
'phone' =>1308888888
]);
dd(User::all()); //查询并打印数据
}
}