K
K
Khaybulla Musaev2016-12-08 19:13:01
Laravel
Khaybulla Musaev, 2016-12-08 19:13:01

Is it possible to access related model properties in a query?

Guys, a question about Eloquent .
There is a Visit entity (visit), it is connected one to one with the Claim entity (complaint).
I want to pull out a complaint with one request through Eloquent, which has some Visit fields in certain values. That is - "Display complaints that have visit.ip = 127.0.0.1"
Example:
visits

id | ip | created_at
1 | 127.0.0.1 | 2016-01-01
2 | 192.168.0.1 | 2016-31-31

claims
id | reason | visit_id
1 | Spam | 1
2 | Bad quality | 2

Request type:
\Model\Claim::with("visits")->where('visits.ip', 'like', '127.0.0.1')->get();

returns an error, they say - there is no such property visits.ip
Question. How can I access a property of a related entity in a query?..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-12-08
@musaev_haybulla

// Retrieve all posts with at least one comment containing words like foo%
$posts = Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->get();

https://laravel.com/docs/5.3/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question