Undefined variable: no_marks (laravel 5.3) - php

I am new to Laravel and I am getting following error Undefined variable: no_student (Laravel 5.3)
Here is my view. blade
#foreach($no_marks as $no_mark)
<tr class="gradeX odd" role="row" id="">
<td style="display:none"></td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes checkbox-index" value="">
<span></span>
</label>
</td>
<td></td>
<td></td>
<td></td>
<td>
#endforeach
my index method in controller
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index');
}
I am storing record using this store method
public function store(Request $request)
{
$this->validate($request, [
'ip_range' => 'required|max:255',
]);
$ip_data['ip_range'] = $request->input('ip_range');
$ip_data['min_ip'] = $request->input('ip_range');
$ip_data['max_ip'] = $request->input('ip_range');
$ip_data['list_id'] = $request->input('list_id');
$ip_data['user_id'] = Auth::user()->id;
$no_marks = Markno::create($ip_data);
session()->flash('msg', ' Successfully created');
return view('no-mark.index');
}
I don't know where I am going wrong. Please help to fix it.

You did not passed your variable $no_marks into your view that's why it is undefined to passed it
return view('no-mark.index', array('no_marks' => $no_marks));

To use a variable available on controller you have to pass it on view like:
return view('no-mark.index', array('no_marks' => $no_marks));
here the second parameter is the array, you can access its index on view page

please update your action
public function index()
{
//
$no_marks = Markno::orderBy('id', 'desc')->get();
return view('no-mark.index',compact(['no_marks']));
}

Related

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id. It says the problem is in my index

Hello I have a problem with Paginator dont know how I can make it work.
This is my ReportsController:
public function index()
{
//Show all created reports
$reports = Reports::orderBy('created_at', 'desc')->paginate(15);
return view('reports.index', ['reports'=>compact('reports')])->with(['reports', $reports]);
}
And this is the index.blade.php:
#foreach($reports as $report)
<tr>
<td scope="row"><a href="/reports/show">{{$report->id}}</td>
<td>{{$report->title}}</td>
<td>{{$report->category}}</td>
<td>{{$report->name}}</td>
<td>{{$report->created_at}}</td>
<td>{{$report->updated_at}}</td>
</tr>
#endforeach
return view('reports.index', ['reports' => $reports]);
// or
return view('reports.index', compact('reports'));
What you had was this:
['reports' => ['reports' => $reports]]
If you were going to use with it would be:
...->with('reports', $reports);
// or
...->with(['reports' => $reports]);

Sending data from Controller to View in Laravel 7

I want to send my data from controller to xedit.blade.php, but I get the same error:
Undefined variable: users
in controller:
public function index3()
{
$users=User::all();
return view('xedit')->with('users' => $users);
}
Routes:
Route::get('/index3','Admin\UsersController#index3');
and I want to use $users in blade.Maybe there is a Route problem?
in your index method
public funtion index()
{
$users=User::all();
return view('xedit', compact('users'));
}
in your view add $users
<table>
#foreach ($users as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
</tr>
#endforeach
</table>
Your code logic is perfect, I guess you have to use proper naming with your routes because of Laravel Standard.
Route::get('/admin/show','Admin\UsersController#index')-name('admin.show');
public function index()
{
$users = User::all();
return view('xedit')->with('users' => $users);
}
In view, blade use a professional approach like below
#isset($users)
... loop ...
#endisset()
check record before sending to view by using dump and die function dd($users);
Want to comment but doesn't have 50 reputation
Replace ('users' => $users); this with (['users' => $users]); as you are using =>

Undefined variable: libros (View: C:\Users\CDS22\Desktop\Crud\resources\views\libros\index.blade.php) on laravel

I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}

Undefined variable issue Laravel 5.4

