Abdallah Sakre
Abdallah Sakre

Reputation: 915

Displaying array values based on a certain condition in a blade - Laravel

The following is a sample of an array inside an array :

array:2 [▼
  0 => {#338 ▼
    +"id": 25
    +"created_at": "2019-10-18 11:13:17"
    +"updated_at": "2019-10-18 11:13:17"
    +"title": "question"
    +"body": """
       \r\n
         aasasas
      """
    +"ttype": 0
    +"cat": 0
    +"a_id": 25
    +"tag": ""
    +"appr": 0
    +"user_id": 6
    +"comment_id": 0
    +"parent_id": null
    +"ppoints": null
    +"status": 0
    +"arank": 1
    +"qatype": 1
    +"country": "Egypt"
    +"wwide": 0
  }

Is there a way to display the required array values based on a condition inside the blade ? For example, the following will show the title in the view : {{$updt_11->title}}. Can I display the title if the id=25 for example ?

A wrong example for demonstration:

{{ $updt_11->where('id',25)->select('title')->first()->title}}

Upvotes: 0

Views: 689

Answers (2)

pr1nc3
pr1nc3

Reputation: 8338

Basically if you want to use Model output in your blade, you can do something like:

{{ \App\Models\MyModel::where('id',25)->first()->title}} 
  • If you don't have a Models folder in your laravel project then your path will be : \App\MyModel

Your Model should look like (just an example) :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{

     protected $table = 'my_table';

     protected $guarded = [];

}

Upvotes: 1

Amit Senjaliya
Amit Senjaliya

Reputation: 2945

Inside blade file, you can be able to use if a condition like:

@if($updt_11->id == '25')
    {{ $updt_11->title }}
@endif

Try it.

Upvotes: 2

Related Questions