laravel8 checkbox deleting last inserted options on view - laravel

good morning, I come accross the following problem, i have some checkboxes, they store correctly and appear on the phpmy admin, but the prevous submited forms on the view page only show the last inserted, if someone know how to fix it tell me please
i have this code:
view blade file
<td>
#foreach($item->motivo_deslocacao as $motivo)
{{$motivo}},
#endforeach
</td>
<td>
#foreach($item->servico_afetacao as $servico)
{{$servico}},
#endforeach
</td>
create.blade file
<input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motiv">motiv
<input type="checkbox" class="form-check-input" name="motivo_deslocacao[]" value="motivdois">motivdois
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="serve">serve
<input type="checkbox" class="form-check-input" name="servico_afetacao[]" value="servedois">servedois
model
protected $casts = [
'motivo_deslocacao' => 'array',
'servico_afetacao' => 'array',
];
controller
public function create(Request $request)
{
$motivo_deslocacao = array($request->motivo_deslocacao);
$servico_afetacao = array($request->servico_afetacao);
return view('admin.formulario.create', [
'viaturas' => Viaturas::all(),
'motivo_deslocacao' => $motivo_deslocacao,
'servico_afetacao' => $servico_afetacao,
]);
}
public function store(Request $request)
{
$data->motivo_deslocacao = $request->input('motivo_deslocacao');
$data->servico_afetacao = $request->input('servico_afetacao');
$data->save();
}

Related

Show checkbox checked from database

Hope you're all fine.
I have an article, and I want to be able to edit it. Inside the article, I have checkboxs, and what I want is to check the ones I've checked when I first create the article (Im using a pivot table).
I have this code for now.
public function createArticle(Request $request)
{
$data = $request->validate([
'titreArticle' => 'bail|required|between:5,40',
'typeArticle' => 'bail|required',
'themeCheckbox' => 'required',
'themeCheckbox.*' => 'required',
'contenuArticle' => 'bail|required',
]);
$type_articles = Type_article::findOrFail($data['typeArticle']);
$article = new Article();
$article->type_article()->associate($type_articles);
$themes = Theme::whereIn('theme_id', array_keys($data['themeCheckbox']))->get();
$article->titre = $data['titreArticle'];
$article->contenu = $data['contenuArticle'];
$article->save();
$article->theme()->attach( $themes);
return view('admin');
}
And inside my view :
#foreach ($themes as $theme)
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" name="themeCheckbox[{{$theme->theme_id}}]" value="1" >
<label class="form-check-label">{{ $theme->nom_theme }}</label>
</div>
#endforeach
I've seen I must use contains but don't really know how to use it...
Cordially

Update data in laravel 6

I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}

Update Comment in Laravel

I am new at Laravel and I want to use if statement inside Laravel Controller.
So I have a form tag where I can add comments. I want to edit comments in the same form.
Here is my form tag:
<form method="post" action="/hotels/{{$id}}" name="review_hotel">
#csrf
<div class="form-group">
<textarea name="comment" id="comment" class="form-control" style="height:100px"
placeholder="Write your review"></textarea>
#error('comment')
<p class="text-danger">{{$message}}</p>
#enderror
</div>
<input type="submit" value="Submit" class="btn_1" id="submit-review">
</form>
I have a script, with the help of this script I can add a comment that I want to change to the textarea.
After this I have no idea how to update it in my controller.
This is CommentController:
public function comment(CommentRequest $request, $id)
{
$comment = new Comment();
$comment->object_id = $id;
$comment->user_id = Auth::id();
$comment->comment = $request['comment'];
$comment->save();
return redirect('/hotels/'.$id);
}
I think I need to write "if" statement in my controller, but I don't know how.
maybe there is a simpler way to update comment?
On your model Comment.php mark form variables as fillable
public $fillable = ['object_id', 'user_id', 'comment'];
then use this Model function (Eloquent)
Comment::updateOrCreate(
[
'object_id' => ...,
'user_id' => ...,
],
[
'object_id' => ...,
'user_id' => ...,
'comment' => ...,
]
);
https://laravel.com/docs/5.8/eloquent#other-creation-methods

How to validate 2 times

I can sucess First page's Laravel validation. After that page I would like to put another data input and I need to validate the data. Here is my code. When I click submit buttom there I backed First page. Could you teach me what is wrong my code?
// This page's validation is OK.
public function confirm(Request $request)
{
$rules = [
'orderer' => 'required'
];
$this->validate($request, $rules);
$data = $request->all();
$request->session()->put('data',$data);
return view('front.confirm', compact("data"));
}
// problem is here
public function complete(Request $request)
{
$rules = [
'num' => "required|in:1,2,3,4,5,6,7,8,9,10",
];
$this->validate($request, $rules);
$data = $request->session()->pull('data');
$token = array_shift($data);
$data = array_merge($data, $request->all());
$Contact = Contact::create($data);
}
UPDATES 2
web.php
Route::post('mail/complete','MailController#complete');
complete.blade.php
<form class="form-horizontal" role="form" method="post" action="http://test.ivory.ne.jp/order/public/mail/complete">
<div class="form-group">
<select class="form-control" name="p1q" value="{{ old('p1q') }}">
<option value="-" selected>select </option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
</select>
  </div>
<button class="btn btn-lg btn-primary btn-block" type="submit">送信</button>
</form>
Thanks for updating the code. So it seems that your are trying to validate a field num which is not present on your $request object, since the name attribute of your HTML <select> is set to p1q.
So in order to make your validation function work, you need to either set
$rules = [
'p1q' => "required|in:1,2,3,4,5,6,7,8,9,10", //mind the changed field name change from "num" to "p1q"
];
or adapt your HTML markup to <select name="num" ... >.

Laravel multiple delete checkbox?

I want to delete only checked tasks. At the moment I have this:
<form method="POST" action="/destroy">
#foreach($tasks as $t)
<label>
<input type="checkbox" name="checked[]" value="$t->id">
</label>
#endforeach
<button type="submit">Submit!</button>
</form>
This is my Controller
public function destroy(Request $request)
{
$this->validate($request, [
'checked' => 'required',
]);
$checked = $request->input('checked');
Task::destroy($checked);
}
And this is my route
Route::post('/destroy', [
'uses' => 'Controller#destroy',
]);
I don't get no error but the system does not work
I fixed the problem! Thanks for your support!
The problem was that my id variable was a hash.

Resources