Controller article
public function update(Request $request, Article $article){
$article->update($request->except('slug', 'image_path'));
if ($request->hasFile('image_path')) {
$image = $request->file('image_path');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$article->image_path = $new_name;
$article->save();
}
$article->categories()->detach();
if ($request->has('categories')) {
$article->categories()->attach($request->input('categories'));
}
$user=auth()->user();
return redirect()->back()->with('message', 'Raksts atjaunots!');
}
public function edit(Article $article){
$user=auth()->user();
return view('edit',[
'article' => $article,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
edit blade
<form class="form-horizontal" action="{{route('update', $article)}}" method="post" enctype="multipart/form-data" style="display: flex;justify-content: center;flex-direction: column;">
<input type="hidden" name="_method" value="put">
{{ csrf_field() }}
{{-- Form include --}}
<img src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
#include('partials.form')
<input type="hidden" name="modified_by" value="{{Auth::id()}}">
</form>
link to the form
<tbody>
#foreach ($articles_suggest_user as $article)
<a class="btn btn-default" href="{{route('edit', $article)}}"><i class="fa fa-edit"></i></a>
<?php } ?>
#endforeach
</tbody>
web.php
Route::get('/home/edit/{id}','Controller_Article_parents#edit', function () {
return view('/home/edit');
})->name('edit');
Route::get('/home/update/','Controller_Article_parents#update', function () {
return view('/home');
})->name('update');
When i clicked the link, i move to for example this url http://127.0.0.1:8000/home/edit/190
But my form is empty... input empty. How I can do when I open form it's display me input information ?
When click my link its display me form, but form is empty.
form example
<div class="rtq">
<label for="parent_id">Cat</label>
<span class="required">*</span>
<select
id="parent_id"
class="form-control"
name="categories[]"
multiple=""
required
>
#include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
</div>
<div class="rtq">
<label for="">Descript</label>
<textarea
name="description_short"
class="form-control"
id="description_short"
>
{{ $article->description_short ?? "" }}
</textarea>
</div>
It my form . Mini example
If you want to redirect with form input, you can use the withInput() function
return back()->withInput();
https://laravel.com/docs/7.x/redirects#creating-redirects
Now, this function is designed to be used with form validation and may not be what you want.
What I have done in the past when combining create & edit views into a single template is something like this:
{!! Form::text('name', isset($model) ? $model->name : null, [
'class' => 'form-control',
'placeholder' => 'Please fill out this field &hellips;',
'required' => true
]) !!}
This uses the Laravel Collective HTML library. However the principle is the same if you are using raw hmtl like so:
<input type="text" value="{{ isset($model) ? $model->name : null }}">
Round 2 Electric Boogaloo
You aren't returning a variable called $article to your edit.blade.php.
Round 3 Apple Tree
Originally, you appeared to be calling both a controller action AND also a callback function. Stick to the controller action only like so:
Route::get('/home/edit/{id}','Controller_Article_parents#edit')->name('edit');
Then within your edit() function on the Controller you will want to do this:
public function edit ($id) {
return view('/home/edit', [
'article' => Article::find($id)
]);
}
Related
I have a words table and I want to keep a search form in my index view to search intended word.
This is function that I have wrote for search in controller.
public function search(string $id){
if($id!="")
{
$terms = Terms::query()
->where('term', $id)
->get();
if(count($terms)==0)
{
$terms = Terms::query()
->where('term', 'LIKE', "{$id}%")
->orWhere('term', 'LIKE', "%{$id}%")
->get();
}
if(count($terms)==0)
{
$terms = Terms::query()
->orWhere('term', 'LIKE', "%{$id}%")
->get();
}
return view('admin/terms/search')->with('terms', $terms);
}
}
and This is my form code in view
{!! Form::open(['action' => 'App\Http\Controllers\TermsController#search', 'method' => 'GET']) !!}
<form class="bd-forms">
<div class="form-group">
{{ Form::label('word', 'Search word') }}
{{ Form::text('word', '', ['class' => 'form-control', 'placeholder' => 'type word here']) }}
#error('word')
<div class=""
style="padding-right:5px;margin-top:5px;margin-bottom:10px; color:red;font-size:12px;">
the field is empty.
</div>
#enderror
</div>
<div>
{{ Form::submit('Search', ['class' => 'btn btn-primary']) }}
</div>
</form>
{!! Form::close() !!}
Now I want to know how can I send text value as $id parameter to search function and should method be 'GET' or ' POST'?
You can Use Get method for filter like operation you just have to send request
public function search(Request $r){
$filter=$r->query('id');
if(!empty($filter))
{
$terms = Terms::query()
->where('term', 'like', '%'.$filter.'%')
->get();
$terms->appends($request->all());
}
else
{
$terms = Terms::get();
}
return view('admin/terms/search')->with('terms', $terms);
}
In blade file you have to do this for filtering purpose
<form class="form-inline justify-content-center" method="GET">
<input type="text" class="form-control mr-2 text-center" name="id" placeholder="term" value="{{$id}}">
<button type="submit" >Search</button>
</form>
I have the following controller, which sends data to a Laravel Blade view:
Controller:
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
In that Laravel blade view there is a form:
<form method="GET" action="{{ route('invoices.choose-periods') }}">
<div class="form-group {{ $errors->has('school') ? 'has-error' : '' }}">
<label>School</label>
<select id="school" class="form-control" name="school[]" multiple size="{{ $schools->count() }}" required>
#foreach ($schools as $school)
<option value="{{ $school->id }}">{{ $school->name }}</option>
#endforeach
</select>
#if ($errors->has('school'))
<span class="help-block">
<strong>{{ $errors->first('school') }}</strong>
</span>
#endif
</div>
<button type="submit" class="btn btn-success btn-sm pull-right">Submit</button>
</form>
As you can see from the HTML, the form is a multi-select form, with the resulting data stored in a school[] array.
On submission of the form, I do a test die and dump on request('school') and see that for every option I have selected, the value seems to have been logged twice. For example, choosing only one option gives me:
array:2 [▼
0 => "15"
1 => "15"
]
Any ideas? Thanks!
I have only worked on laravel 5.7. Try this It is working for me.
Since you are passing 2 objects
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
It is obvious you will get 2 errors.
In your controller change this
public function create()
{
$schools = School::all()->sortBy('school_type');
return view('invoices.create')->with([
'schools' => $schools,
'dayTypes' => $dayTypes,
]);
}
to this
public function create(){
$schools = School::all()->sortBy('school_type');
return view('invoices.create', ['schools' => $schools]),
]);
}
My view blade is like this :
#foreach($reviews as $key => $review)
...
<div class="form-group">
{!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform']) !!}
<label for="review" class="sr-only">Review</label>
{!! Form::textarea('review', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}
#if ($errors->has('review'))
<span class="help-block">
<strong>{{ $errors->first('review') }}</strong>
</span>
#endif
{!! Form::hidden('id', $review->_id) !!}
{!! Form::hidden('store', $review->store_id) !!}
{!! Form::close() !!}
</div>
...
#endforeach
My routes is like this :
Route::group(['prefix' => 'message','as'=>'message.'],function(){
Route::post('review/update', ['as'=>'review.update','uses'=>'ReviewController#update']);
});
My controller to update is like this :
public function update(CreateReviewRequest $request)
{
$param = $request->only('id', 'review', 'store');
...
}
Before update, it will call CreateReviewRequest to validation
My CreateReviewRequest is like this :
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateReviewRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'review'=>'required|max:300'
];
}
}
When, only one data, I input comment and submit, it works
But, when more than one data, it does not works
There exist error like this :
An invalid form control with name='review' is not focusable.
How can I solve it?
You made some mistakes here... In HTML IDs MUST be unique. You can't put "review" as id for all your textareas... My comment was about to use an array as name like this name=review[], and in the ID add dynamically the $key at the end of the id to make them unique.
This will give you something like this :
<textarea name="review[]" class="form-control" id="review0" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review1" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review2" rows="3" required></textarea>
EDIT
I've just founded this stackoverflow topic. Could you try to add novalidate attribute in the <form>?
<form name="myform" novalidate>
Something like this in your case :
{!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform', 'novalidate']) !!}
VIEW
{!! Form::textarea('review[]', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}
or
<textarea name="review[]" class="form-control" id="review" rows="3" required></textarea>
You must insert name field like array empty, when you will submit the form you can get all values in your controller calling:
CONTROLLER
public function update(Request $request)
{
// Validate the form
$this->validate($request, [
'review' => 'required',
]);
// get inputs
$review_input = $request->get('review');
dd($review_input); // see if it work
}
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
I have a problem, I want to use the form to do a search and use pagination. The problem is when I change the page, an error occurs:
No query results for model [Aluno].
My controller
Route::post(
'alunos/busca',
array(
'as' => 'alunos.busca',
'uses' => 'AlunosController#busca'
)
);
My Controller
public function busca()
{
$search = Input::get('q');
$alunos = Aluno::where('curso', $search)->paginate(1);
return View::make('alunos.index', compact('alunos', 'search'));
}
My view:
<form method="post" action="{{ URL::to('alunos/busca') }}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
<div class="controls">
<input type="submit" class="btn" id="submit" value="Submit" />
</div>
</div>
<div class="divPagination">
{{ $alunos->links() }}
</div>
I need help..
Sorry my English
I think the best solution is the next one:
Copy and paste this form wherever you want it:
{{Form::open(array('url' => 'alunos/busca','method' => 'get'))}}
{{Form::text('q', null , array('placeholder' => 'Search...','class' => 'input-xxlarge'))}}
{{Form::submit('SEARCH',array('class' => 'btn','id' => 'submit'))}}
{{Form::close()}}
In routes.php :
Route::get('alunos/busca', function() {
$search = Input::get('q');
$alunos = Aluno::where('curso', 'LIKE', '%'.$search.'%')->paginate(15);
return View::make('alunos.index')
->with('alunos', $alunos);
});
Now, in your view app/views/alunos/index.blade.php :
#if(!$alunos->isEmpty())
#foreach($alunos as $aluno)
{{$aluno->subject}}
#endforeach
{{$alunos->links()}}
#else
<h1> No results </h1>
#endif
Change it as you wish and enjoy
The problem is when you click on the next page it has no memory of the search param you just entered, essentially $q = null; which will cause the error.