ORM addition, deletion, modification and query examples

不言
Release: 2023-03-24 18:46:02
Original
1510 people have browsed it

The content introduced in this article is about ORM addition, deletion, modification and query examples. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

<?php
namespace App\Http\Controllers\Admin;use App\Http\Controllers\Controller;
use App\Models\Admin\Member;use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;class MemberController extends Controller{
    public function index()
    {
        //查询所有记录
        //$result = Member::all();

        //查询一条记录
        $result = Member::find(8);        echo $result->created_at;        //查询多条记录
        //$result = Member::find([1,2,3]);

        //找不到时抛出异常(404页面)
        //$result = Member::findOrFail(55);

        //查询所有记录,where条件
        //$result = Member::where(&#39;id&#39;,&#39;>&#39;,1)->get();

        //分块查询
        /*$result = Member::where(&#39;id&#39;,&#39;>&#39;,1)->chunk(2,function($res){
            var_dump($res);
            return false;
        });*/
       // dd($result);

    }    //插入
    public function add(){
        //使用模型创建
        /*$result = new Member();
        $result -> username = &#39;1&#39;;
        $result -> password = &#39;2&#39;;
        $result -> email = &#39;3&#39;;
        $result ->save(); // 新增保存*/

        //使用create创建,需设置 $fillable
        //$result = Member::create([&#39;username&#39;=>&#39;11&#39;,&#39;password&#39;=>&#39;222&#39;,&#39;email&#39;=>&#39;333&#39;]);

        //其他创建方法

        //firstOrCreate 使用给定的列/值来查找数据库。如果在数据库找不到该模型,则会从第一个参数的属性乃至第二个参数的属性中创建一条记录插入到数据库
        //使用 username 查找用户,不存在则创建,存在的话也不报错
        /*$result = Member::firstOrCreate(
            [&#39;username&#39;=>&#39;1111&#39;],[&#39;password&#39;=>&#39;222&#39;],[&#39;email&#39;=>&#39;333&#39;]
        );*/

        //使用username 查找用户,不存在则创建实例,保存的话需要save
        /*$result = Member::firstOrNew(
            [&#39;username&#39;=>&#39;111111&#39;],[&#39;password&#39;=>&#39;222&#39;],[&#39;email&#39;=>&#39;333&#39;]
        );
        $result->save();*/

        //updateOrCreate 更新现有模型或在不存在的情况下则创建新的模型,不需要save
        /*$result = Member::updateOrCreate(
            [&#39;username&#39;=>&#39;11111112&#39;],
            [&#39;password&#39;=>&#39;2222&#39;,&#39;email&#39;=>&#39;22222&#39;]

        );*/

        //dd($result);
    }    //修改
    public function edit(){
        //通过模型更新数据
        /*$result = Member::find(14);
        $result -> password = &#39;3333333&#39;;
        $result -> save();*/

        //批量更新
        /*$result = Member::where(&#39;id&#39;,&#39;>&#39;,&#39;13&#39;)->update(
            [&#39;password&#39;=> &#39;11&#39;]
        );*/

        //dd($result);
    }    //删除
    public function delete(){
        //通过模型删除,返回布尔值
        /*$result = Member::find(14);
        $result -> delete();*/

        //通过主键删除,返回影响的行数
        //$result = Member::destroy(13);
        //删除多个
        //Member::destroy([1, 2, 3]);
        //Member::destroy(1, 2, 3);

        //通过查询删除,返回影响的行数
        //$result = Member::where(&#39;id&#39;,&#39;>&#39;,11)->delete();

        //软删除
        /*$result = Member::find(8);
        $result -> delete();*/


        dd($result);
    }
}
Copy after login
<?php
namespace App\Models\Admin;use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes; 
//开启模型的软删除功能
class Member extends Model{

    protected $table      = &#39;member&#39;; //与模型关联的数据表
    //指定主键
    //如果使用非递增或者非数字的主键,则必须在模型上设置 public $incrementing = false
    //如果主键不是一个整数,你应该在模型上设置 protected $keyType = string
    protected $primaryKey = &#39;id&#39;;    public    $timestamps = true;     //是否自动维护时间戳 created_at updated_at
    protected $dateFormat = &#39;U&#39;;       //模型的日期字段的存储格式
    //const CREATED_AT = &#39;creation_date&#39;;//自定义用于存储时间戳的字段名
    //const UPDATED_AT = &#39;last_update&#39;;  //自定义用于存储时间戳的字段名

    protected $fillable = [&#39;username&#39;,&#39;password&#39;]; //批量赋值 ,create 新增数据的时候需要设置 ,$guarded 为黑名单,相反

    use SoftDeletes;//使用软删除功能
    protected $dates = [&#39;deleted_at&#39;]; //软删除字段}
Copy after login

Related recommendations:

Detailed explanation of Django’s method of operating database based on ORM

The above is the detailed content of ORM addition, deletion, modification and query examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!