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)
Related
I'm using the dynamic dependent dropdown with laravel 5.4 it works fine in local machine but when I put it on hosting and get accessed by many users it takes too long to get the second dropdown.
Fopm.php:
<div class="form-group col-md-4 col-sm-4 col-xs-6">
{!! Form::label('id_country',trans('Country').":",['class' => 'labels']) !!}
{!! Form::select('id_country',['' => '...'] +$countries->toArray(),Input::get('id_country'),['class' => 'js-example-basic-single form-control select2_country']) !!}
</div>
<div class="form-group col-md-4 col-sm-4 col-xs-6">
{!! Form::label('id_city',trans('City').":",['class' => 'labels']) !!}
{!! Form::select('id_city',['' => '...']+$cities->toArray(),Input::get('id_city'),['class' => 'js-example-basic-single form-control select2_city']) !!}
</div>
script.php:
sw = $('.select2_country').select2({
placeholder: "Sélectionner",
allowClear: true
});
sw.on("change", function (e) {
id = $(this).select2('data')[0].id;
$('.select2_city').val(null).trigger('change');
$('.select2_city').select2({
placeholder: "selectionner",
allowClear: true,
ajax: {
url: '/api/caneva/cities?id=' + id
}
});
});
$('.select2_city').select2({
placeholder: "selectionner",
allowClear: true,
});
Controller.php:
public function cities(SearchRequest $request)
{
$id = $request->input('id');
$city= DB::table('city')->select('libelle_city as text', 'id as id')
->where('id_country', '=', $id)
->get();
return response()->json([
'results' => $city
]);
}
I am new to laravel and I want to let the user update his/her profile when logged in. I want to get the ID of the user when updating his/her profile but when I click on the edit view to pass the data using the id I got this error:
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\Applicant\HomeController::edit()
Here is the code to my controller:
public function edit($id)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant');
return redirect(route('applicant.home'));
}
return view('applicant-dashboard.edit')->with('applicants', $applicant);
}
public function update($id, UpdateApplicantRequest $request)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant not found');
return redirect(route('applicant.index'));
}
$input = $request->all();
$cashier = $this->applicantRepository->update([
'name' => $input['name'],
'email' => $input['email'],
'password' => bcrypt($input['password']),
'address' => $input['address'],
'cellphone_no' => $input['cellphone_no']], $id);
Flash::success('Profile updated successfully.');
return redirect(route('applicant.index'));
}
Here is the code in my routes file:
Route::get('/edit', 'HomeController#edit')->name('applicant.edit');
Here is the code in my blade file:
#extends('layouts.app')
#section('content')
<section class="content-header">
<h1>
Applicant Profile
</h1>
</section>
<div class="content">
{{-- #include('adminlte-templates::common.errors') --}}
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'patch']) !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
You need to pass the id into your route:
Route::get('/edit/{id}', 'HomeController#edit')->name('applicant.edit');
You pass the ID into web.php:
Route::get('edit/{ID}', 'HomeController#edit')->name('applicant.edit');
I have this problem here with my query.
In my getIndex method I'm listing an array of objects from a table and then in my postIndex when I'm trying to post them it turns out that I'm posting the number of the object in the array, but not the object itself. Why is this happening, someone?
public function getIndex() {
$user = Auth::user();
return view('educator.account.account',[
'user' => $user,
'class'=> ClassSubject::where('teacher_id','=',$user->id)()->lists('class_id'),
'subject'=> ClassSubject::where('teacher_id','=',$user->id)->lists('subject_id'),
]);
}
public function postIndex(Request $request) {
ClassSubject::where([
['subject_id','=',$request->input('subject')],
['class_id','=',$request->input('class')]
])->get();
The form:
<div class="panel panel-default">
<div class=" panel-heading" id="admin-heading"> Добре дошли!</div>
<div class="panel-body">
<br>
{!! Form::open(['action' => 'Educator\AccountController#postIndex', 'class' => 'form form-vertical' ]) !!}
<div class="form-group col-md-6">
<div class="col-md-4">{!! Form::label('class','Избери клас:') !!}</div>
<div class="col-md-6">{!! Form::select('class', $class, null, ['class'=>'form-control']) !!}</div>
</div>
<div class="form-group col-md-6">
<div class="col-md-5">{!! Form::label('subject','Избери предмет:') !!}</div>
<div class="col-md-6">{!! Form::select('subject', $subject, null, ['class'=>'form-control']) !!}</div>
</div>
<div align="center">
<br>
<br>
{!! Form::submit('Избери', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
This might work for you in getIndex():
return view('educator.account.account', [
'user' => $user,
'class'=> ClassSubject::lists('class_id', 'YOUR_HUMAN_READABLE_FIELD')->where('teacher_id', $user->id)->get(),
'subject'=> ClassSubject::lists('subject_id', 'YOUR_HUMAN_READABLE_FIELD')->where('teacher_id', $user->id)->get()',
]);
Let me know if it worked or not.
EDIT
The problem has been fixed, this did the trick:
DB::table('class_subject')->where('teacher_id', $user->id)->pluck('id', 'name');
As you are passing only one parameter you will receive an array with its values set to the column you are passing to lists method.
But Form::select uses array's keys for values of options and values for the display names of options.
So use the lists method as following.
ClassSubject::lists('displayName', 'value')
In laravel 5.2 lists method is deprecated and consider using the pluck method instead which has the same signature.
I'm new to laravel and trying to learn using it. I created a little project for threads. Nothing special. The thing is, the function to edit something doesn't work and I cant see why. Maybe someone of you see the mistake?
thats my Routes:
Route::get('/index', 'Test\\TestController#index');
Route::get('/add', 'Test\\TestController#add');
Route::post('/test', 'Test\\TestController#store');
Route::get('/show/{id}', 'Test\\TestController#show');
Route::get('/show/{id}/edit', ['as' => 'edit', 'uses' => 'Test\\TestController#edit']);
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController#update']);
thats the important parts of the edit method:
public function edit($id) {
$thread = Thread::query()->findOrFail($id);
return view('test.edit', [
'thread' => $thread
]);
}
public function update($id, StoreRequest $request) {
$thread = Thread::query()->findOrFail($id);
$thread->fill($request->all());
$thread->save();
return redirect(action('Test\\TestController#show', [$thread->id]));
}
}
show.blade
#extends('master')
#section('content')
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
{{ $thread->thread }}
</div>
</div>
<div class="form-body">
<div class="form-group"><br>
<ul>
{{$thread->content }}<br><br>
{{$thread->created_at->format('d.m.Y H:i:s')}}
</ul>
</div>
</div>
<div class="panel-footer">
<div class="btn btn-primary">Thread bearbeiten</div>
</div>
</div>
#stop
edit blade ( formular where the text I want to add is in the textbox and the save button )
#extends('master')
#section('content')
{!! Former::horizontal_open()->method('PUT')->action(action("Test\\TestController#update", [$thread->id])) !!}
{!! Former::populate($thread) !!}
{!! Former::text('thread')->label('Thread') !!}
{!! Former::text("content")->label("Content") !!}
{!! Former::large_primary_submit('Save!') !!}
{!! Former::close() !!}
#stop
model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
ERROR MESSAGE :
No query results for model [App\Models\Thread\Thread].
I have written a code that sends email to selected people using check-box, now I have to add select all and clear all button so that all the check-boxes should get selected/dis-selected.
Here is my code:
My controller:
public function display(){
$data = Input::get('agree');
$count = count ($data);
$this->contact( $data );
return view('clientinfo.display', compact('data','count'));
}
My View:
#extends('app')
#section('content')
<h1>Welcome! Send e-mail to selected people.</h1>
<hr>
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
#foreach($clients as $client)
{!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email]) !!}
<article>
{{ $client->user_name }}
{{ $client->email }}
</article>
#endforeach
{!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
<br/>
#include('clientinfo.newmember')
#stop
if i add a button in my view naming select-all and clear-all how do i relate them, to select all the members within list?
You could give the checkboxes a class and use jquery.
You also need a checkbox that says "select all" with an ID.
$(function() {
$('#selectAll').click(function() {
if ($(this).prop('checked')) {
$('.your_checkbox_class').prop('checked', true);
} else {
$('.your_checkbox_class').prop('checked', false);
}
});
});
PS: Code is not tested
I have made changes to my code and it worked using JavaScript.
Here is my code:
#extends('app')
#section('content')
<h1>Welcome! Send e-mail to selected people.</h1>
<hr>
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET','name'=>'f1' , 'id'=>'form_id'))!!}
<br/>
#foreach($clients as $client)
<div class="form-group">
{!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email], ['class' => 'questionCheckBox']), $client->email !!}
<br/>
</div>
#endforeach
<div class="form-group">
{!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
</div>
<div class="form-group">
{!! Form::button('Select All',['class' => 'btn btn-info form-control','onClick'=>'select_all("agree", "1")']) !!}
</div>
<div class="form-group">
{!! Form::button('Clear All',['class' => 'btn btn-danger form-control','onClick'=>'select_all("agree", "0")']) !!}
</div>
{!! Form::close() !!}
#stop
#endsection
And in my master file,I have added a JavaScript:
<script type="text/javascript">
var formblock;
var forminputs;
function prepare() {
formblock= document.getElementById('form_id');
forminputs = formblock.getElementsByTagName('input');
}
function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
// regex here to check name attribute
var regex = new RegExp(name, "i");
if (regex.test(forminputs[i].getAttribute('name'))) {
if (value == '1') {
forminputs[i].checked = true;
} else {
forminputs[i].checked = false;
}
}
}
}
if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}
</script>
Earlier i was doing this using Jquery, that was not working, then i tried using JavaScript and it worked.
I have tried various ways to add jquery in my code which are given in answers of stackoverflow.com, but nothing worked in laravel. If anyone know how to use jquery in laravel, please leave a comment.