Home > Back-end >  How to check if a specific string contains in a db value and display that string on the blade in lar
How to check if a specific string contains in a db value and display that string on the blade in lar

Time:08-07

In my laravel 9 application's DB I have a table called members. There I have a column for a summary.

I need to find if any user has entered a text for the summary which includes the string, "ACCURA".

If there are any, then when displaying users' details in the blade, for the user's last name ACCURA string needs to be concatenated.

EG: if the user's last name is Adams and he has an ACCURA string in his summary text value, his last name should display as

Adams ACCURA

if not it should be just Adams

Following is my controller's index function

public function index(Request $request)
    {
        $findString = 'ACCURA';
        $accuraString = Member::where('summary', 'like', '%' . $findString . '%')->get();
        ;
        if ($request->filled('search')) {
            $members = Member::search($request->search)->get(); // search by value
        } else {
            $members = Member::orderBy('id','asc')->paginate('5'); // list 10 rows
        }

        return view('members', compact('members','accuraString'));
    }

And following is my blade

@foreach ($members as $member)
                    <tr>
                        <td>{{ $member->id }}</td>
                        <td>{{ $member->firstname }}</td>
                        <td>{{ $member->lastname }}{{ $accuraString }}</td>
                        <td>{{ $member->dateofbirth }}</td>
                        <td>{{ $member->dsdivision }}</td>
                        <td>
                            <form action="{{ route('members.destroy',$member->id) }}" method="Post">
                                <a  href="{{ route('members.edit',$member->id) }}">Edit</a>
                                @csrf
                                @method('DELETE')
                                <input name="_method" type="hidden" value="DELETE">
                                <button type="submit" onclick="return confirm('Are you sure?')" >Delete</button>
                            </form>
                        </td>
                    </tr>
                @endforeach

Here the blade output is wrong,

How can I display the ACCURA text on my blade with the last name only if the summary contains the string ACCURA

CodePudding user response:

public function index(Request $request)
{
   $accuraString = 'ACCURA';
   $members = Member::all();
   if ($request->filled('search')) {
      $members = $members->where('column you want', $request->search) ; // can change search "like"
   }
   $member = $members->paginate(5);

   return view('members', compact('members','accuraString'));
}


@foreach ($members as $member)
   @php
      $checkAccuraString = strpos($member->summary, $accuraString);
   @endphp
   <tr>
      <td>{{ $member->id }}</td>
      <td>{{ $member->firstname }}</td>
      <td>
          {{ $member->lastname }}
          @if ($checkAccuraString)
             {{ $accuraString }}
          @endif
      </td>
      <td>{{ $member->dateofbirth }}</td>
      <td>{{ $member->dsdivision }}</td>
      <td>
          <form action="{{ route('members.destroy',$member->id) }}" method="Post">
             <a  href="{{ route('members.edit',$member->id) }}">Edit</a>
             @csrf
             @method('DELETE')
             <input name="_method" type="hidden" value="DELETE">
             <button type="submit" onclick="return confirm('Are you sure?')" >Delete</button>
           </form>
      </td>
   </tr>
@endforeach
  • Related