vendredi 6 septembre 2019

How to get data in first model that doesn't have equal foreign key on second model

I want to get data in request-model that not having data on status-model.

OUTPUT:

This is the request ID

4

3

2

1

This is status and request ID

Status ID:1 Request ID:2

Status ID:2 Request ID:4

Question: How can I get the data in request model that doesn't have on status model.

I want to get the request ID 3 and 1

*/

$requests = Request::orderBy('id','desc')->get();
        $statuses = Status::orderBy('id','desc')->get();

        echo "<h3>This is request ID</h3>";
        foreach ($requests as $request) {
            echo "<h3>".$request->id."</h3>";
        }

        echo "<h3>This is status and request ID</h3>";
        foreach ($statuses as $status) {
            echo "<h3>Status ID: ".$status->id."&nbsp;&nbsp; Request ID: ".$status->request->id."</h3>";
        }



Request Model :

class Request extends Model
{
    protected $table = 'requests';

    public $primaryKey = 'id';

    public $timestamp = true;

    public function status(){
        return $this->hasOne('App\Status');
    }

}

Status Model:

class Status extends Model
{
    protected $table = 'statuses';

    public $primaryKey = 'id';

    public $timestamp = true;

    public function request(){
        return $this->belongsTo('App\Request','request_id');
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire