ErrorException undefined variable data in view - laravel

(2/2) ErrorException
Undefined variable: data(View: E:\xampp\htdocs\snipe-it\resources\views\hardware\bulk-checkout.blade.php)
My Controller
public function bulkAssetImport($assets_ids)
{
$data='testersdfsdf';
return Redirect('hardware/bulk-checkout',compact('data'));
}
My blade.php File
#foreach
{{$data}}
#endforeach
I tried different ways but no use, Please help me

If you just want to display data in html. Yo can do something like this.
<p>{{ $data }}</p>
If you want to loop data in your html
In your controller
public function bulkAssetImport($assets_id)
{
$data= array(
array('name'=> 'John doe', 'age' => 22),
array('name'=> 'Doe not', 'age' => 22),
);
return view('hardware/bulk-checkout',compact('data'));
}
In your html:
#foreach($data as $d)
My name is: {{$d->name}} and my age is: {{ $d->age }}
#endforeach

Related

laravel controller returns only 2 values

I have a database with 3 tables. A separate model is connected to each table, and there is a controller that accepts values from all models. On the site page, I will have 3 tables that will be populated from a mysql table.
When I connected 2 models, everything worked fine. But after connecting 3, I get an error
undefined variable: sec_3.
If you delete one of the variables, then everything will work fine. It seems to me that the problem is either with the controller or with the file blade.php but I do not know how to fix it so that everything works properly. How to fix it?
My code:
Controller:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context=['bbs' =>PreschoolInstitution3::latest()->get()];
$context_2=['s' =>PreschoolInstitution::latest()->get()];
$context_3=['sec_3' => TrainingPrograms::latest()->get()];
return view('our_employees', $context, $context_2, $context_3);
}
}
web.php:
Route::get('/OurEmployees',[PreschoolInstitution3Controller::class,'index'] )->name('OurEmployees');
blade.php:
#foreach ($s as $section_2) <tr> <td>{{$section_2->number}}<td> <td>{{$section_2->fullname }}<td> <td>{{$section_2->post }}<td> <td>{{$section_2->telephone }}</td> <td>{{$section_2->email }}</td>
#endforeach #foreach ($bbs as $section )
{{$section->number}} {{$section->full_name}} {{$section->post}} {{$section->education}} {{$section->category}} {{$section->teaching_experience}} {{$section->professional_development}}
#endforeach #foreach ($sec_3 as $section_3)
{{ $section_3->number }}
{{ $section_3->level }}
{{ $section_3->directions }}
{{ $section_3->type_of_educational_program }}
{{ $section_3->period_of_assimilation }}
{{ $section_3->number_of_students }}
#endforeach
You should pass an array of data to view:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context = [
'bbs' => PreschoolInstitution3::latest()->get(),
's' => PreschoolInstitution::latest()->get(),
'sec_3' => TrainingPrograms::latest()->get()
];
return view('our_employees', $context);
}
}
https://laravel.com/docs/9.x/views#passing-data-to-views
Another one is that add a second parameter of an array with name to view( ) .
class PreschoolInstitution3Controller extends Controller {
public function index(){
$bbs = PreschoolInstitution3::latest()->get();
$s = PreschoolInstitution::latest()->get();
$sec_3 = TrainingPrograms::latest()->get();
return view('our_employees', [
'bbs' => $bbs,
's' => $s,
'sec_3' => $sec_3
]);
}
}

Undefined property: Illuminate\Database\MySqlConnection::$quizUser

I run the laravel code for fetching the data from database but i got the error.
This is controller's code name is UserProfileController.blade.php
public function FetchUserQus()
{
$data = DB::table('userquestion')->where('userEmail', '=', '{{
Auth::user()->email }}');
return view('designpages/userqus', ['data' => $data]);
}
Route::get('designpages/userqus',
'UserProfileController#FetchUserQus')->name('designpages/userqus');
This is view page code saved with name designpages/userqus.blade.php
#foreach($data as $datas)
<p><b>Question: {!! $datas->quizUser !!}</b></p>
<p><b>Answer: </b>{!! $datas->ansAdmin !!}</p>
#endforeach
You cannot use blade syntax in the controller: So change this:
$data = DB::table('userquestion')->where('userEmail', '=', '{{ Auth::user()->email }}');
to this:
$data = DB::table('userquestion')->where('userEmail', '=', auth()->user()->email)->get();
And also I use get() to return a collection, without it it returns a Illuminate\Database\Query\Builder instance.

