# CRUD

Tea has a small ORM called Goku, Goku currently is not very advanced and can only do the following: all, find, where, create, update, delete

# all

Return all rows.

$products = Product::all();

# find

Find a row by ID (you can change ID to something else, like slug).

$product = Product::find(1);

# where

Find rows by a where clause.

$products = Product::where('active', '=', true);

where returns an array, thus you can access the first row as:

Product::where('active', '=', true)[0]

That acts similar to first() in Laravel.

# create

Create a new row.

$product = Product::create($data);

# update

Update an existing row.

$product = Product::find(1);

$product->update($data);

# delete

Delete an existing row.

$product = Product::find(1);

$product->delete();

# Relationships

Tea has a small ORM called Goku, Goku currently is not very advanced and can only do the following: belongsTo, hasMany

# belongsTo

<?php

namespace App;

use Devlob\Database\Goku\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

In the example above, because of the method name (user), Tea will look for a user_id inside the posts table, if you want to override the method name, you can pass the ID as a second argument:

<?php

namespace App;

use Devlob\Database\Goku\Model;

class Post extends Model
{
    public function owner()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# hasMany

<?php

namespace App;

use Devlob\Database\Goku\Model;

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

In the example above, because of the class name (User), Tea will look for a user_id inside the posts table, if you want to override the foreign key, you can pass the foreign key as a second argument:

<?php

namespace App;

use Devlob\Database\Goku\Model;

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class, 'owner_id');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13