This is my route :
Route::post('/store', 'EditlinkController#store');
My controller
public function index()
{
$id = $_GET['id'];
$links = DB::table('slugs')->where('slugs.id',$id)
->join('links','links.sid','=','slugs.id')
->get();
return view('editlink', ['links' => $links]);
}
public function store(Request $request, $id)
{
$url = $request->input('url');
$data =new link;
$sid = $id;
$data->sid = $sid;
$data ->url = $url;
$data ->save();
return redirect('/links');
}
And my view:
<form role="form" method='POST' action="{{url('store')}}">
<div class="entry input-group col-xs-3">
<input class="form-control" name="url" type='url' placeholder="https://..." size="100"/>
<input type="hidden" name="_Token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary" type="button">
<span class="material-icons md-12">add</span>
</button>
{{ csrf_field() }}
</div>
</form>
So basically here I want to call $id from index to store. I also have tried
Route::post('/store/{id}', 'EditlinkController#store');
But still doesn't work. Thank you.
Your route, /store/{id}, requires you to have a route parameter. Make sure you have an $id available to you before you generate your url for your form.
An example of what your open form tag should look like with the $id included:
<form role="form" method='POST' action="{{url('store', $id)}}">
I'm assuming the view editlink is where the markup for the form resides. If so, then you can simply pass the value of the id to your view from your controller:
return view('editlink', ['links' => $links, 'id' => $id]);
Related
I have several buttons to insert data into the database, but only one command works, while the others don't work, what should I do, I will give an example of the data I created
this is my controller :
public function store(Request $request)
{
//
$days = $request->hari;
$hours = $request->jam;
$minutes = $request->menit;
$model = new Q_Topic_User;
$model->status = $request->status;
$model->topic_id = $request->topic_id;
$model->timer = Carbon::now()->addDays($days)->addHours($hours)->addMinute($minutes);
$topik = $request->topic_id;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeAns(Request $request)
{
$modellama = Q_Answer::where('user_create', '=', $request->kode_user)
->Where('topic_id', '=', $request->topic_id)
->Where('question_id', '=', $request->question_id);
$a = $request->user_answer;
$b = $request->answer;
if ($a === $b){
$nilai = "1";
}
else{
$nilai = "0";
}
if ($modellama >= "1"){
$modellama->delete();
}
// dd($request->all());
$model = new Q_Answer;
$model->topic_id = $request->topic_id;
$model->question_id = $request->question_id;
$model->user_answer = $request->user_answer;
$model->answer = $request->answer;
$model->hasil = $nilai;
$model->user_create = auth()->id();
$topik = $request->topic_id;
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeTot(Request $request)
{
$tot_nilai = Q_Answer::select(DB::raw("CAST(SUM(hasil) as int) as tot_nilai"))
->Where('topic_id', '=', $request->topic_idi)
->Where('user_create', '=', auth()->id())
->pluck('tot_nilai');
// dd($tot_nilai);
$tot_soal = Q_Question::select(DB::raw("CAST(COUNT(topic_id) as int) as tot_soal"))
->Where('topic_id', '=', $request->topic_idi)
->pluck('tot_soal');
dd($request->all());
$model = new Q_Answer_Tot;
$model->topic_id = $request->topic_idi;
$model->totalsoal = $tot_soal;
$model->totalnilai = $tot_nilai;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
$topik = $request->topic_id;
return redirect('qanswers/'. $topik);
}
blade :
<form method="POST" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success">Kirim data</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeTot') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $t_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Peserta</button>
#endforeach
</form>
my web.php :
Route::resource('/qanswers', Q_AnswersController::class);
Route::post('/storeAns',[Q_AnswersController::class, 'storeAns'])->name('storeAns');
Route::post('/storeTot',[Q_AnswersController::class, 'storeTot'])->name('storeTot');
i can save data in store but i can save data using storeAns and storeTot. I dont find error in laravel log but i can see my data using dd($request->all()); can u help me?
Its good practice to give each form an unique id. With multiple forms and buttons, I would specify the form for the button. And you need to use value to figure out which button was pressed, especially when you are going to the same url in more than one form.
<form method="POST" id="answers" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success" value="form1">Kirim data</button>
#endforeach
</form>
<form method="POST" id="storeans1" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form2">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" id="storeans2" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form3">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
I have shown the value with formx. SInce you are creating many buttons for each form, each should have an unique value such as
form1btn1
form1btn2
form1btn3
#extends('layouts.admin')
#section('content')
#foreach($galerys as $data)
<form method="post" enctype="multipart/form-data" action="{{url('/ubahGaleryPost')}}">
{{ csrf_field() }}
<h4>Ubah Gambar</h4>
<input name="id" id="id" type="hidden" value="{{$data->id}}" >
<h5>Gambar</h5>
<input name="gambar" id="gambar" type="file">
<h5>Deskripsi</h5>
<input name="deskripsi" id="deskripsi" class="form-control" type="text" value="{{$data->deskripsi}}">
<button type="submit" class="btn btn-primary">Ubah</button>
</form>
#endforeach
#endsection
controller
public function ubahGalery($id){
$data = Galery::findOrFail($id);
return view('admin.updategalery',['galerys'=>$data]);
}
public function ubahGaleryPost(Request $request){
$data = Galery::where('id',$request->id)->first();
if (empty($request->file('gambar'))){
$data->gambar = $data->gambar;
}
else{
unlink('img/galery/'.$data->gambar);
$file = $request->file('gambar');
$ext = $file->getClientOriginalExtension();
$newName = rand(100000,1001238912).".".$ext;
$file->move('img/galery',$newName);
$data->gambar= $newName;
}
$data->deskripsi = $request->deskripsi;
$data->update();
return redirect('/galeryAdmin');
}
$data = Galery::findOrFail($id); this will return Galery object instead of a Collection of Galery (except if $id is an array).
So in your HTML you don't need a foreach and instead could just do $data->id, $data->deskripsi etc.
You don't need to loop on galerys as you are fetching only one result so you don't need to loop on your result so instead of that just fetch data directly.
Like this,
$galerys->deskripsi
Hope this helps:)
I'm learning Laravel from Lacast and i'm trying to create a CRUD application.I've implemented the index,show,create and store correctly,but with the edit form when i try to submit data it's throwing BadMethodCallException.
Here are my routes
Route::get('/projects','ProjectsController#index');
Route::get('/projects/{id}','ProjectsController#show')->where('id','[0-9]+');
Route::get('/create','ProjectsController#create');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/{id}/edit','ProjectsController#edit')->where('id','[0-9]+');
Route::put('/projects/{id}','ProjectsController#update')->where('id','[0-9]+');
Route::delete('/projects/{id}','ProjectsController#destroy');
Here is the edit form:
#extends('template');
#section('content')
<h2>Create new project</h2>
<p>/projects/{{ $project->id }}</p>
<form method="POST" action="/projects/{{ $project->id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div>
<input value="{{ $project->title }}" type="text" name="title" id="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Enter the project description">{{ $project->description }}</textarea>
</div>
<div>
<button type="submit">Update project</button>
</div>
</form>
#endsection
The controller code:
public function edit($id){
$project= Project::find($id);
return view('projects.edit',compact('project'));
}
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description');
$project->save();
return redirect('/projects');
}
The edit form displays as expected with data coming from the database,after submit i get the following error page:
With the example of the instructor the code has worked perfectly,and before using the PUT either on my form and in my controller i used PATCH like the instructor but always the same result.
Here is the instructor video link: Faking PATCH and DELETE Requests
The error is pretty straightforward.
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description'); // You don't have a method description()
$project->save();
return redirect('/projects');
}
This is what you probably meant to do:
public function update($id)
{
$project= Project::find($id);
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
I have a create method in my controller
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main]);
}
This is my photos.blade file
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form><form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value= " Descending" class="settings-photos-header2 text-center"/>
</form>
<h2 class="settings-photos-header2 text-center">Photo Gallery</h2>
#foreach ($image_array as $images)
<div class="image-warp"><img src="{{$images->filename}}"
style="width:100px;height:100px;"><br/><span style="color: #1b1e21">{{$images->description}}</span>
</form>
</div>
#endforeach
Question- How can i make the asc button sort the images in ascending order and des in descending order, is there a way to connect it to my controller, or is there a way to sort them in ascending and descending order through any other means?
There are many ways you can achieve the sorting issue but as you have setup your page let me tell you the way you can sort records. Create another action which point to your settings.photos route
public function settings_photos($property_id) {
$form = Input::get('submit');
$orderBy = $form == "Ascending" ? "asc" : "desc";
# Fetch Images in of Specific Property
$list_of_images = Image::where('property_id', $property_id)
# Order by Asc/Desc
->sortBy('id', $orderBy)->get();
return view('settings.photos', ['image_array' => $list_of_images]); }
Now add Image->id to pass in your controller action by form
https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form>
<form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Descending" class="settings-photos-header2 text-center"/>
</form>
Now make some changes to your previous Controller code
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main, 'id' => $id]);
}
As I told you there are various way to sort records even by Ajax but I suggest you using your code example. I also did not optimize the code.
Let me know if you have any question.
Thanks
Im new in Laravel and i dont understand how to make action forms and routing for editing post
Here is my routes --
Route::post('/menu', 'MenuController#store');
Route::resource('/menu', 'MenuController');
here is controller --
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
return $id;
// $menus->name = $request->input('menuName');
//$menus->link = $request->input('menuLink');
//$menus->save();
//return redirect('/menu')->with('success', 'Menu updated');
}
Return $id is for check what "id" will give me and he gives me this- "{id}"
here is form --
<form action = "/menu/{id}" method = "POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
i am really messed up allready and dont know what i am doing wrong. I cant understand why update funtion returns me "{id}" not value of ID and how can i make this all works
Your are not passing id in your view , you are passing it as a string , u need it create a variable and set it to the right id and pass it to your form.
Change your form action from
action = "/menu/{id}"
to
action = "/menu/".$id"
or you can use laravel blade
Form::open(['route' => ['menu.update', $id]])
and don't forget to close the form at the end
{!!Form::close!!}
You did something wrong with the action attribute. Change it to:
action="/menu/{{ $id }}"
Okay,
you have an edit method which will show the form, that method should return the menu object
public function update($id)
{
// get the menu you wan to edit
$menu = Menu::find($id);
// return the form with the menu object
return view('your.form.view', compact('menu'));
}
Now the form should be like this
// use that object u returned in the form action
<form action = "/menu/{{ $menu->id }}" method = "POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
Now the update method should be something like this
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
// update the data
$bool = Menu::where('id',$id)->update([
'menuName'=> $request->menuName,
'menuLink'=> $request->menuLink,
]);
if(!$bool){
Session::flash('alert','error');
return view('page.index');
}
Session::flash('alert','success');
return view('page.index');
}
This first method is "get" edit that should show the form,
the second is put update that should receive the data from the form(after the show method) and use i to update.
I resolved the error ---
in update blade befor form i added foreach
<div class="container">
<div class="row">
#foreach($menus as $menus)
<form action="/menu/{{$menus->id}}" method="POST">
#endforeach
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link ">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
</div>
</div>
But now he is editing only 1st post and no matter wich post edit im switching
here is controller --
public function edit($id)
{
$menus = Menu::all();
return view('admin.editmenu')->with('menus', $menus);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
$menus = Menu::find($id);
$menus->name = $request->input('menuName');
$menus->link = $request->input('menuLink');
$menus->save();
return redirect('/menu')->with('success', 'Menu updated');
}
menu list blade ---
#if(count($menus) > 1)
#foreach($menus as $menus)
<tr>
<th scope="row">1</th>
<td>{{$menus->name}}</td>
<td>{{$menus->link}}</td>
<td>{{$menus->created_at}}</td>
<td>{{$menus->updated_at}}</td>
<td>
<button type = "button"class = "btn btn-outline-danger btn-sm">Delete</button>
<a href = "/menu/{{$menus->id}}/edit" class = "btn btn-outline-warning btn-sm">Edit</button>
</td>
</tr>
#endforeach
#else
Ok ive got it and resolved.
I will start with routes
routes----------
Route::resource('menu', 'MenuController');
that post route was unnecessary, but that didnt affect anything
controller -------
public function edit($id)
{
$menus = Menu::find($id);
return view('admin.editmenu')->with('menus', $menus);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'menuName' => 'required',
'menuLink' => 'required'
]);
//create new menu
$menus = Menu::find($id);
$menus->name = $request->input('menuName');
$menus->link = $request->input('menuLink');
$menus->save();
return redirect('/menu')->with('success', 'Menu updated');
}
Where in edit function i must find ID witch needs to be editing
menu list -----------------
#if(count($menus) > 1)
#foreach($menus as $menu)
<tr>
<th scope="row">1</th>
<td>{{$menu->name}}</td>
<td>{{$menu->link}}</td>
<td>{{$menu->created_at}}</td>
<td>{{$menu->updated_at}}</td>
<td>
<button type = "button"class = "btn btn-outline-danger btn-sm">Delete</button>
<a href = "/menu/{{$menu->id}}/edit" class = "btn btn-outline-warning btn-sm">Edit</button>
</td>
</tr>
#endforeach
#else
<p class = "well"> No menu items created!</p>
#endif
I only changed $menus as $menus on "$menus as $menu" to be clear.
edit blade ------------
#extends('admin.main')
#section('content')
<div class="container">
<div class="row">
<form action="/menu/{{$menus->id}}" method="POST">
{{ csrf_field() }}
<input name = "_method" type = "hidden" value = "PUT">
<input type="text" id="menuName" name="menuName" class="input-block-level" placeholder="Menu name" value="{{ $menus->name }}">
<input type="text" id="linkName" name="menuLink" class="input-block-level" placeholder="Menu link " value="{{ $menus->link }}">
<button type="submit" class="btn btn-success pull-right">Submit</button>
</form>
</div>
</div>
#endsection
Here i deleted #foreach like #Snapey told and added the value option.
I guess value option gave me the errrors all the time.
Thank you all for attention and willing to help! Hope that someday i could help others with my knowledge.