Zubair
Zubair

Reputation: 295

How to fetch data from database related to key using Laravel?

I am a beginner, and I want to show the value data into the input field related to the key but I am very confused that how can I show the value data into the input field so please if you have an idea please help me thanks.

Database table

Setting table https://ibb.co/jGFX4t2

I want to show a value data in this field, please see https://ibb.co/Mh08c9b

Settings Model

class Settings extends Model
{
    protected $table="setting";
    protected $fillable =['id','key','value'];
}

Controller

public function setting()
{
    $setting=Settings::all();
    return view('admin.setting.setting',compact('setting'));
}

HTML view

<form method="post"  action="{{route('update.setting')}}" enctype="multipart/form-data"    >     
    @csrf
    <div class="card-box">
        <div class="panel panel-heading">
            <h3>Update Settings</h3>
        </div>        
        <div class="col-lg-5">
            <div class="mt-3">
                <input type="file" name="logo_image" class="dropify"  />
            </div>
        </div>
        <div class="row">
            <div class="col-lg-5 mt-3">
                <div class="group-form">
                    <label>Contact Number*</label>
                    <input type="text" name="contact_number"  value="{{  }}"   class="form-control" >
                </div>
            </div>   
            <div class="col-lg-5 mt-3">
                <div class="group-form">
                    <label>Contact Email *</label>
                    <input type="email" name="email" value="{{    }}" class="form-control" >
                </div>
            </div>  
            <div class="col-lg-10 mt-3">
                <div class="group-form">
                    <label>Location *</label>
                    <input type="text" name="location" value="{{   }}" class="form-control" >
                </div>
            </div>    
            <div class="col-lg-5 mt-3">
                <h3> Social Links:</h3>
                <div class="group-form">
                    <label>Facebook *</label>
                    <input type="text" name="facebook" value="{{  }}" class="form-control" >
                </div>
            </div> 
            <div class="col-lg-5 mt-5">
                <div class="group-form">
                    <label>Twitter *</label>
                    <input type="text" name="twitter" value="{{}}" class="form-control" >
                </div>
            </div> 
            <div class="col-lg-6 mt-3">
                <div class="group-form">
                    <label>Linkedin *</label>
                    <input type="text" name="linkedin" value="{{}}" class="form-control" >
                </div>
            </div> 
            <div class="col-lg-7 mt-3">
                <div class="group-form ">
                    <button type="submit" id="btnsubmit" class="btn btn-danger waves-effect waves-light col-lg-2">Save</button>
                </div>
            </div> 
        </div>   
    </div> <!-- end card-box -->
</form>

Upvotes: 0

Views: 295

Answers (2)

Zubair Mukhtar
Zubair Mukhtar

Reputation: 320

please use this code.

         {{ $setting->Where('key', 'Contact_Email')->first()->value }}

Upvotes: 1

CUGreen
CUGreen

Reputation: 3184

How about something like:

{{ $setting->firstWhere('key', 'Contact_Number')->value; }}
{{ $setting->firstWhere('key', 'Contact_Email')->value; }}
{{ $setting->firstWhere('key', 'Location')->value; }}

etc

When you use Eloquent Model::all(), it returns a collection to the blade view.

Therefore you can use firstWhere to find the key value.

Upvotes: 1

Related Questions