Eloquent Inverse Relationship

Update your Post model by adding this:

    public function user(){
        return $this->belongsTo('App\Models\User');
    }

We can now display the post’s user in our routes:

use App\Models\User;

/*
|--------------------------------------------------------------------------
| ELOQUENT Relationships
|--------------------------------------------------------------------------
*/

Route::get('/post/{id}/user', function($id){
    return Post::find($id)->user->name;
});

View in your browser.

Eloquent One to One Relationship

Update your post migration to have a user_id column

        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });

Update your migrations with

php artisan migrate:refresh

Add a user and a post in your database, where you add the user ID to the new post.

Then update your User model by adding this:

    public function post(){
        return $this->hasOne('App\Model\Post');
    }
The user_id is used as the default connection to Post model. Add a second parameter to use a different id name. Add a third parameter to change the post ID.

Now that we’ve added this relationship, we can display the user’s post in our routes:

use App\Models\User;

/*
|--------------------------------------------------------------------------
| ELOQUENT Relationships
|--------------------------------------------------------------------------
*/

Route::get('/user/{id}/post', function($id){
    return User::find($id)->post->title;
});

Database Eloquent ORM Soft Deleting / Trashing

Update your model to include soft deletes like this:

use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];

}

Now create a migration (name of file with flag for name of database table to add onto)

php artisan make:migration add_deleted_at_column_to_posts_tables --table=posts

Now add your new columns to this file in the up and down functions like so:

    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->softDeletes();
        });
    }


    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }

Get the new column to show up with:

php artisan migrate

To your routes file add this to soft delete an item in the database:

Route::get('/softdelete', function(){
    Post::find(11)->delete();
});

Now running queries like Post::all() won’t find the soft deleted items anymore so check the database directly.

Database Eloquent ORM Deleting

Multiple ways of deleting:

use App\Models\Post;

/*
|--------------------------------------------------------------------------
| ELOQUENT ORM (Object Relational Model)
|--------------------------------------------------------------------------
*/

Route::get('/delete', function(){
    $post = Post::find(2);
    $post->delete();
});

Route::get('/delete2', function(){
    Post::destroy(3);
});

Route::get('/delete3', function(){
    Post::destroy([4,5]);
});

Route::get('/delete4', function(){
    Post::where('is_admin',0)->delete();
});

Database Eloquent ORM Updating

use App\Models\Post;

/*
|--------------------------------------------------------------------------
| ELOQUENT ORM (Object Relational Model)
|--------------------------------------------------------------------------
*/

Route::get('/update', function(){
    Post::where('id',2)->where('is_admin',0)->update(['title'=>'NEW PHP TITLE','content'=>'More content']);
});

Second way of updating. First way uses same method as inserting.

Database Eloquent ORM Creating data and configuring mass assignment

First add a create method:

use App\Models\Post;

/*
|--------------------------------------------------------------------------
| ELOQUENT ORM (Object Relational Model)
|--------------------------------------------------------------------------
*/

Route::get('/create', function(){
    Post::create(['title'=>'the create method', 'content'=>'learning about create method']);
});

Now check URL. You should get an error like so:

Add [title] to fillable property to allow mass assignment on [App\Models\Post].

To allow us to save multiple items of data you need to edit your model with this inside the class:

protected $fillable = [
    'title',
    'content'
];

If you check the URL again you should get an error free page and if you check phpmyadmin, you’ll see a new record.

Database Eloquent ORM Inserting / Saving Data

use App\Models\Post;

/*
|--------------------------------------------------------------------------
| ELOQUENT ORM (Object Relational Model)
|--------------------------------------------------------------------------
*/

Route::get('/basicinsert', function(){
    $post = new Post;
    $post->title = 'New Eloquent title insert';
    $post->content = 'Wow Eloquent is really cool';
    $post->save();
});

Route::get('/basicinsert2', function(){
    $post = Post::find(2);
    $post->title = 'New Eloquent title insert 2';
    $post->content = 'Wow Eloquent is really cool 2';
    $post->save();
});

The second one is really an update statement using find to find by ID, then updating the title and content.