Wednesday, March 15, 2017

How to create the conditions on the model of eloquent? (Laravel 5.3)

My delete method on the controller is like this :

<?php
public function deleteMessage($id, $seller_id, $buyer_id)
{
    //if the seller delete
    if($seller_id == auth()->user->id)
        //const DELETED_AT = 'deleted_by_seller';
    //if the buyer delete
    else($buyer_id == auth()->user->id)
        //const DELETED_AT = 'deleted_by_buyer';

    $result = Message::destroy($id);
    return $result;
}

My model is like this :

<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent; 
use Jenssegers\Mongodb\Eloquent\HybridRelations;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
class MessageThread extends Eloquent
{
    use HybridRelations; 
    use SoftDeletes; 
    // const DELETED_AT = 'deleted_by_seller';
    // const DELETED_AT = 'deleted_by_buyer';
    protected $connection = 'mongodb';
    protected $dates = ['deleted_by_seller', 'deleted_by_buyer'];
    protected  $fillable = ['subject', 'information', 'created_at', 'update_at'];
}

I wanted to like this :

if the seller delete the message then : const DELETED_AT = 'deleted_by_seller';

if the buyer delete the message then : const DELETED_AT = 'deleted_by_buyer';

How can I do it?



via moses toh

Advertisement