here's my form code :
<div class="form-group">
<label>Warranty:</label>
{{ Form::checkbox('warranty', 0, false) }}
</div>
here's my controller code :
public function save(Request $request, $obj = null) {
if (!$obj) {
$obj = new Service;
}
(Input::has('warranty')) ? true : false;
return $this->saveHandler($request, $obj);
}
it throws this error Class 'App\Http\Controllers\Input' not found
any idea ?
You can use request object to get the warranty input as:
$request->get('warranty')
or
$request->has('warranty')
$request->merge(array('checkbox_name' => $request->has('checkbox_name') ? true : false));
Object::create($request->all());
This is how I save boolean parameters which are checkboxes in form.
Or simply give your checkbox value
{{ Form::checkbox('checkbox_name', 1) }}
Add this at top in the controller,
use Illuminate\Support\Facades\Input;
Input facade was removed in latest verison, so you can add it to config/app.php:
'Input' => Illuminate\Support\Facades\Input::class,
Or add this line to the beginning of a controller:
use Illuminate\Support\Facades\Input;
Or write full path when you use it:
(\Illuminate\Support\Facades\Input::has('warranty')) ? true : false;
Related
I need to pass the data from the controller to a header file inside partials folder.
Suggest me how to pass the data "currency" from the controller to the header file.
Controller :
class HeaderController extends Controller
{
public function rate(){
$currency = Whmcs::GetCurrencies([
]);
return view('partials.header',compact('currency'));
}
}
Header file:
<form name="form">
<select name="currency" class="form-control">
#foreach($currency['currencies']['currency'] as $key=>$value)
#for($key=0;$key<100;$key++)
#endfor
<option value="{{$value['code']}}">{{$v=$value['code']}}</option>
#endforeach
</select>
</form>
Route:
Route::any('/partials.header', 'HeaderController#rate');
You can make this via laravel view composer method as below:
Add this method in App/Providers/ComposerServiceProvider.php in boot method
view()->composer(['partials.header'], function ($view) {
$currency = Whmcs::GetCurrencies([]);
$view->with('currency', $currency);
});
Then you can use $currency variable to your header file you don't need to pass it from any controller.
You can achieve this :
return view('partials.header', ['currency' => $currency]);
Your data should be an array with a key-value pair.
You can also use with method:
return view('partials.header')->with('currency', $currency);
Also, you can use compact :
return view('partials.header', compact('currency'));
For more, please refer Passing Data To Views
why edit process fails, how to display the data in the dropdown ?
controller :
public function edit($id)
{
$data['data_article'] = Article::find($id);
$data['tambah_author'] = \DB::table('authors')->list('username','id');
$data['id_author'] = \DB::table('authors')->list('id');
return View::make('article.edit',$data)->with('authors',$data);
}
VIew :
{{ Form::select('author',
(['0' => 'Pilih Author'] + $tambah_author),
$id_author,
['class'=>'form-control']) }}
thanks, for your help
According to Query Builder API, you need to change list to lists:
$data['tambah_author'] = \DB::table('authors')->lists('username','id');
$data['id_author'] = \DB::table('authors')->lists('id');
The correct method is lists.
So change to:
$data['tambah_author'] = \DB::table('authors')->lists('username','id');
$data['id_author'] = \DB::table('authors')->lists('id');
I am new to Laravel and I have been trying to store all records of table 'student' to a variable and then pass that variable to a view so that I can display them.
I have a controller - ProfileController and inside that a function:
public function showstudents() {
$students = DB::table('student')->get();
return View::make("user/regprofile")->with('students',$students);
}
In my view, I have this code:
<html>
<head>
//---HTML Head Part
</head>
<body>
Hi {{ Auth::user()->fullname }}
#foreach ($students as $student)
{{ $student->name }}
#endforeach
#stop
</body>
</html>
I am receiving this error: Undefined variable: students (View:regprofile.blade.php)
Can you give this a try,
return View::make("user/regprofile", compact('students')); OR
return View::make("user/regprofile")->with(array('students'=>$students));
While, you can set multiple variables something like this,
$instructors="";
$instituitions="";
$compactData=array('students', 'instructors', 'instituitions');
$data=array('students'=>$students, 'instructors'=>$instructors, 'instituitions'=>$instituitions);
return View::make("user/regprofile", compact($compactData));
return View::make("user/regprofile")->with($data);
For Passing a single variable to view.
Inside Your controller create a method like:
function sleep()
{
return view('welcome')->with('title','My App');
}
In Your route
Route::get('/sleep', 'TestController#sleep');
In Your View Welcome.blade.php. You can echo your variable like {{ $title }}
For An Array(multiple values) change,sleep method to :
function sleep()
{
$data = array(
'title'=>'My App',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('welcome')->with($data);
}
You can access you variable like {{ $author }}.
The best and easy way to pass single or multiple variables to view from controller is to use compact() method.
For passing single variable to view,
return view("user/regprofile",compact('students'));
For passing multiple variable to view,
return view("user/regprofile",compact('students','teachers','others'));
And in view, you can easily loop through the variable,
#foreach($students as $student)
{{$student}}
#endforeach
You can try this as well:
public function showstudents(){
$students = DB::table('student')->get();
return view("user/regprofile", ['students'=>$students]);
}
Also, use this variable in your view.blade file to get students name and other columns:
{{$students['name']}}
Try with this code:
return View::make('user/regprofile', array
(
'students' => $students
)
);
Or if you want to pass more variables into view:
return View::make('user/regprofile', array
(
'students' => $students,
'variable_1' => $variable_1,
'variable_2' => $variable_2
)
);
In Laravel 5.6:
$variable = model_name::find($id);
return view('view')->with ('variable',$variable);
public function showstudents() {
$students = DB::table('student')->get();
return (View::make("user/regprofile", compact('student')));
}
try with this code :
Controller:
-----------------------------
$fromdate=date('Y-m-d',strtotime(Input::get('fromdate')));
$todate=date('Y-m-d',strtotime(Input::get('todate')));
$datas=array('fromdate'=>"From Date :".date('d-m-Y',strtotime($fromdate)), 'todate'=>"To
return view('inventoryreport/inventoryreportview', compact('datas'));
View Page :
#foreach($datas as $student)
{{$student}}
#endforeach
[Link here]
$books[] = [
'title' => 'Mytitle',
'author' => 'MyAuthor,
];
//pass data to other view
return view('myView.blade.php')->with('books');
or
return view('myView.blade.php','books');
or
return view('myView.blade.php',compact('books'));
----------------------------------------------------
//to use this on myView.blade.php
<script>
myVariable = {!! json_encode($books) !!};
console.log(myVariable);
</script>
In laravel 8 and above, You can do route binding this way.
public function showstudents() {
$students = DB::table('student')->get();
return view("user/regprofile",['students'=>$students]);
}
In the view file, you can access it like below.
#foreach($students as $student)
{{$student->name}}
#endforeach
In Twig partial rendered by separate controller, I want to check if current main route equals to compared route, so I can mark list item as active.
How can I do that? Trying to get current route in BarController like:
$route = $request->get('_route');
returns null.
Uri is also not what I'm looking for, as calling below code in bar's twig:
app.request.uri
returns route similar to: localhost/_fragment?path=path_to_bar_route
Full example
Main Controller:
FooController extends Controller{
public function fooAction(){}
}
fooAction twig:
...some stuff...
{{ render(controller('FooBundle:Bar:bar')) }}
...some stuff...
Bar controller:
BarController extends Controller{
public function barAction(){}
}
barAction twig:
<ul>
<li class="{{ (item1route == currentroute) ? 'active' : ''}}">
Item 1
</li>
<li class="{{ (item2route == currentroute) ? 'active' : ''}}">
Item 2
</li>
<li class="{{ (item3route == currentroute) ? 'active' : ''}}">
Item 3
</li>
</ul>
pabgaran's solution should work. However, the original problem occurs probably because of the request_stack.
http://symfony.com/blog/new-in-symfony-2-4-the-request-stack
Since you are in a subrequest, you should be able to get top-level (master) Request and get _route. Something like this:
public function barAction(Request $request) {
$stack = $this->get('request_stack');
$masterRequest = $stack->getMasterRequest();
$currentRoute = $masterRequest->get('_route');
...
return $this->render('Template', array('current_route' => $currentRoute );
}
Haven't run this but it should work...
I think that the best solution in your case is past the current main route in the render:
{{ render(controller('FooBundle:Bar:bar', {'current_route' : app.request.uri})) }}
Next, return it in the response:
public function barAction(Request $request) {
...
return $this->render('Template', array('current_route' => $request->query->get('current_route'));
}
And in your template compares with the received value.
Otherwise, maybe is better to use a include instead a render, if you don't need extra logic for the partial.
in twig you can send request object from main controller to sub-controller as parameter:
{{ render(controller('FooBundle:Bar:bar', {'request' : app.request})) }}
in sub-controller:
BarController extends Controller{
public function barAction(Request $request){
// here you can use request object as regular
$country = $request->attributes->get('route_country');
}
}
I'm developing a Laravel PHP application and one of my forms that I'm using for creating new data contains a checkbox field. When I try to submit the form leaving this checkbox unchecked, I get a 'SQLSTATE: cannot be null' error. I've tried using a couple of solutions to fix this problem but they haven't worked for me yet.
Controller:
public function store()
{
$input = \Input::all();
$validation = new Validators\Video;
if($validation->passes())
{
/* Additional Controller Code for storing file path names */
return \Redirect::route('overview');
}
else
{
return \Redirect::back()
->withInput()
->withErrors($validation->errors)
->with('message', 'Could not create video');
}
}
The name of the field where I'm storing the checkbox data is 'video_active'.
EloquentVideoRepository:
public function create($input, $filename, $thumb_filename)
{
/* Need this structure in order for photos to actually be displayed. */
$newVideo = new Video;
/* Store Data here */
/* Using '\Input::get()' method for accepting unchecked checkboxes */
$newVideo->video_active = \Input::get('video_active');
return $newVideo->save();
}
Form:
#section('content')
{{ Form::open(array('route' => 'store_video', 'method' => 'POST', 'files' => true)) }}
/* Additional Form Code here */
/* Code for accepting checkbox data */
<div class="form-group">
{{ Form::label('video_active', 'Active') }}
{{ Form::checkbox('video_active', 'Active') }}
</div>
<div class="form-group">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
#stop
I'm not sure why I can't accept an unchecked checkbox when creating new data in my form. Any help is greatly appreciated!
$newVideo->video_active = \Input::get('video_active');
\Input::get('video_active'); will return null if it hasn't been defined.
Use a ternary statement like this
$newVideo->video_active = (\Input::get('video_active') !== null) ? \Input::get('video_active') : "";
this is equivalent to
if(\Input::get('video_active') !== null) {
$newVideo->video_active = \Input::get('video_active');
} else {
$newVideo->video_active = "";
}
What I'd recommend you do is use either 1 or 0 rather than "Active" and ""
set your database column video_active as INT(1) and then do
{{ Form::checkbox('video_active', '1') }}
$newVideo->video_active = (\Input::get('video_active') == 1) ? 1 : 0;
then if you ever need to check if video_active is set
if($myModel->video_active == 1) {
// Video is active
}
While explanation looks good, my suggestion of implementation would be:
$newVideo->video_active = \Input::get('video_active', false);