I'm trying to retrieve data from database into my Laravel CRUD, but it says undefined variable in my view.
Here's the view frame.blade.php
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
Here's the controller
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$mhs = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame')->with('frame', $mhs);
}
And here's the route
Route::group(['prefix' => 'mahasiswa'], function () {
Route::get('/dashboard/{id}', [
'uses' => 'DashboardController#show',
'as' => 'mahasiswa.show',
]);
Route::post('/dashboard/', [
'uses' => 'DashboardController#store',
'as' => 'mahasiswa.store',
]);
Route::put('/dashboard/{id}', [
'uses' => 'DashboardController#update',
'as' => 'mahasiswa.update',
]);
Route::delete('/dashboard/{id}', [
'uses' => 'DashboardController#destroy',
'as' => 'mahasiswa.destroy',
]);
});
I keep getting "Undefined variable: mahasiswa" on my view
Does anyone know the reason for this?
You are passing a data called frame and trying to iterate over $mahasiswa, so either change your blade to this:
#foreach($frame as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
or in your controller:
public function index(Request $request)
{
$mhs = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame')->with('mahasiswa', $mhs);
}
Try this way:
First in the controller assign your DB query results to $mahasiswa:
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$mahasiswa = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame', compact('mahasiswa'));
}
Then in the view:
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
It will get rid of that error.
I used return view('frame', compact('mahasiswa')); i.e compact instead of with. I think it's a lot cleaner and less confusing for starters.
it can still work this way:
use App\Dashboard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class DashboardController extends Controller
{
public function index(Request $request)
{
$data['mahasiswa'] = Dashboard::orderBy('id', 'desc')->paginate(5);
return view('frame',$data);
}
view
#foreach($mahasiswa as $mhs)
<tr>
<td>{{$mhs->id}}</td>
<td>{{$mhs->nim}}</td>
<td>{{$mhs->nama}}</td>
<td>{{$mhs->alamat}}</td>
<td>{{$mhs->fakultas}}</td>
<td><a onclick="event.preventDefault();editmhsForm({{$mhs->id}});" href="#" class="edit open-modal" data-toggle="modal" value="{{$mhs->id}}"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a onclick="event.preventDefault();deletemhsForm({{$mhs->id}});" href="#" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
#endforeach
Related
i'm new to laravel. So in this case i'm trying to sum child data and pass it to parent nominal column inside parent index #foreach,
the thing is idk how to declare the parent id inside controller
here's my parent index which i want to show the sum child each parent id in #foreach
here's my child index that i've already sum it, i want to show it into parent #foreach nominal column as well
here's my SPJCreateController which is parent controller
class SPJCreateController extends Controller
{
public function index(Request $request, $id)
{
$kegiatan = DPAKegiatan::with('dpatahun')->findorfail($id);
$data = SPJCreate::with('spjcreatedetail')
->where('id_dpakegiatan', $kegiatan->id)
->orderBy('created_at', 'asc')
->paginate(10);
$total = SPJCreateDetail::where('id_spj', $request->id)->sum('nominal');
return view('spj.kegiatan.create.data', compact('kegiatan','data','total'));
}
}
in this controller i declare variable $total which is gonna sum column 'nominal' in SPJCreateDetail Model but idk how to declare where inside of it since $id is the id of grandparent
It returns 0 inside nominal column if i put $request->id inside where
however it shown when i call it manually like this
$total = SPJCreateDetail::where('id_spj', 6)->sum('nominal');
my parent index after i put where id manually
But i don't want that because i want my id to be dynamically called and sum eachrow of it
My SPJCreate Model which is parent model
class SPJCreate extends Model
{
protected $primaryKey = 'id';
protected $table = 'spj';
protected $fillable = ['id_dpakegiatan','tipebelanja','tipebelanjadetail'];
public function dpakegiatan()
{
return $this->belongsTo(DPAKegiatan::class, 'id_dpakegiatan');
}
public function spjcreatedetail()
{
return $this->hasMany(DPAKegiatan::class, 'id');
}
}
My SPJCreateDetail model which is child model
class SPJCreateDetail extends Model
{
protected $primaryKey = 'id';
protected $table = 'spj_detail';
protected $fillable = ['id_spj','id_listbelanja','ket','penerima','nominal','ppn','pph21','pph22','pph23'];
public function spjcreate()
{
return $this->belongsTo(SPJCreate::class, 'id_spj');
}
}
Sorry for my bad english i hope u guys can help me because im stuck for a month trying to figure it out :( , Thank you.
i dd inside my controller
$total = dd(SPJCreateDetail::where('id_spj', $request->id)->sum('nominal'));
it returns like this
image
however
$total = dd(SPJCreateDetail::where('id_spj', 6)->sum('nominal'));
it return the value correctly
image
here's my web routes
// --Here's my grandparent routes-- //
Route::get('/spj/kegiatan&id={spjkeg}', 'SPJKegController#index')->name('spjkeg')->middleware('auth');
// ------------- //
// --Here's my Parent Route Which is Gonna show my child sum-- //
Route::get('/spj/kegiatan/create&id={spjcreate}', 'SPJCreateController#index')->name('spj')->middleware('auth');
Route::post('/spj/kegiatan/create/store&id={spjcreate}', 'SPJCreateController#store')->name('spj.store')->middleware('auth');
Route::post('/spj/kegiatan/create/updt&id={spjcreate}', 'SPJCreateController#update')->name('spj.update')->middleware('auth');
Route::delete('/spj/kegiatan/create/del&id={spjcreate}', 'SPJCreateController#destroy')->name('spj.destroy')->middleware('auth');
// ------------ //
// --Here's the child route-- //
Route::get('/spj/kegiatan/create/detail&id={spjdet}', 'SPJCreateDetailController#index')->name('spj.detail')->middleware('auth');
Route::post('/spj/kegiatan/create/detail/store&id={spjdet}', 'SPJCreateDetailController#store')->name('spj.detail.store')->middleware('auth');
Route::post('/spj/kegiatan/create/detail/updt&id={spjdet}', 'SPJCreateDetailController#update')->name('spj.detail.update')->middleware('auth');
Route::delete('/spj/kegiatan/create/detail/del&id={spjdet}', 'SPJCreateDetailController#destroy')->name('spj.detail.destroy')->middleware('auth');
// ------------ //
Here's my View.blade.php
#php $no = 0; #endphp
#forelse($data as $spj => $result)
#php $no++; #endphp
<tr>
<td class="text-center">{{ $spj + $data->firstitem() }}.</td>
<td class="text-center"><b>{{ $result->tipebelanjadetail }}</b></td>
<td class="text-center">{{ $total }}</td>
<td class="text-center">
<div class="dropdown">
<button data-id="{{ $result->id }}" data-idtipebelanja="{{ $result->tipebelanja }}" data-idtipebelanjadetail="{{ $result->tipebelanjadetail }}" data-toggle="modal" data-target="#editspj" class="btn btn-success btn-s" style="background-color:#138496;border-color:#138496"><i class="fad fa-pencil"></i></button>
<button data-id="{{ $result->id }}" data-toggle="modal" data-target="#deletespj" class="btn btn-success btn-s" style="background-color:#eb1c0f;border-color:#eb1c0f"><i class="fad fa-trash"></i></button>
<button type="button" data-toggle="dropdown" class="btn btn-warning dropdown dropdown-toggle" aria-haspopup="true" aria-expanded="false"><i class="fad fa-info"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="{{ Route('spj.detail', $result->id)}}"><i style="padding-right:5px" class="fad fa-eye"></i>Detail</a>
</div>
</div>
</td>
</tr>
#empty
<tr>
<td colspan="7" class="text-center">Tidak ada data <i class="far fa-frown fa-5"></i></td>
</tr>
#endforelse
Try this.
public function index($spjcreate)
{
$kegiatan = DPAKegiatan::with('dpatahun')->findorfail($spjcreate);
$data = SPJCreate::with('spjcreatedetail')
->where('id_dpakegiatan', $kegiatan->id)
->withCount(['spjcreatedetail AS total' => function ($query) {
$query->select(DB::raw('SUM(nominal) as total'));
}])
->orderBy('created_at', 'asc')
->paginate(10);
return view('spj.kegiatan.create.data', compact('kegiatan','data'));
}
#php $no = 0; #endphp
#forelse($data as $spj => $result)
#php $no++; #endphp
<tr>
<td class="text-center">{{ $spj + $data->firstitem() }}.</td>
<td class="text-center"><b>{{ $result->tipebelanjadetail }}</b></td>
<td class="text-center">{{ $result->total }}</td>
<td class="text-center">
<div class="dropdown">
<button data-id="{{ $result->id }}" data-idtipebelanja="{{ $result->tipebelanja }}" data-idtipebelanjadetail="{{ $result->tipebelanjadetail }}" data-toggle="modal" data-target="#editspj" class="btn btn-success btn-s" style="background-color:#138496;border-color:#138496"><i class="fad fa-pencil"></i></button>
<button data-id="{{ $result->id }}" data-toggle="modal" data-target="#deletespj" class="btn btn-success btn-s" style="background-color:#eb1c0f;border-color:#eb1c0f"><i class="fad fa-trash"></i></button>
<button type="button" data-toggle="dropdown" class="btn btn-warning dropdown dropdown-toggle" aria-haspopup="true" aria-expanded="false"><i class="fad fa-info"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="{{ Route('spj.detail', $result->id)}}"><i style="padding-right:5px" class="fad fa-eye"></i>Detail</a>
</div>
</div>
</td>
</tr>
#empty
<tr>
<td colspan="7" class="text-center">Tidak ada data <i class="far fa-frown fa-5"></i></td>
</tr>
#endforelse
I create edit page to edit the form. When I debug it. It's showing error which is Missing required parameters. I already try many ways but i can't solve it. Anyone can help on this?
<td class="text-right">
<a href='{{ route("email.edit",["id"=>$mailTemplate->id]) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
route file
Route::get('api/email/create', ['as' => 'email.create', 'uses' => 'Havence\AutoMailController#create']);
Route::get('automail/mail', 'Havence\AutoMailController#mail');
Route::get('automail/index',['as'=>'email.index','uses' => 'Havence\AutoMailController#index']);
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Route::get('automail/delete',['as'=>'email.delete','uses' => 'Havence\AutoMailController#destroy']);
Controller
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit')->with('mailTemplates', $mailTemplates);
}
You could do the following:
<td class="text-right">
<a href='{{ route("email.edit", $mailTemplate) }}' class="btn btn-danger badge-pill editbtn" style="width:80px" >EDIT </a>
</td>
The route:
Route::get('automail/edit/{id}',['as'=>'email.edit','uses' => 'Havence\AutoMailController#edit']);
Then in the controller you can get it:
public function edit(AutoEmailTemplate $mailTemplates , $id)
{
$mailTemplates=AutoEmailTemplate::find($id);
return view('havence.marketing.edit', compact('mailTemplates'));
}
I don't know why I'm getting 404 not found error when I'm trying to pass an id to another controller.
here's my index.blade.php
<a class="btn btn-sm btn-default" href="{{ route('receiving_details',
['id'=>$r_main->id])}}" title="Show Received Data"><i class="fa fa-arrow
right"</i></a>
web.php
Route::get('receiving_details/{$id}',[
"uses" => 'ReceivingDetailsController#index',
"as" => 'receiving_details'
]);
ReceivingDetailsController.blade.php(This is where I want to pass id from view.blade.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ReceivingDetailsController extends Controller
{
public function index($id){
echo $id;
}
}
Just remove $ from your get request.
Route::get('receiving_details/{id}',[
"uses" => 'ReceivingDetailsController#index',
"as" => 'receiving_details'
]);
In your href please have this little adjustment
<a class="btn btn-sm btn-default" href="{{ route('receiving_details', $r_main->id)}}" title="Show Received Data"><i class="fa fa-arrow right"</i></a>
I'm trying to update my 'pay' record in the DB after pressing a button in a table row, but it shows this error when pressing the button:
ErrorException in 0db55b8fee884912074e1a4700061051aeb18bcc.php line
15: Undefined variable: users (View:
/Users/mauricio/code/cnr/resources/views/pages/users.blade.php)
I don't know why it gives me that error when I have use the users variable before in the blade file.
Here is my Registration Controller:
<?php
namespace App\Http\Controllers;
use DB;
use App\User;
class RegistrationController extends Controller
{
public function updatePay(User $id)
{
DB::table('users')->where('id', $id)->update(['pay' => 1]);
return view('pages.users');
}
}
Welcome controller:
public function usersAdmin()
{
$users = DB::table('users')->get();
return view('pages.users', compact('users'));
}
Routes file:
// for admin only
Route::get('/users', 'WelcomeController#usersAdmin');
Route::post('/users/{id}', 'RegistrationController#updatePay');
Here is my view
#extends('layouts.master')
#section('content')
<table class="highlight">
<thead>
<tr>
<th data-field="id" hidden="">ID</th>
<th data-field="name">Nombre</th>
<th data-field="last_name">Apellidos</th>
<th style="text-align: right;">Acciones</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->last_name}}</td>
<td style="text-align: right; width: 30em">
#if( $user->isAdmin )
{{"ADMINISTRADOR"}}
#elseif( $user->pay )
<a class="waves-effect waves-light btn yellow darken-2"><i class="material-icons left" style="margin:0">create</i></a>
<a class="waves-effect waves-light btn red darken-1"><i class="material-icons left" style="margin:0">delete</i></a>
#else
<form method="POST" action="/users/{{$user->id}}">
{{ csrf_field() }}
<button type="submit" class="waves-effect waves-light btn green lighten-1"><i class="material-icons left" style="margin:0">done</i></button>
</form>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
#endsection
You need to retrieve users and send it to the view similar like this:
Controllers:
public function methodName()
{
$users = User::all();
return view('path.to.view')->with(compact('users'));
}
Also, You have two choice to use:
First
public function updatePay($id)
Second
public function updatePay(User $user)
{
DB::table('users')->where('id', $user->id)->update(['pay' => 1]);
and instead of return view('pages.users'); use return redirect()->url('users'); in the updatePay method.
I'm unable to pass my controller variable $sickLeaves to my view, which is reached via index.blade.php of my SickLeaveController (which is a RESTful resource controller).
I keep getting the error: Undefined variable 'sickLeaves'
Here is the routes.php:
Route::group(['middleware' => 'web'], function(){
Route::auth();
Route::get('/', 'HomeController#index');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
Route::get('/home', 'HomeController#index');
Route::resource('sickleaves','SickLeaveController');
});
Here is my SickLeaveController:
class SickLeaveController extends Controller
{
public function index()
{
$sickLeaves = SickLeave::all();
return view('sickleaves.index')->with('sickleaves',$sickLeaves);
}
public function create()
{
$users = User::pluck('name','id');
return View::make('sickleaves.create')->with('sickleaves',$users);
}
public function store(Request $request)
{
$this->validate($request, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'reason' => 'required|max:255',
'startdate' => 'required',
'status' => 'required',]);
}
public function show($id)
{
$sickLeave = SickLeave::find($id);
return View::make('sickleaves.show')->with('sickleaves',$sickLeave);
}
public function edit($id)
{
$sickLeave = SickLeave::findOrFail($id);
return View::make('sickleaves.edit')->with('sickleaves',$sickLeave);
}
public function update(Request $request, $id)
{
//TODO
}
public function destroy($id)
{
$sickLeave = SickLeave::findOrFail($id);
$sickLeave->delete();
Session::flash('Item successfully deleted');
return redirect()->route('sickleave.index');
}
}
Here is the index.blade.php:
#foreach($sickLeaves as $sickLeave)
<tr>
<td style="width: 8%">{{ $sickLeave->id }}</td>
<td style="width: 12%"><span class="fw-semi-bold">{{ $sickLeave->firstname }}</span></td>
<td style="width: 12%"><span class="fw-semi-bold">{{ $sickLeave->surname }}</span></td>
<td class="no-sort hidden-xs" style="width: 12%">{{ $sickLeave->reason }}</td>
<td class="hidden-xs">{{ $sickLeave->startdate }}</td>
<td class="hidden-xs">{{ $sickLeave->enddate }}</td>
<td class="no-sort">{{ $sickLeave->status }}</td>
<td class="no-sort"><div class="btn-group">
<button class="btn btn-inverse">Actions</button>
<button class="btn btn-inverse dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-caret-down"></i>
</button>
<ul class="dropdown-menu">
<li>View Details</li>
<li>Edit</li>
<li class="divider"></li>
<li>Delete</li>
</ul>
</div>
</td>
</tr>
#endforeach
Use correct variable:
#foreach($sickleaves as $sickLeave)
Also, remove web middleware if you're using 5.2.27 or later.
PHP variables are case sensitive!
You passing sickleaves, but in view You are trying to get sickLeaves, that is the problem.