My problem is the next in a voyager project I'm working on. I have an enterprise table and a users table.
I need to add a select input in the edit view of users table - but this view is in vendor\tcg\voyager\resources\views\users\edit-add.blade.php
I tried to add the select like:
<div class="form-group">
<label for="enterprise">Enterprises</label>
<select name="enterprise_id" id="inputEnterprise_id" class="form-control">
#foreach ($enterprises as $enterprise)
<option value="{{$enterprise['id']}}">{{$enterprise['name']}}</option>
#endforeach
</select>
</div>
I wrote the controller so it gets the data from the table. But, when I go to voyager and I try to edit some user, I have an exception error where says that the $enterprises var is not defined.
My edit() function in controller:
public function edit()
{
$enterprises = Enterprise::all();
return view('users.edit-add',compact('enterprises'));
}
Can someone tell me what the bug is?
Related
I'm new to Laravel and learning mostly by laravel docs and other resources online. This is my first post on asking for help on forums, so please bear with me. I am having an issue in calling a route from laravel blade.
In my view blade I have dropdown selection for list of businesses which user can select from. When user select the business it will call route the route below.
This is my route that I am trying to call from my view.
Route::group(['as' => 'frontend.', 'namespace' => 'Frontend', 'middleware' => ['auth', 'subscribed']], function () {
Route::get('/businesses/{business_id}', 'BusinessesController#business')->name('business');
});
In my view frontend header blade I am listing all the business user has access to
#if (auth()->user()->businesses->count() > 1)
<div class="d-flex align-items-center" data-select2-id="select2-data-14-ocz7">
<select class="form-control" name="business_id" id="business_id" onchange="showBusinessData(this.value)">
<?php $businesses = auth()->user()->businesses; ?>
#foreach($businesses as $business)
<option value="{{$business->id}} {{ session('business_id', '') }}" <?php if(session('business_id') == $business->id) echo "selected"; ?>>{{ $business->business_name }} </option>
#endforeach
</select>
</div>
#endif
Above view does not load the route.
I found the mistake. In my view blade I have set for onchange to call function in js. I just had to update to set the window href location to the route.
function showBusinessData(val){
window.location.href = val;
}
In my Laravel app, I am creating a dynamic search functionality using Eloquent and I have a customer search form which looks like this:
customers.search.blade.php Contrived Example
<form method="post" action="{{ route('customers.search') }}">
#csrf
<div class="form-group">
<label for="first_name" class="font-weight-bold">First Name</label>
<div class="input-group">
<div class="input-group-prepend">
<select name="first_name['operator']" class="custom-select">
<option value="%">%</option>
<option value="=">=</option>
<option value="!=">!=</option>
</select>
</div>
<input id="first_name" name="first_name['query']" type="text" class="form-control">
</div>
</div>
</form>
I have a SearchCustomerRequest (FormRequest) class which builds out the validation rules dynamically. I've dumped it, so you can see what the generated rules looks like:
In my CustomersController under search method, I did the following to see what the validated request array looks like:
class CustomersController extends Controller
{
// ...
public function search(SearchCustomerRequest $searchCustomerRequest)
{
dd($searchCustomerRequest->validated());
}
// ...
}
In order to test this, in the search form, I've selected the firstname['operator'] to % and typed the first_name['query'] to test and submitted the form.
I got the following response (i.e. empty array):
[]
So, I dumped the whole request object $searchCustomerRequest to see what was in the parameter bag and this is what I see:
As you can see, my request is valid and my rules also looks correct, yet the validation doesn't seem to be working as expected.
Any ideas what might be wrong here?
In your ParameterBag, the first_name properties operator and query are enclosed in single quotes. In your HTML, the name attribute should exclude the single quotes. Ex: name="first_name[query]"
I'm new at Laravel and not good with syntax. I have seen many tutorials and have read many answers, but still, my mind didn't get the point of how can I have a dropdown field for a foreign key.
I'm having one table "Section" and other "Class." I want to show the name of classes in section page.
Sections Migration
Schema::create('sections', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('class_id')->unsigned();
$table->foreign('class_id')->references('id')->on('classses');
});
Classses Migration
Schema::create('classses', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Honestly, I don't know if I should have changed my controller or not.
Blade/View
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="title">
</div>
<div class="form-group">
<label for="cid">Class</label>
???????????????
</div>
Index Function
public function index()
{ $sections = Section::all();
return view('sections.index', compact('sections'));
$classs = Classs::all()->pluck(['id', 'title']);
return view('sections.index')->with('classs', $classs); }
Error is Unreachable Statement at line $class & Expected string, got array at ([id,'title])
In your controller, you have a function to return the view.
Change it to include ->with(), so you can access the classes in the view:
// if the controller is not for the classes, add this up top:
use App\Classs; // include model name
$classs = Classs:all();
return view('yourView')->with('classe', $classs);
Then, in your view, you can just do this:
<div class="form-group">
<label for="cid">Class</label>
<select class="" name="cid">
<option value="null">Class</option>
#foreach($classs as $class)
<option value="{{$class->id}}">{{$class->title}}</option>
#endforeach
</select>
</div>
It loops over all the classes in your database and creates a <option> element for them. Looking at your first migration, you're using the id in the other table, so you need to set that as the value.
Change your index function to this:
public function index()
{
$sections = Section::all();
$classs = Class::all();
return view('sections.index')->with('sections', $sections)->with('classs', $classs);
}
Can you tell me where can I write conditions such as select * from class where role_id=2 etc.
Basically, in an MVC framework, you do all your queries in your controller and pass the data to the view, in which you display the data.
Laravel has a DB class which you can use for basic queries:
select * from class where role_id = 2
Would become this in Laravel, using the DB class.
DB::table('class')->where('role_id', 2)->get();
// or if it's a model:
Model::where('role_id', 2)->get();
I have now used this code in blade page
#php
use Illuminate\Support\Facades\DB;
$cid=DB::table('classses')->get();
$uid=DB::table('users')->where('role_id','3')->get();
$counter=0;
#endphp
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $class)
#if($counter==0)
<option selected="selected" value="{{$class->id}}">{{$class->title}}</option>
{{$counter++}}
#else
<option value="{{$class->id}}">{{$class->title}}</option>
#endif
#endforeach
</select>
I am new to laravel, I have a two tables: users, selected_users both contains id, name.
I wanted to create a drop down list that is populated from users, when selecting a user name it will insert the user's name beside the drop down menu.
When I press submit the name should be saved to the selected_user table.
Can please someone help with this code how to write it in the view and controller.
I'm not quiet sure what you really want to achieve but try the code below;
userview.blade.php
<div class="container">
#if(session('success'))
<h1>{{session('success')}}</h1>
#endif
<form method="POST" action="{{route('save.selected-user')}}">
{{ csrf_field() }}
<div class="form-group row">
<div class="col-sm-8">
<select class="form-control" id="selectUser" name="user_selected" required focus>
<option value="" disabled selected>Please select user</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{ $user->name }}</option>
#endforeach
</select>
</div>
<label class="col-sm-4 col-form-label" id="displayUser">Show selected User
here</label>
</div>
<input type="submit" value="Save">
<script type="text/javascript">
var mytextbox = document.getElementById('displayUser');
var mydropdown = document.getElementById('selectUser');
mydropdown.onchange = function(){
mytextbox.value = mytextbox.value + this.value; //to appened
mytextbox.innerHTML = this.value;
}
</script>
TestController.php (make sure you have User model and SelectedUser model)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\SelectedUser;
class TestController extends Controller
{
public function populateUsers()
{
$users = User::all();
return view('test.userview', compact('users'));
}
public function saveUser(Request $rq)
{
$selectedUser = new SelectedUser;
$selectedUser->name = $rq->user_selected;
$selectedUser->save();
return redirect()->back()->with('success', 'Selected Username added successfuly');
}
}
WEB.php
Route::get('/selected-user', 'TestController#populateUsers');
Route::POST('/selected-user', 'TestController#saveUser')->name('save.selected-user');
Please let me know if it works
I'm currently new on Laravel and trying to develop my first project. I have this MethodNotAllowedHttpException in RouteCollection.php line 218 error during my development for inserting data into database. I have searched both Google & Stackoverflow for solutions but non are related to my current problem and some of them way too complex for this simple problem (I think so...).
I have my form in my checklist page:-
<form action="{{url('addchecklist')}}" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-sm-12">
<div class="text-left">
<input type="hidden" name="schmFK" value="{{$id}}">
<div class="col-sm-6">
<h4>
<label>Section</label>
<select class="selectpicker form-control" data-live-search="true" name="sctionPK">
<option selected>Select the Section</option>
#foreach ($sction as $key=>$slct1)
<option value="{{$slct1->ssctionPK}}">{{strtoupper($slct1->ssctionName)}}</option>
#endforeach
</select>
</h4>
</div>
<div class="col-sm-2">
<button type="button" data-toggle="modal" data-target=".bs-example-modal-lg" class="btn btn-primary btn-sm" style="margin-top:33px; padding-top:7px; padding-bottom:7px;">Add Section</button>
</div>
<div class="col-sm-4">
<h4>
<label>Severity</label>
<select class="selectpicker form-control" name="svrityPK">
<option selected>Select the Severity</option>
#foreach ($svrity as $key=>$slct2)
<option value="{{$slct2->severityPK}}">{{strtoupper($slct2->severityName)}}</option>
#endforeach
</select>
</h4>
</div>
<div class="col-sm-12">
<h4>
<label>Question</label>
<input class="form-control" type="text" placeholder="Question" name="question">
</h4>
</div>
<div class="col-sm-6">
#include('widgets.button', array('class'=>'primary btnaddstd', 'size'=>'lg', 'type'=>'submit', 'value'=>'Add Checklist'))
</div>
</div>
</div>
</div>
</form>
Then I have this route for inserting data from the form into database:-
Route::post('/addchecklist', function (Request $request){
// Create instance to store record
$scheme = new App\checklists;
$scheme->schmFK = $request->schmFK;
$scheme->schSectionFK = $request->sctionPK;
$scheme->severityFK = $request->svrityPK;
$scheme->clQuestion = $request->question;
$scheme->save(); // save the input
// Sort all records descending to retrieve the newest added record
$input = App\checklists::orderBy('cklistPK','desc')->first();
// Set search field variable default value of null
$src = isset($src) ? $src : null;
// Get Checklist reference from cklists_stddetails with the designated ID
$chkstd = App\Cklists_stddetail::where('cklistFK', $input->cklistPK)
->join('stddetails', 'stdDtlFK', '=', 'stddetails.sdtlPK')
->get();
// Get the newest stored record
$chcklst = App\checklists::where('cklistPK', $input->cklistPK)->firstOrFail();
// Get all data from table 'stddetails'
$stddetail = App\stddetails::all();
// Get all data from table 'standards'
$stndrd = App\standard::all();
// Get all data from table 'sections'
$sction = App\Section::all();
// Redirect to 'addref.blade' page with the newest added record
return redirect('addref/'.$input->cklistPK)
->with('src', $src)
->with('chkstd', $chkstd)
->with('id',$input->cklistPK)
->with('schmid', $request->schmFK)
->with('chcklst', $chcklst)
->with('stddetail', $stddetail)
->with('stndrd', $stndrd)
->with('sction', $sction);
});
My scenario is this, I have a form for user to input data in it. Then when the data is saved, they will be redirected to the page of that data to do something there. The data is successfully saved in the database but the redirection to the designated page (addref.blade) with the newest record ID return error:-
But the URL goes where I wanted it to go (means the URL is right):-
As you can see, the usual solution from the net that I found are:-
Make sure both method from routes and the form is the same, and mine it is:-
method="POST"
Route::post
Make sure the URL routes can recognize the form's action URL, and mine it is:-
<form action="{{url('addchecklist')}}" method="POST">
Route::post('/addchecklist', function (Request $request)
Include CSRF token field in the form, and mine it have been included:-
<form action="{{url('addchecklist')}}" method="POST">
{{ csrf_field() }}
I have tried those simple solution provided on the net and nothing is helpful enough. I'm still wondering what else I have missed and hoped that anyone here can assist on solving my issue.
I think the error is that you have a redirect which you have not registered in your routes or web.php file.
Sample redirect:
Route::post('/addchecklist', function (Request $request){
//some post process here...
return redirect('addref/'.$input->cklistPK)
->with('src', $src)
->with('chkstd', $chkstd)
->with('id',$input->cklistPK)
->with('schmid', $request->schmFK)
->with('chcklst', $chcklst)
->with('stddetail', $stddetail)
->with('stndrd', $stndrd)
->with('sction', $sction);
});
Route::get('addref/{id}', function(Request $request){
//show the blade.php with data
});
Can you please write :
url('/addchecklist')
instead of :
url('addchecklist')
and then print_r('in');
and die; and check what you get.
Route::post('/addchecklist', function (Request $request){
print_r('in');
die;
});