How to dynamically populate checkbox from database - laravel-4

I want to populate the view.blade.php page with the value given in add.blade.php. I am getting my form values using a variable $form_value from the database. How can I use this variable to populate the chekbox in view page.
view.blade.php
<div class = "row">
<div class="col-lg-4">
<label>
<input type="checkbox" name="hoogwerker"><b> Hoogwerker gebruikt</b>
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="koerier"><b> Materiaal toegeleverd via koerier</b>
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="bijgewerkt"><b> Kraanboek bijgewerkt</b>
</label>
</div>
</div>
add.blade.php
<div class = "row">
<div class="col-lg-4">
<label>
<input type="checkbox" name="hoogwerker"> Hoogwerker gebruikt
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="koerier"> Materiaal toegeleverd via koerier
</label>
</div>
<div class="col-lg-4">
<label>
<input type="checkbox" name="bijgewerkt"> Kraanboek bijgewerkt
</label>
</div>
</div>

Laravel will do it automatically for you if your blade template is done using Form::model():
{{ Form::model($model) }}
{{ Form::checkbox('hoogwerker') }}
{{ Form::label('hoogwerker', 'Hoogwerker gebruikt') }}
{{ Form::checkbox('koerier') }}
{{ Form::label('koerier', 'Materiaal toegeleverd via koerier') }}
{{ Form::close() }}
And your controller should do something like
$model = User::find(1);
return View::make('your-view')->with('model', $model);

Related

The PATCH method is not supported for this route. when second form is submitted

I've been working on a CMS system for a website im building and it all went good untill I added 2 forms on the same page (both updating different things) and yet it still gives me the error in the title while the other form works fine?
The page banner is the one which returns the error and the 'normal' update works fine!
Web.php
Route::patch('/beheer/paginas/{product}', 'PageController#update')->middleware('auth')->name('beheer.pages.update');
Route::patch('/beheer/paginas/update-banner/{product}', 'PageController#update-banner')->middleware('auth')->name('beheer.pages.banner.update');
Also shows in php artisan route:list that it's a PATCH method route.
My view:
#extends('layouts.beheer')
#section('content')
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Pagina bewerken</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form action="{{ route('beheer.pages.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<div class="form-group">
<label>Zichtbaarheid:</label>
<select class="custom-select" name="page_hidden">
<option value="0" #if (!$page->page_hidden) selected #endif>Zichtbaar</option>
<option value="1" #if ($page->page_hidden) selected #endif>Verborgen</option>
</select>
</div>
<div class="form-group">
<label>Pagina naam:</label>
<input type="text" name="page_name" value="{{ $page->page_name ? $page->page_name : old('page_name') }}" class="form-control" placeholder="Pagina naam">
</div>
<div class="form-group">
<label>Pagina tekst:</label>
<textarea name="page_text" class="form-control">{{ $page->page_text ? $page->page_text : old('page_meta_title') }}</textarea>
</div>
<div class="form-group">
<label>Pagina title SEO:</label>
<input type="text" name="page_meta_title" value="{{ $page->page_meta_title ? $page->page_meta_title : old('page_meta_title') }}" class="form-control" placeholder="Pagina titel SEO">
</div>
<div class="form-group">
<label>Pagina beschijving SEO:</label>
<input type="text" name="page_meta_description" value="{{ $page->page_meta_description ? $page->page_meta_description : old('page_meta_description') }}" class="form-control" placeholder="Pagina beschijving SEO">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Banner</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<input type="hidden" name="image_section_banner" value="1">
<input type="hidden" name="page_id" value="{{ $page->id }}">
<div class="form-group">
<img src="{{ asset($page_banner ? $page_banner->image_large_url : asset('assets/img/banner-home.jpg')) }}" class="img-fluid" style="max-height:250px;">
</div>
<div class="form-group">
<label>Afbeelding:</label>
<input type="file" name="image" value="{{ old('image') }}" class="form-control">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
If there is any additional information needed please let me know!
There is an attribute missing on your 2nd <form>. Here is what you have:
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
But it should look like this:
<form action="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
Since you did not specify an action attribute, it is submitting to the current url which probably doesn't have a patch route defined.

