Reputation: 404
I have a search function, and I need to add id
of posts ( from table opinions
, column id
). In my view I already have subject
, and I get it from database with $user->subject
, but at $user->id
, I get id of user, not of the post. So, I need to fetch data correctly. I need to get id of posts based on subject .
Now, I've made a new variable which returns something like that :
$user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )
->join('role_users' , 'role_users.user_id', '=', 'opinions.user_id')
->join('roles' , 'roles.id', '=', 'role_users.role_id')
->join('users', 'users.id', '=', 'opinions.user_id')
->orWhere ( 'opinions.user_id', 'LIKE', '%' . $q . '%' )
->orWhere ( 'opinions.id', '=', 'opinions.subject')
->orWhere('opinions.category_id' ,'=', $category->id)
->orWhere ( 'opinions.user_id', '=', 'users.username')
->get ();
$topic_id = Opinion::select('id', 'subject')
->get();
Collection {#883 ▼
#items: array:20 [▼
0 => Opinion {#873 ▶}
1 => Opinion {#851 ▼
#table: "opinions"
+timestamps: false
#fillable: array:10 [▶]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
#attributes: array:2 [▼
"id" => "2"
"subject" => "4 Ways We Can Avoid Workplace Burnout"
]
Here is my view:
<?php $count_event = 1; ?>
@foreach($details as $user)
@if($count_event == 1)
<div class="row news-v2 margin-bottom-50 ">
<div class="col-sm-6 sm-margin-bottom-30">
<div class="news-v2-badge">
<div class="easy-block-v1">
<div class="easy-block-v1-badge rgba-{{ $category->color }} noticeboard-topic-category">
<i class="icon-{{ $typee }}"></i> / {{ $type }}
</div>
<?php
$video_content = preg_replace("/<img[^>]+\>/i", "", $user->information);
preg_match('/src="([^"]+)"/', $video_content, $video);
?>
<?php
$match = '';
$str = $user->information;
$start = "<iframe src='";
$end = "' width='100%' height='281'></iframe>";
$pattern = sprintf(
'/%s(.+?)%s/ims',
preg_quote($start, '/'), preg_quote($end, '/')
);
if (preg_match($pattern, $str, $matches)) {
list(, $match) = $matches;
}
?>
@if(isset($match) && $match != '')
<iframe src="{{ $match }}" width='100%' height='258'></iframe>
@elseif(isset($video[1]))
<iframe src="{{ $video[1] }}" width='100%' height='258'></iframe>
@else
<?php preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $user->information, $image); ?>
@if(isset($image['src']))
<?php $img = str_replace('&', '&', $image['src']); ?>
<img class="img-responsive category-image" src="{{ url('ass/409/258?'.$img) }}" alt="">
@else
<?php $img = "thumbnail/".$user->profile_picture; ?>
@if(@getimagesize($img))
<img class="img-responsive category-image" src="{{ url('ass/409/258?'.$img) }}" alt='' />
@else
<?php $img = "assets/img/main/img12.jpg"; ?>
<img class="img-responsive category-image" src="{{ url('ass/409/258?'.$img) }}" alt="">
@endif
@endif
@endif
<!--
<div class="card" style="width:300px">
<div class="card-body">
<?php $img = "thumbnail/".$user->profile_picture; ?>
@if(@getimagesize($img))
<img style="position: relative;top: 9px;" class="img-circle noticeboard-profile-picture-neo col-md-2" src="{{ url('ass/50/50?'.$img) }}" alt="">
@endif
<h4 class="noticeboard-title">
<a href="{{ url( $test ) }}/{{ $user->id }}_{{ Slugify::slugify( $user->subject ) }}" class="noticeboard-subject">{{ $user->subject }} </a>
</h4>
<p class="card-text">
<a href="{{ url( $test ) }}/{{ $user->id }}_{{ Slugify::slugify( $user->subject ) }}" class="btn btn-primary">Read more</a>
</div>
</div>
</div> -->
</div>
</div>
<div class="news-v2-desc" style="background-color: #f7f8fa">
<div class="row">
<div class="col-md-2">
<?php $img = "thumbnail/".$user->profile_picture; ?>
@if(@getimagesize($img))
<img style="position: relative;top: 9px;" class="img-circle noticeboard-profile-picture-neo col-md-2" src="{{ url('ass/50/50?'.$img) }}" alt="">
@endif
</div>
<div class="col-md-10 noticeboard-subjecttitle">
<h4 class="noticeboard-title" style="text-align: justify;position: relative;right: -15px;">
@foreach($topic_id as $opinion)
{{$opinion->id}}
<a href="{{ url( $test ) }}/{{ $user->id }}_{{ Slugify::slugify( $user->subject ) }}" class="noticeboard-subject">{{ $user->subject }} </a>
@endforeach
</h4>
<ul style="position: relative;right: -15px" class="list-unstyled list-inline blog-info noticeboard-ul-link">
<li>
@if($user->role_id == 1)
<i class="icon-user"></i>
<a href="{{ url('')}}/{{$user->username}}">{{$user->username}}</a>
@else
<i class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
@endif
</li>
<li>
<i style="font-size: 11px" class="icon-{{$typee}}"></i>
<a href="{{ url('') }}/{{$link}}">{{ $type }}</a>
</li>
</ul>
</div>
</div>
<?php
$information = preg_replace("/<img[^>]+\>/i", "", $user->information);
$Output = preg_replace('/<iframe.*?\/iframe>/i','', $information);
?>
<p>{{ str_limit(trim(strip_tags(preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $Output))), 200) }}</p>
<p><a style="border-radius: 0rem !important;border: 0.1rem solid #18ba9b" class="btn-z btn-xs g-mr-10 g-mb-15" href="{{ url( $test ) }}/{{ $user->id }}_{{ Slugify::slugify( $user->subject ) }}">Read more <i class="fa fa-angle-double-right margin-left-5"></i></a></p>
</div>
</div>
@endif
@endforeach
And I get every post from opinion. But now, If I use it on view with a @foreach
, I got all records ( id + subject), on every subject from my page: https://i.sstatic.net/KhVRy.jpg .
So, how can I assign correctly id
of every subject?
https://i.sstatic.net/mvh7P.jpg
Upvotes: 2
Views: 612
Reputation: 114
You can add select function in your query builder to fetch opinion id like this
$user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )
->join('role_users' , 'role_users.user_id', '=', 'opinions.user_id')
->join('roles' , 'roles.id', '=', 'role_users.role_id')
->join('users', 'users.id', '=', 'opinions.user_id')
->orWhere ( 'opinions.user_id', 'LIKE', '%' . $q . '%' )
->orWhere ( 'opinions.id', '=', 'opinions.subject')
->orWhere('opinions.category_id' ,'=', $category->id)
->orWhere ( 'opinions.user_id', '=', 'users.username')
->select('opinions.id as id','subject')
->get ();
Upvotes: 1