I've got stuck with this error so if ever pls forgive me because I'm still new at laravel. I got this error
Undefined variable: clientTransactions (View:
C:\xampp\htdocs\dcgwapo\resources\views\service_details\create.blade.php)
but I have a right code but I still wondering why it is still undefined variable given I define it in my controller.
create.blade.php in service details code
<div class="form-group">
<label for="client_transaction_id">Client Trans ID: </label>
<select class="form-control" name="client_transaction_id">
#foreach ($clientTransactions as $clientTransaction)
<option value= "{{ $clientTransaction->id }}">
{{ $clientTransaction->id }}
</option>
#endforeach
</select>
</div>
ServiceDetailsController code
public function create()
{
$users = User::pluck('fname', 'lname', 'id');
$services = Service::pluck('name', 'id');
$clientTransactions = ClientTransaction::all();
return view('service_details.create', ['users' => User::all()], ['services' => Service::all()], ['clientTransactions' => ClientTransaction::all()]);
}
ServiceDetail.php model code
public function clientTransaction()
{
return $this->belongsTo(ClientTransaction::class);
}
I hope you can help me. Thanks!
You're sending variables to your view the wrong way. The seconds argument should be an array with all your variables. As of now your adding a new parameter to the view function for each variable.
view('view', [...], [...], [...])
It should be like this:
view('view', [...1, ...2, ...3])
So what you need to change is the return statement to this:
return view('service_details.create', ['users' => User::all(), 'services' => Service::all(), 'clientTransactions' => ClientTransaction::all()]);
Second parameter to view function accepts an associative array of data, you are passing an indexedArray of arrays, Just use this return statement and you are good to go. ;)
return view('service_details.create', [
'users' => User::all(),
'services' => Service::all(),
'clientTransactions' => ClientTransaction::all()
]);
You can use compact to pass data from controller to view:
public function create()
{
$users = User::pluck('fname', 'lname', 'id');
$services = Service::pluck('name', 'id');
$clientTransactions = ClientTransaction::all();
return view('service_details.create',compact('users','services','clientTransactions');
}

Keep getting error: implode(): Invalid arguments passed when updating data

I keep on getting this error whenever I try to update something into my data. It didn't happen a lot the last time but after putting the new update function, the error keep popping out and for some reason it points back to the new function that I had entered even though I am updating at the old function.
Controller:
//update for user
public function edit($id){
$object = user::find($id);
return view('edit', compact('object'));
}
public function update(Request $request, $id){
$object = user::find($id);
$object->Name = $request->input('Name');
$object->update();
return redirect('/home');
}
//update for Schools table
public function edit1($id){
$object2 = school::find($id);
return view('edit1', compact('object2'));
}
public function update1(Request $request, $id){
$object2 = school::find($id);
$test = array();
$test['School'] = implode(' , ', $request->School);
$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$object2->update($test);
return redirect('/home');
}
//error start here after putting this whole thing in. (I tried putting it into another separate controller but the error still continues)
public function edit2($id){
$object3 = hobby::find($id);
return view('edit2', compact('object3'));
}
public function update2(Request $request, $id){
$object3 = hobby::find($id);
$test2 = array();
$test2['computer_game'] = implode(' , ', $request->computer_game);
$test2['reading_book'] = implode(' , ', $request->reading_book);
$object3->update($test2);
return redirect('/home');
}
Error is highlighting this part even though when I try to update user or school data
$test2['computer_game'] = implode(' , ', $request->computer_game);
And it says
:implode(): Invalid arguments passed
I have no issue updating the hobby data but it the error keep pointing it back to the hobby implode part.
Is it possible I could only use update once on an implode function? Thanks in advance
edit2.blade.php (edit page for hobby, as shown it is an array)
<form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object3->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Sports:
</th>
<th class="text-center">
Books read:
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='computer_game[]' class="form-control"/>
</td>
<td>
<input type="text" name='reading_book[]' class="form-control"/>
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Save">
Just simple try the following:
$test2['computer_game'] = implode(' , ', (array)$request->computer_game);
This will convert $request->computer_game in to an array if it is not already.
You are getting the error because $request->computer_game is not an array. This can be verified by dumping the variable and killing the script.
var_dump($request->computer_game); die;
first of all what does var_dump(); gives to that object oriented array. plz share then we'll try to answer.
Then to stop multiple loading you can make a private var ($loaded) and by default it would be falseprivate static $loaded = false; then befor any code starts in function use if statement to detect if loaded is true
if(self::$loaded){
return;
}
self::$loaded = true
this would help you! please post var_dump!

Categories