Laravel : throw new MethodNotAllowedHttpException($others); - laravel-5

After much research on this forum, but elsewhere on the Internet, "Cat got your tongue".
Many messages speak about this subject, but I can not correct my mistake on my side.
I use the same form to add or modify my data.
I have an error with my form only when I want to modify my data.
Wep.app :
// Gérer les personnes (sociétés, personnes...) photographiées
Route::get('tiers', 'tiersController#afficheliste'); // Fait
Route::get('tiersajouter', 'tiersController#ajouter'); // Fait
Route::post('tiersmodifier', 'tiersController#modifier'); // Fait
Route::post('tiersupdatesql', 'tiersController#updatesql'); // Fait
Route::post('tierssupprimer', 'tiersController#supprimer'); // Fait
Route::post('tiersreactiver', 'tiersController#reactiver'); // Fait
ctrltiersRequest :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ctrltiersRequest extends FormRequest {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize() {
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules() {
return [
'tiersNom' => 'required|min:5',
'ville' => 'required',
'tiersiptc1' => 'required',
'tiersiptc2' => 'required'
];
}
public function messages() {
return [
'tiersNom.required' => "Le nom est obligatoire",
'tiersNom.min' => "Le nom doit faire au minimum 5 caractères",
'ville.required' => "La ville est obligatoire",
'tiersiptc1.required' => "Le thème principal est obligatoire",
'tiersiptc2.required' => "Le sous-thème est obligatoire"
];
}
}?>
tiersModif.blade :
I can not paste the contents of my blade file into this message. I add it in a reply to this message. I am sorry...
tiersControllers :
public function updatesql(ctrltiersRequest $request) {
//public function updatesql(tiersRequest $request) {
//['tiers_id' => (int)$request->tiersModif],
$tiers = tiers::updateOrCreate(
['tiers_id' => $request->tiersModif],
['tiers_nom' => $request->tiersNom,
'tiers_prenom' => $request->tiersPrenom,
'tiers_societeproduction' => $request->production,
'tiers_ville' => $request->ville,
'tiers_iptc1' => $request->tiersiptc1,
'tiers_iptc2' => $request->tiersiptc2,
'tiers_iptc3' => $request->tiersiptc3]);
// Afficher
return Redirect('tiers');

The contents of the blade file:
#extends('layouts.app')
#section('style')
<link href="{{ asset('css/grid.css') }}" rel="stylesheet">
<style>
body {
padding-top: 50px;
}
.navbar-template {
padding: 40px 15px;
}
</style>
#endsection
#section('bardemenu')
#include('menu')
#endsection
#section('contenu')
#if ( $Action['Action'] == "Ajouter" )
<h4> Ajouter un tiers (artiste, photographe, club...)</h4>
#if ($errors->any())
<ul>{!! implode('', $errors->all('<li style="color:red">:message</li>')) !!}</ul>
#endif
<form method="POST" action="{!! url('tiersupdatesql') !!}" accept-charset="UTF-8">
<label for="Nom">Nom : </label><input name="tiersNom" type="text" id="tiersNom" maxlength="50" size="50"><br>
<label for="Prenom">Prénom : </label><input name="tiersPrenom" type="text" id="tiersPrenom" maxlength="50" size="50"><br>
<label for="Production">Société de production : </label><input type="radio" name="production" value="0" checked> Non (par défaut) <input type="radio" name="production" value="1"> Oui<br>
#if ( $boiteprod->count()==0 )
<label for="Production">Pas de boite de production dans la base.</label>
#else
<label for="Production">Manager par la boite de production : </label>
<select name="production" id="production">
<option value="">--- choisir une boite de production ---</option>
#foreach ($boiteprod as $bprod)
<option value="{{ $bprod->tiers_id }}">{{ $bprod->tiers_nom }} {{ $bprod->ville_name }} ({{ $bprod->zip_code }})</option>
#endforeach
</select>
#endif
<br>
<label for="ville"> Ville : </label>
<select name="ville" id="ville">
<option value="">--- choisir une ville ---</option>
#foreach ($villes as $ville)
<option value="{{ $ville->ville_id }}">{{ $ville->ville_name }} ({{ $ville->zip_code }}), {{ $ville->namedept }}-{{ $ville->nom_fr_fr }}</option>
#endforeach
</select><br>
<label for="tiersIPTC1">Thème : </label>
<select name="tiersiptc1" id="tiersiptc1">
<option value="0">Choisissez un thème principal</option>
#foreach ($iptc1s as $iptc1)
#if( !empty($MetadataSeance[0]->metadata_Id) )
<option value="{{ $iptc1->IPTC1_Id }}" {{ ($iptc1->IPTC1_NomFR==$MetadataSeance[0]->comadobecategory) ? "selected=selected" : '' }}>{{ $iptc1->IPTC1_NomFR }}</option>
#else
<option value="{{ $iptc1->IPTC1_Id }}">{{ $iptc1->IPTC1_NomFR }}</option>
#endif
#endforeach
</select><label for="tiersIPTC2">, </label>
<select name="tiersiptc2" id="tiersiptc2">
<option value="0">---</option>
#foreach ($iptc2s as $iptc2)
#if( !empty($MetadataSeance[0]->metadata_Id) )
<option value="{{ $iptc2->IPTC2_Id }}" CLASS="{{ $iptc2->IPTC2_IPTC1 }}" {{ ($iptc2->IPTC2_Numero==$MetadataSeance[0]->comadobeiptcSubjectCode ) ? "selected=selected" : '' }}>{{ $iptc2->IPTC2_NomFR }} ({{ $iptc2->IPTC2_Numero }})</option>
#else
<option value="{{ $iptc2->IPTC2_Id }}" CLASS="{{ $iptc2->IPTC2_IPTC1 }}">{{ $iptc2->IPTC2_NomFR }} ({{ $iptc2->IPTC2_Numero }})</option>
#endif
#endforeach
</select><label for="tiersIPTC3">, </label>
<select name="tiersiptc3" id="tiersiptc3">
<option value="">---</option>
#foreach ($iptc3s as $iptc3)
<option value="{{ $iptc3->IPTC3_Id }}" CLASS="{{ $iptc3->IPTC3_IPTC2 }}">{{ $iptc3->IPTC3_NomFR }}</option>
#endforeach
</select>
<br>
{!! csrf_field() !!}
<input type="hidden" name="action" id="action" value="{{ $Action['Action'] }}">
<input type="submit" value="Ajouter">
Annuler
</form>
#else
<h4> Mettre à jour un tiers (artiste, photographe, club...)</h4>
#if ($errors->any())
<ul>{!! implode('', $errors->all('<li style="color:red">:message</li>')) !!}</ul>
#endif
<form method="POST" action="{!! url('tiersupdatesql') !!}" accept-charset="UTF-8">
<label for="Nom">Nom : </label><input name="tiersNom" type="text" id="tiersNom" maxlength="50" size="50" value="{{ $letiers[0]->tiers_nom }}"><br>
<label for="Prenom">Prénom : </label><input name="tiersPrenom" type="text" id="tiersPrenom" maxlength="50" size="50" value="{{ $letiers[0]->tiers_prenom }}"><br>
<label for="Production">Société de production : </label><input type="radio" name="production" value="0" {{ ($letiers[0]->tiers_societeproduction==0 ) ? "checked" : '' }}> Non <input type="radio" name="production" value="1" {{ ($letiers[0]->tiers_societeproduction==1 ) ? "checked" : '' }}> Oui<br>
#if ( $boiteprod->count()==0 )
<label for="Production">Pas de boite de production dans la base.</label>
#else
<label for="Production">Manager par la boite de production : </label>
<select name="production" id="production">
<option value="">--- choisir une boite de production ---</option>
#foreach ($boiteprod as $bprod)
<option value="{{ $bprod->tiers_id }}" {{ ($bprod->tiers_id==$letiers[0]->tiers_societeproduction ) ? "selected=selected" : '' }}>{{ $bprod->tiers_nom }} {{ $bprod->ville_name }} ({{ $bprod->zip_code }})</option>
#endforeach
</select>
#endif
<br>
<label for="ville"> Ville : </label>
<select name="ville" id="ville">
<option value="">--- choisir une ville ---</option>
#foreach ($villes as $ville)
<option value="{{ $ville->ville_id }}" {{ ($ville->ville_id==$letiers[0]->tiers_ville ) ? "selected=selected" : '' }}>{{ $ville->ville_name }} ({{ $ville->zip_code }}), {{ $ville->namedept }}-{{ $ville->nom_fr_fr }}</option>
#endforeach
</select><br>
<label for="tiersIPTC1">Thème : </label>
<select name="tiersiptc1" id="tiersiptc1">
<option value="0">Choisissez un thème principal</option>
#foreach ($iptc1s as $iptc1)
#if( !empty($letiers[0]->tiers_id) )
<option value="{{ $iptc1->IPTC1_Id }}" {{ ($iptc1->IPTC1_Id==$letiers[0]->tiers_iptc1) ? "selected=selected" : '' }}>{{ $iptc1->IPTC1_NomFR }}</option>
#else
<option value="{{ $iptc1->IPTC1_Id }}">{{ $iptc1->IPTC1_NomFR }}</option>
#endif
#endforeach
</select><label for="tiersIPTC2">, </label>
<select name="tiersiptc2" id="tiersiptc2">
<option value="0">---</option>
#foreach ($iptc2s as $iptc2)
#if( !empty($letiers[0]->tiers_id) )
<option value="{{ $iptc2->IPTC2_Id }}" CLASS="{{ $iptc2->IPTC2_IPTC1 }}" {{ ($iptc2->IPTC2_Id==$letiers[0]->tiers_iptc2 ) ? "selected=selected" : '' }}>{{ $iptc2->IPTC2_NomFR }} ({{ $iptc2->IPTC2_Numero }})</option>
#else
<option value="{{ $iptc2->IPTC2_Id }}" CLASS="{{ $iptc2->IPTC2_IPTC1 }}">{{ $iptc2->IPTC2_NomFR }} ({{ $iptc2->IPTC2_Numero }})</option>
#endif
#endforeach
</select><label for="tiersIPTC3">, </label>
<select name="tiersiptc3" id="tiersiptc3">
<option value="">---</option>
#foreach ($iptc3s as $iptc3)
#if( !empty($letiers[0]->tiers_id) )
<option value="{{ $iptc3->IPTC3_Id }}" CLASS="{{ $iptc3->IPTC3_IPTC2 }}"{{ ($iptc3->IPTC3_Id==$letiers[0]->tiers_iptc3 ) ? "selected=selected" : '' }}>{{ $iptc3->IPTC3_NomFR }}</option>
#else
<option value="{{ $iptc3->IPTC3_Id }}" CLASS="{{ $iptc3->IPTC3_IPTC2 }}">{{ $iptc3->IPTC3_NomFR }}</option>
#endif
#endforeach
</select>
<br>
{!! csrf_field() !!}
<input name="tiersModif" type="hidden" id="tiersModif" value="{{ $letiers[0]->tiers_id }}">
<input type="submit" value="Modifier">
Annuler
</form>
#endif
#endsection
#section('scripts')
<script src="{{ asset('js/app.js') }}"></script>
<script src="//code.jquery.com/jquery-1.11.3.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-chained/1.0.1/jquery.chained.js"></script>
<script>
$(function() {
$('#tiersiptc2').chained('#tiersiptc1');
$('#tiersiptc3').chained('#tiersiptc2');
});
</script>
#endsection

Related

How get old value on select in laravel blade?

i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
i add my function controller :
public function search_filter_alkes(Request $request)
{
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$user_id = $request->user_id;
$alat = Alat::with('users')->where('user_id',$user_id)
->where('jenis', 'Alkes')->
get();
session()->put('user_id',$user_id);
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
->with('user_id', $user_id)
;
}
as per your code use this and make sure you use withInput()
return redirect()->back()->withErrors($validator)->withInput();
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
EDITED
When you use redirect() after post method you have to use withInput() as i mention above example
return redirect('route')->withInput();
when you use view() you have to pass data like as array like your code then use same variable name
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>
You wil have to compare old value with input key. So replace the below code inside your for loop
#if (Input::old('user_id') == $id)
<option value="{{ $id }}" selected>{{ $nama_unit }}</option>
#else
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endif
You have multiple options:
<select name="tags[]" class="form-control select-tag" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
#endforeach
</select>
OR
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
</select>
This worked for me in laravel 8:
<select class="form-control" id="gender" name="gender" required>
<option value="">Select gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
<option value="O" #if (old('gender') == "O") {{ 'selected' }} #endif>Other</option>
</select>

How can I set the default value Laravel <select> element?

I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.
I have a category_product table.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
ProductController.php
public function edit(Product $product)
{
$categories = Category::all();
return view('Admin.products.edit', compact('product', 'categories'));
}
Product.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
I get this error.
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
You need to add default value in the select tag above foreach() loop.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
<option value="xyz">xyz</option> /* set default option value */
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
If you want set category name selected then you need to match category in product collection. ( if you have catetory_id in product table )
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->catetory_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>

How can i Get value selected from database to edit

Let's say that I select a field the record is added to my database and my form, but when I want to change this field that I selected by another the first field is always by default, it does not return the selected field
Here was my code of create
<select name="father_id" class="form-control" required>
<option value="">--selectionner le parent svp --</option>
#foreach($fathers as $father)
<option value="{{ $eleve->father_id }}" >{{ $father->nom }}
{{ $father->prenom }}</option>
#endforeach
</select>
this is my edit form i tried this code
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}"#if(old($father->father_id) ==
"father_id") selected #endif >{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>
my controller
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','Cet éléve a été modifié avec succés');
return redirect()->back();
}
the route
Route::resource('eleves','EleveController');
please help I am really stuck in this error
try it.
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}" {{ old('father_id', '') === $father->id ? 'selected' : '' }}>
{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>

Save options radios or checkbox

I want to save radio from form in database but I get "on" in the database, I don't know why.
I tried to use HTML form with labels, radios and checkbox but I see in the database saving "on".
Controller :
public function store(Request $request, Survey $survey)
{
// remove the token
$arr = $request->except('_token');
foreach ($arr as $key => $value) {
$newAnswer = new Answer();
if (! is_array( $value )) {
$newValue = $value['answer'];
} else {
$newValue = json_encode($value['answer']);
}
$newAnswer->answer = $newValue;
$newAnswer->question_id = $key;
$newAnswer->user_id = Auth::id();
$newAnswer->survey_id = $survey->id;
$newAnswer->save();
$answerArray[] = $newAnswer;
};
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
view :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="input-field col s12">
<input id="answer" type="text" name="{{ $question->id }}[answer]">
<label for="answer">Answer</label>
</div>
#elseif($question->question_type === 'textarea')
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" />
<label for="{{ $key }}">{{ $value }}</label>
</p>
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" />
<label for="something{{$key}}">{{ $value }}</label>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endforelse
{{ Form::submit('Submit Survey', array('class'=>'btn waves-effect waves-light')) }}
{!! Form::close() !!}
You forgot to give a value attribute to your checkbox and radio inputs, for example something like this:
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" value="{{ $key}}"/>
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" value="{{ $key}}"/>

How to use isset in select option in laravel form

How to use isset if i use select option?
Please help.
This is how i use to get my data for normal insert.
<div class="form-group">
<label>Zone Name</label>
<input type="text" name="name" class="form-control" value="{{ old('name',isset($zone) ? $zone->name : '') }}" placeholder="Name">
</div>
But i don't know how to use isset in select option.
<div class="form-group">
<label>Select Country</label>
<select class="form-control" name="country_id">
<option value="" selected>Please Select</option>
#if ($country->count())
#foreach($country as $c)
<option value="{{ $c->country_id }}" {{ $selectCountry == $c->country_id ? : '' }}>{{ $c->name }}</option>
#endforeach
#endif
</select>
</div>
This is my Controller
public function edit(Request $request, $id){
$zone = Zone::find($id);
$country = Country::all();
$selectCountry= Country::first()->country_id;
if(!$zone){
return redirect('/');
}
return view('zone.edit',['zone' => $zone, 'country'=>$country, 'selectCountry'=>$selectCountry]);
}
my expected result is when i press on edit button, it will show the previous selected result.
You can write
<select class="form-control" name="country_id">
<option value="" {{ !isset($selectCountry) ? 'selected' : '' }}>Please Select</option>
#if ($country->count())
#foreach($country as $c)
<option value="{{ $c->country_id }}" {{ (isset($selectCountry) && $selectCountry == $c->country_id) ? 'selected' : '' }}>{{ $c->name }}</option>
#endforeach
#endif
</select>

Resources