Laravel 5.5 - Missing required parameters for [Route:]

What I'm trying to achieve is very simple, but I'm just making this a lot harder than it needs to be. So am seeking help for what is apparently, my lack of Laravel experience.
All I want to do is have a form that can update database entries via a text input. This has to be dynamic as it's being used for a few databases and I don't want to have multiple files for them.
Sorry in advance for the probable messy/crap code...
Here's the routes I have:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')->name('greeting.update');
The PlayerProfileController
// Display greeting message
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
And finally the form
{{ Form::open(['action' => ['PlayerProfileController#updateGreeting', $greetInfo->greeting], 'method' => 'POST']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::hidden('_method', 'PUT') }}
<br>
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Any and all help is appreciated. Thank you in advace.
The ErrorException you're seeing is caused by the missing argument for the route you're trying to call. Your route is expecting server and playerId, wheres you're only sending it $greetInfo->greeting.
As you're using named route, my suggestion would be to use route() helper like so:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')
->name('greeting.update');
class PlayerProfileController extends Controller {
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
}
{{ Form::open(['action' => route('greeting.update', [$server, $playerId]), 'method' => 'post']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Please replace $server and $playerId variables with the ones representing the arguments you want to send with the request.
You can see I've also removed {{ Form::hidden('_method', 'PUT') }}, as this was trying to overwrite the post method on the Form::open method.

How do i separate two error message in laravel using one error blade file?

Say,i have two form in one page.I have included one error blade file bellow both of the form. Now when i make wrong in one form & submit it the error message is showing bellow the both form.Its normal.But my question is, how do i separate this two error message,how can i differentiate by giving them two different name?
Give this a try
return redirect()->back()->withErrors([
'form1.name' => 'name is required in Form 1',
'form1.email' => 'email is required in Form 1',
'form2.city' => 'city is required in form 2'
]);
in your view
#if($errors->any())
#foreach ($errors->get('form1.*') as $error) {
{{ $error }}
#endforeach
#endif
So you can group errors by form using array notation form.name and get all with $errors->get('form.*).
Read more about errors here: https://laravel.com/docs/5.4/validation#working-with-error-messages
If you're using Form Request Validation, you can change the errorBag property to get a unique array of errors for your view file.
In your Request file:
class MyFormRequest {
protected $errorBag = 'foobar';
public function rules() { // ... }
}
In your controller:
public function store(MyFormRequest $request) {
// Store entry.
}
Then in your view file:
#if ($errors->foobar->isNotEmpty())
// Work with the errors
#endif
You can use the named error bags.
$validator = Validator::make($request->all(), [
'field1' => 'required',
'field2' => 'required|digits:1',
]);
if ($validator->fails()) {
return back()
->withErrors($validator, 'form1error')
->withInput();
}
To print the error in blade file use-
#if(count($errors->form1error)>0)
<ul>
#foreach($errors->form1error->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
#endif

Missing required parameters for in a Update Form Laravel 5.2

I've been working on a webapp recently in laravel and i wanted to have a eddit function within tthe application. but im getting this error Missing required parameters for [Route: producten.update] [URI: producten/{producten}], and i dont know what i've done wrong.
This is the Routes im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);
This is the controller function im using for showing the edit page and updating.
The Edit function
public function edit(Request $request, Product $product)
{
// $product = Product::FindorFail($id);
// Product is a table with all products, with sellprice and buy price
// fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
The Update Function:
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
and this is the view i use for it.
{{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController#update', $model ]) }}
{{ Form::label('naam:')}}
{{ Form::text('naam') }} <br>
{{ Form::label('inkoopPrijs:')}}
{{ Form::text('inkoopPrijs') }} <br>
{{ Form::label('verkoopPrijs:') }}
{{ Form::text('verkoopPrijs') }} <br>
{{Form::label('Fabrieken', 'Fabrieken Id:') }}
{{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
{{ Form::submit('edit')}}
{{ Form::close() }}
if there is anything else that i need to add to the question just let me know and i'll add it
Missing thing is the id you are not getting id there in your edit function
your edit function should as i am assuming that you are just showing the form from this method where user can edit
public function edit($id)
{
$product = Product::FindorFail($id);
//Product is a table with all products, with sellprice and buy price
//fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
your update method should seem like this
public function update(Request $request, $id)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
your routes should like this no need for only
Route::resource('/producten', 'productionController');
edit route will be as
<a href="{{ route('production.edit', $model->id) }}">
Try this hope it will help

Resources