Laravel Validation If the collection contains a foreign key

I was wondering if there is a function or something else where you can get a different element from the collection than the primary key ... For example, if the voices have the foreign key 'user_id', how can I check this? The laravel document was just an example of checking the primary key with contains (). Can anyone help me out?

An example that checks if there is a vote with id = 2

@foreach($projects as $project) @if ($project->votes->contains(2)) // @endif @endforeach 

I would like something to check if there is a vote that has user_id = = id in user id

 @foreach($projects as $project) @if ($project->votes->contains('user_id' == Auth::id())) // @endif @endforeach 
+6
source share
2 answers
 if ($votes->contains('user_id', auth()->id())) { // } 
+13
source

In your model

 public static checkForeign($thisId) { ( $thisId == Auth::user()->id ) ? return true : return false; } 

In view

 @if ( ModelName::checkForeign($project->votes->id) ) // Do something @endif 
+1
source

Source: https://habr.com/ru/post/978968/


All Articles