I have had a lengthy search both here and the Laravel forums, but i can't find an answer to this problem. ->withInput() coughs up an Undefined offset: 0.
For Context:
Controller
public function getJobs()
{
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
$result = $query->get();
return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options))->withInput();
}
View
<form action="{{ action('JobsearchController#getJobs') }}" method="post">
<div class="row">
<div class="large-8 columns">
<input type="text" name="realm" placeholder="Keywords/Skills" />
</div>
<div class="large-4 columns">
{{ Form::select('category', $category_options , Input::old('category')) }}
</div>
</div>
<div class="row">
<div class="large-4 columns">
{{ Form::select('location', $location_options , Input::old('location')) }}
</div>
<div class="large-4 columns">
{{ Form::select('type', $position_options , Input::old('type')) }}
</div>
<div class="large-4 columns">
<input type="submit" value="Search" style="width:100%; padding-top: .5rem;
padding-bottom: .5rem;" class="button border-btn" />
</div>
</div>
</form>
Now according to the documentation there should not be an issue, and the page loads fine if the ->withInput(); is removed.
The end goal is to roll in the answer that i received from my previous question Undesired result from db:raw and have a single page that loads the "Filtering" form and displays the relevant results on the reload and remembers the selections in the form.
Thanks in advance.
UPDATE:
Following a comment i have updated the controller and routes, still same result:
routes.php
Route::get('jobs/search', 'JobsearchController#getSearch');
&
Route::post('jobs/search', 'JobsearchController#getJobs');
Controller
public function getSearch()
{
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options));
}
public function getJobs()
{
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options))->withInput();
}
withInput() doesn't work the way you think it does. It's only a function of Redirect, not View.
Calling withInput($data) on View has a completely different effect; it passes the following key value pair to your view: 'input' => $data (you get an error because you're not passing any data to the function)
To get the effect that you want, call Input::flash() before making your view, instead of calling withInput(). This should allow you to use the Input::old() function in your view to access the data.
Alternatively, you could simply pass Input::all() to your view, and use the input[] array in your view:
View::make(...)->withInput(Input::all());
which is translated to
View::make(...)->with('input', Input::all());
As for your comment, I recommend doing it like so:
$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
$category = Input::get('category');
$location = Input::get('location');
$type = Input:: get('type');
$data = compact('position_options', 'category_options', 'location_options', 'category', 'type', 'location');
return View::make('jobsearch.search', $data);
Related
I've implemented a modal type Update and Delete functions in my website but it always return Too few arguments to function App\Http\Controllers\AdminController::destroy(), 1 passed in D:\SUDRTest\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
it is also the same for the Update function as well
Here is my route for the CRUD
Route::resource('papers', AdminController::class)->only(['edit', 'update', 'destroy']);
Here is the View
<li class="pdfpaperInfo">
<div class="colpdf col-1" data-label="Title:">{{ $paper->PaperTitle }}</div>
<div class="colpdf" data-label="Paper Type:">{{ $paper->PaperType }}</div>
<div class="colpdf" data-label="College:">{{ $paper->College }}</div>
<div class="colpdf" data-label="Author(s):">{{ $paper->Authors }}</div>
<div class="colpdf" data-label="Date Published:">{{ $paper->DatePublished }}</div>
<div class="pdfbtnCont">
<button class="pdfBtn redBtn" onclick="location.href='{{route('MyProfile')}}'">Back</button>
<button class="pdfBtn redBtn" id="modalOneBtn" onclick="location.href='{{route('papers.edit', $paper->PaperID)}}'">Update</button>
<button class="pdfBtn redBtn" id="modalTwoBtn">Delete</button>
</div>
</li>
<div id="modalOne" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="m1Close close">×</span>
<div class="modalinfoCont">
<h2>Update Paper</h2>
#include('admin.updatepaper')
</div>
</div>
</div>
<div id="modalTwo" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="m2Close close">×</span>
<div class="modalTwoCont modalinfoCont">
<h2>Delete Paper</h2>
<br>
Are you sure you want to delete this paper?
<br>
<br>
<div class="modalbtnCont">
<form method="POST" action="{{route('papers.destroy', $paper->PaperID) }}">
#csrf
#method('DELETE')
<button class="redBtn" type="submit">Yes</button>
</form>
<button class="redBtn" type="submit">No</button>
</div>
</div>
</div>
</div>
</div>
and the controller
public function destroy(Papers $paper, $PaperID)
{
$paper=Papers::find($PaperID);
$paper->delete();
return redirect()->back();
}
public function edit(Papers $paper, $PaperID)
{
$paper=Papers::find($PaperID);
return view('admin.updatepaper',compact('paper'));
}
public function update(Request $request,Papers $paper, $PaperID )
{
$request->validate([
'PaperTitle' => 'required',
'PaperType' => 'required',
'file' => [
'required',
File::types('pdf')
->max(12 * 1024),
],
]);
$paper=new Papers();
$file=$request->file;
$filename=time().'.'.$file->getClientOriginalExtension();
$request->file->move('assets', $filename);
$paper->file=$filename;
$paper->DatePublished=$request->DatePublished;
$paper->PaperTitle=$request->PaperTitle;
$paper->PaperType=$request->PaperType;
$paper->Authors=$request->Authors;
$paper->update();
return redirect()->back();
}
I've tried not to do it in modal form and still it kept on displaying the same error and I don't know what is the missing parameter since it doesn't tell me
You need to take another look at route-model binding.
Laravel will by default do the Papers::find($paperID) and pass the Papers model as the Papers $papers argument to your methods.
So the destroy method should be:
public function destroy(Papers $paper)
{
$paper->delete();
return redirect()->back();
}
Of course you can disable route-model binding and do your own thing but it doesn't seem necessary here.
Its not clear what you intend to do in the update method. If you want to create a new paper on update and keep the old one then change $paper->update() to $paper->save() and you should be good. But if you want to do an actual update you should do something like this:
update(Papers $paper, Request $request) {
// validate
$paper->DatePublished=$request->DatePublished;
// update other fields
$paper->save();
return redirect()->back();
}
I am quite new to Laravel
I have two views
Book
Read
The Book View displays a single book
<section class="cont-readingone">
<div class="container">
<div class="row row-grid">
<div class="col-md-6">
<div class="row">
<div class="col-md-6">
<div class="cont-reading-image">
<img src="{{ $book->image_url }}" alt="trending image" />
</div>
</div>
<div class="col-md-6">
<div class="out-box">
<h2>{{ $book->name }}</h2>
<h3>{{ $book->author->name }}</h3>
<br>
Start Reading<br><br>
<img src="\images\cart-buy.png" width="13px"/> Buy
</div>
</div>
</div>
</div>
In my controller, I was able to achieve it using
public function show(Book $book) {
$relatedBooks = Book::where('author_id', $book->author_id)
->where('id', '!=', $book->id)
->get();
return view('book')->with('book', $book)->with('relatedBooks', $relatedBooks);
}
In my web.php
Route::get('/books/{book}', [BooksController::class, 'show'])->name('book');
What I am trying to achieve is that, when I click Start Reading on
the Single Book Page, it takes me to another view page (Read) but it takes the book id that I clicked.
In the Read View I have this code,
<script>
"use strict";
document.onreadystatechange = function () {
if (document.readyState == "complete") {
window.reader = ePubReader("{{ $book->epub_url }}", {
restore: true
});
}
};
</script>
My problem is that I don't know how to take the id of the book that I
click and Pass it to the Read View
I will be glad if someone can explain the logic to me as I am confused.
To do via POST
//Book View
//change Start Reading to
<form action="{{ route("your.route.to.read") }}" method="POST">
#csrf
<input name="book_id " value ={{$book->id}} hidden>
<button type="submit">Start Reading</button>
</form>
//your Route will be
Route::get('/read',YourReadController#yourFunction)->name('your.route.to.read');
//your controller will be
public function yourFunction(Request $request)
{
//book id is in $$request->book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
To do via GET
//Book View
//change Start Reading<br><br> to
Start Reading
//route for get will be
Route::get('/read/{book_id}',YourReadController#yourFunction)->name('your.route.to.read');
//your countroller will be
public function yourFunction($book_id)
{
//book id is in $book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
I'm trying to pass a variable from a controller route to a view so i can display data relating to new assoicate model. It's working for other contrllers, but i can't figure out why it's not here.
Controller
#Index - Not passing down
public function index(){
$Advert = new PropertyAdvert();
return view('pages/Advert/index', compact('Advert'));
}
#Show - Is working, this is just an example.
public function show($id){
$user = Auth::user();
$Advert = PropertyAdvert::where('id', $id)->first();
return view('pages/Advert/show', compact('Advert', 'user'));
}
This is the route
Route::get('/property', 'AdvertisementController#index');
Route::get('/advertise', 'AdvertisementController#create')->middleware('auth');
Route::post('/createadvert', 'AdvertisementController#store');
Route::get('/property/{id}', 'AdvertisementController#show');
This is the template i'm trying to pass down to show.blade.php
<div class="row text-center" style="display:flex; flex-wrap:wrap">
<div class="col-lg-12">
<h3>Our most recent properties</h3>
#foreach ($Advert as $property)
<div class="col-md-3 col-sm-6">
<div class="thumbnail">
<img src="data:image/gif;base64,{{ $property->photo }}">
<div class="caption">
<h4>{{$property->address .', '. $property->town .', '. $property->county}}</h4>
</div>
<p>
More Info!
</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
Try this:
public function index(){
$Advert = PropertyAdvert::get();
return view('pages/Advert/index', compact('Advert'));
}
You need to actually access the data for that model.
I have a selectbox/dropdown in my sidebar.blade.php View:
{!! Form::open(array('method'=>'patch','route'=>'search')) !!}
{!! Form::select('search',$categories,null,array_merge(['class'=>'postform ts-select'],['placeholder'=>'Select Category'],['id'=>'search'],['name'=>'search'])) !!}
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3 col-centered" style="margin-top: 15px">
{!! Form::submit('Search', array('class'=>'blog-read ts-button')) !!}
</div>
</div>
{!! Form::close() !!}
The select box receives its values from model -> Blog.php :
public static function getCategories()
{
return self::groupBy('category')->lists('category', 'category');
}
On BlogController.php:
public function getIndex()
{
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
//echo'<pre>';
//dd($mostRecommended);
return View('blog::index', array('title' => "Welcome ", 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
public function search($category)
{
$query = Request::get('search');
$articles = DB::table('blog')->where('category', '=', $query);
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
return View('blog::index', array('title' => $query, 'articles' => $articles, 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
Routes:
Route::controller('/blog', '\Serverfireteam\blog\BlogController');
//Below line doesn't work (obviously)
Route::get('/blog/search',['as'=>'search','uses'=>'Serverfireteam\blog\BlogController#search']);
My main content (list of displayed posts) is iterated through the index.blade.php View as:
#foreach($last as $post)This iterates appropriately for ALL blogs. I need to make this dynamic with the dropdown / select box. I am sure that I need AJAX (but this is my first project and I'm clueless)
Here's a small sample of what I have tried:
Filter with dropdown Laravel
Laravel Ajax dropdown filter
Laravel 5.2 filter with dropdownlist
laravel 5.2 - search function
Get selected text from a drop-down list (select box) using jQueryI know that I am close and it is my inexperience that's destroying me here.My question: Please, where am I screwing up? I have spent days on this one issue.
** UPDATE Here, I updated my code to try something else; please take a look below
<div id="categories-6" class="widget widget_categories">
<div class="title-widget"><h3>Categories</h3></div>
<label class="screen-reader-text" for="cat">Categories</label>
{!! Form::open(array('method'=>'patch','route'=>'search')) !!}
{!! Form::select('search',$categories,null,array_merge(['class'=>'postform ts-select'],['placeholder'=>'Select Category'],['id'=>'search'],['name'=>'search'])) !!}
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3 col-centered" style="margin-top: 15px">
{!! Form::submit('Search', array('class'=>'blog-read ts-button')) !!}
</div>
</div>
{!! Form::close() !!}
</div>
Here is a script that I currently have on my index.blade.php
<script>
$('#search').on('change', function () {
var category = $(this).val();
var base_url = $({!! json_encode(url('/')) !!}).val;
$.ajax({
url: base_url + "/blog/search/" + category,
dataType : "json",
success: function (data) {
$('#inner-container').html(data.html); //here container is the wrapper of index view
}
});
});
</script>
Here is an updated look at my BlogController.php
public function getIndex()
{
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
//echo'<pre>';
//dd($mostRecommended);
return View('blog::index', array('title' => "Welcome ", 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
Lastly, the routes.php
Route::get('/blog/search/{category}', <br>
['a s'=>'search','uses'=>'Serverfireteam\blog\BlogController#search']);
Here is the error I'm receiving
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: search] [URI: blog/search/{category}].
(View:web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)
(View: web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)
(View: web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)
I have 2 actions functions in my controller and I want to pass variable to an other action function
This is my first function in my controller
public function newUserAction(Request $request)
{ .........
$url = $this->generateUrl('userBundle_new_user_reasonCodeAjaxView',
array('id' => $newUser->getCode(),
'countCode' => $countCode,));
return $this->redirect($url);
This my second funtion in my controller
public function userCodeAjaxViewAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$pc = $em->getRepository('UserBundle:Code')->find($id);
if($pc != null)
{
return $this->render('UserBundle:userCodeView.html.twig', array(
'pc' => $pc,
));
}
And my twif looks like this
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ pc.name|trans }}
{{countCode}}
</div>
</fieldset>
</div>
</div>
</div>
I am getting an error Variable "countCode" does not exist in...
Is there any idea how I can use a variable from a controller in a other controller?
You can not get a variable from another controller.
Every request creates only one controller instance.
What you can do is create a Service that you can call in the controller.
Something like that:
class CountUserService{
public function count(){
return 1; // count users here
}
}
And the in the controller do this:
$service = new CountUserService();
$data=['countCode' => $service->count()];
return $this->render('UserBundle:userCodeView.html.twig', $data);
You are using the same template scope for the two controllers, and this is not possible. If you are using two controllers to render two simple informations, why not simply returning a JSON and displaying it in a more global template using AJAX ?
Else a pure symfony solution would be to have a main template view.html.twig where you would put
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ render(controller("AppBundle:UserController:newUserAction")) }}
{{ render(controller("AppBundle:UserController:userCodeAjaxViewAction")) }}
</div>
</fieldset>
</div>
</div>
Given that then your two controller action templates would be simple {{ pc.name|trans }} and {{countCode}}.
Hope it helps you out !