Replicate a Model
You can replicate a model in Laravel using the replicate()
method, which creates a new instance of the model with the same details as the original. Additionally, you can modify any field values before saving the new instance to the database.
$user = User::find(1);
$newUser = $user->replicate();
$newUser->save();
Select Attributes in find()
Method
When using the find()
method in Laravel, you can select specific attributes to retrieve by passing them as the second argument.
$user = User::find(1, ['name', 'status']);
Expressive "where" Syntax
You can make this query more expressive by suffixing the attribute to the where clause itself.
$users = User::whereStatus('active')->get();
Model Properties
Here are some properties of an Eloquent model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users'; // users table is associated with model we can change here.
protected $connection = 'sqlite'; // we can customize our database connection.
protected $fillable = ['name', 'email', 'password']; // The attributes that are mass assignable.
protected $dates
See You in next Laravel Tips & Tricks 😊