checked for multiple checkbox in update form

This is my blade file for the update form. So how I should I retrieve those data in the form by checked the checkbox.
<div class="form-group row">
<label class="col-md-3 col-form-label form-control-label" for="input-description">Hobbies</label>
<div class="col-md-9">
<input type="checkbox" name="hobbies[]" class="form-control-label" value="football" > Football
<input type="checkbox" name="hobbies[]" class="form-control-label" value="basketball"> Basketball
<input type="checkbox" name="hobbies[]" class="form-control-label" value="table_tennis"> Table Tennis
<input type="checkbox" name="hobbies[]" class="form-control-label" value="hockey"> Hockey
<input type="checkbox" name="hobbies[]" class="form-control-label" value="others"> Others
</div>
</div>
</div>
This is my Controller
public function edit($id)
{
$student=Student::find($id);
return view('students.edit', compact('student'));
}
In the database, .hobbies are stored in hobbies column like this
["basketball","football",'table_tennis']
You should use foreach loop inside blade that will be more maintainable:
<div class="form-group row">
<label class="col-md-3 col-form-label form-control-label" for="input-description">Hobbies</label>
<div class="col-md-9">
#foreach($hobbies as $hobby)
<input type="checkbox" name="hobbies[]" #if(in_array($hobby,$checked_hobbies)) checked #endif class="form-control-label" value="{{ $hobby }}" > {{ $hobby }}
#endforeach
</div>
</div>
and of course you should pass hobbies and checked_hobbies array in controller:
$hobbies = ["basketball","football",'table_tennis'];
$checked_hobbies = ["basketball","football"];
return view('test', compact('hobbies','checked_hobbies'));

Laravel : Create Button returns the same view but blank and does not create any record

I am new to laravel and I am working on creating a CMS. I have created a view to create posts but after I input data and click create to submit, page refreshes and shows the same view but with no input and no record created, when it should show a flash message and return the posts index view.
Here is my VIEW HTML:
#section('content')
<div class="card card-default">
<div class="card-header">
{{ isset($post) ? 'Edit Post' : 'Create Post' }}
</div>
<div class="card-body">
<form action="{{ isset($post) ? route('posts.update', $post->id) : route('posts.store') }}" method="POST" enctype="multipart/form-data">
#csrf
#if (isset($post))
#Method('PUT')
#endif
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" id="title" value="{{isset($post) ? $post->title : ""}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" cols="5" rows="3" class="form-control">{{isset($post) ? $post->description : ""}}</textarea>
</div>
<div class="form-group">
<label for="content">Content</label>
<input id="content" type="hidden" name="content" value="{{isset($post) ? $post->content : ""}}">
<trix-editor input="content"></trix-editor>
</div>
<div class="form-group">
<label for="published_at">Published at</label>
<input type="text" class="form-control" name="published_at" id="published_at" value="{{isset($post) ? $post->published_at : ""}}">
</div>
#if (isset($post))
<div class="form-group">
<img src="{{ asset('/storage/'.$post->image) }}" alt="" style="width: 100%">
</div>
#endif
<div class="form-group">
<label for="category">Category</label>
<select name="category" id="category" class="form-control">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" class="form-control" name="image" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">
{{ isset($post) ? 'Update Post' : 'Create Post' }}
</button>
</div>
</form>
</div>
</div>
#endsection
Here is METHOD:
public function store(CreatePostRequest $request)
{
$image = $request->image->store('posts');
Post::create([
'title'=>$request->title,
'description'=>$request->description,
'image'=>$image,
'content'=>$request->content,
'published_at' => $request->published_at,
'category_id' => $request->category
]);
session()->flash('success', 'Post Created Successfully');
return redirect(route('posts.index'));
}
What am I doing wrong?
Thanks,

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

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>
please use this code.
{{ $setting->Where('key', 'Contact_Email')->first()->value }}
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.

Resources