The error message I get is:
ErrorException (E_ERROR) Undefined variable: userdata
I know these things get asked a lot. I have searched Google and here and I have already tried suggested answers to no avail.
Controller:
public function index()
{
$userdata = EditUserModel::all()->paginate(5);
return view('admin.userman')->with(['userdata' => $userdata])->with('i', (request()->input('page', 1) - 1) * 5);
}
Route:
Route::resource('admin.userman', 'EditUserController');
Model:
public class EditUserModel extends Model
{
public $table = "users";
protected $fillable = [
'id',
'name',
'email',
'role',
'password'
];
}
View:
#foreach($userdata as $data)
<tbody>
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->created_at }}</td>
<td>{{ $data->role }}</td>
<td>{{ $data->status }}</td>
<td>
Edit
</td>
<td>
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</td>
</tr>
</tbody>
#endforeach
The output should be a table listing the contents of the users table from the database. but i'm getting undefined variable error.
You should try this:
Controller:
public function index()
{
$userdata = EditUserModel::all()->paginate(5);
$i = (request()->input('page', 1) - 1) * 5;
return view('admin.userman',compact('userdata','i'));
}
Route:
Route::resource('admin.userman', 'EditUserController');
Model:
public class EditUserModel extends Model
{
public $table = "users";
protected $fillable = [
'id',
'name',
'email',
'role',
'password'
];
}
View:
#if(isset($userdata))
#foreach($userdata as $data)
<tbody>
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->created_at }}</td>
<td>{{ $data->role }}</td>
<td>{{ $data->status }}</td>
<td>
Edit
</td>
<td>
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</td>
</tr>
</tbody>
#endforeach
#endif
public function index()
{
$userdata = EditUserModel::all()->paginate(5);
$page = array('i'=>(request()->input('page', 1) - 1) * 5);
return view('admin.userman',compact('userdata','page'));
}
Related
I have data like this
How to validate, if the user chooses same Kode, or user chooses a Kode that has conflicting Hari or Jam.
My Controller
public function store(Request $request)
{
$validate = Validator::make($request->all(), [
'kode_matkul' => 'required',
]);
if($validate->fails()) {
return response()->json(['errors' => $validate->errors()]);
}
}
My View Blade
<tbody>
#foreach ($mapel as $row)
<tr>
<td class="text-center">{{ $row->course }}</td>
<td>{{ $row->course_name }}</td>
<td class="text-center">{{ $row->sks }}</td>
<td class="text-center">{{ $row->kelompok }}</td>
<td>{{ $row->name }}</td>
<td class="text-center">{{ $row->hari }}</td>
<td class="text-center">{{ date('H:i', strtotime($row->start_time)) . ' - ' . date('H:i', strtotime($row->end_time)) }}</td>
<td class="text-center">{{ $row->ruangan }}</td>
<td class="text-center">
<input class="form-check-input" type="checkbox" name="kode_matkul[]" id="kode_matkul" value="{{ $row->course }}">
</td>
</tr>
#endforeach
</tbody>
Thank you
Please refer to https://laravel.com/docs/9.x/validation#form-request-validation for more information, but basically your steps would be -
create a store request for mapel (i guess?) with something like -
php artisan make:request StoreMapelRequest
In this newly create store request, you should create a rule for uniqueness to your desired fields -
public function rules()
{
return [
'Kode' => 'required|unique:mapels|max:255',
'Hari' => 'required|unique:mapels|max:255',
'Jam' => 'required|unique:mapels|max:255',
];
}
Edit: You can also add a simple |unique on your current store request, like this -
public function store(Request $request)
{
$validate = Validator::make($request->all(), [
'kode_matkul' => 'required|unique:mapels',
]);
if($validate->fails()) {
return response()->json(['errors' => $validate->errors()]);
}
}
Please notice that I assume that your table name is 'mapels', in which you are seeking this uniqueness rule.
I have done one-to-many relationships but when I loop through it in my view only one subject displays even when I have more than one subjects.
This is what I have done in my controller
$broadsheet = Broadsheet::with(['subject', 'session', 'term', 'student', 'classarm'])
->where('session_id', $request->sessions)
->where('term_id', $request->terms)
->where('class_arm_id', $request->classarms)
->selectRaw('sum(total) as totals, count(subject_id) as countrow, student_id, subject_id, session_id, term_id, class_arm_id, total')
->groupBy('student_id')
->orderBy('totals', 'desc')
->get();
return view('broadsheet.index')->with('broadsheet', $broadsheet);
This is the index view
#extends('layouts.app')
#section('content')
{{--#foreach($broadsheet as $subjects)
#foreach ($subjects->subject as $sub)
<th> {{ strtoupper($sub->subject->subject_name) }}</th>
#endforeach
#endforeach--}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
#foreach($broadsheet as $subject)
<th> {{ strtoupper($subject->subject->subject_name) }}</th>
#endforeach
<th>TOTAL</th>
<th>AVERAGE</th>
<th>POSITION</th>
</tr>
</thead>
<tbody>
#foreach($broadsheet as $result)
<tr>
<td>{{ $result->total }}</td>
<td>{{ $result->totals }}</td>
<td>{{ number_format($result->totals/$result->countrow,2) }}</td>
<td>{{ $i++ }}</td>
<td>{{ $result->exams }}</td>
<td>{{ $result->total }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
and this is my Broadsheet schema
class Broadsheet extends Model
{
use HasFactory;
protected $fillable = [
'student_id',
'subject_id',
'session_id',
'term_id',
'class_arm_id',
'subject_total',
];
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
public function session()
{
return $this->belongsTo(Session::class, 'session_id');
}
public function term()
{
return $this->belongsTo(Term::class, 'term_id');
}
public function classarm()
{
return $this->belongsTo(ClassArm::class, 'class_arm_id');
}
}
I want to display students results like this
Intended result
Please, I would really need assistance to get this working as any help would be greatly appreciated.I just started learning laravel recently. I don't really know where the issue is coming from
I'm trying to fetch value from different tables. I get the value but it repeat same quantity. I'm getting this see this image
I expect this see this image
Here is my controller code
$orders = Order::all();
view('admin.order.index', compact('orders'));
Here is my model relationship
return $this->belongsToMany(FoodItem::class, 'order_food_items', 'order_id', 'food_item_id')->withTimestamps();
}
public function orderItems() {
return $this->hasMany(OrderFoodItem::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Here is in blade code, May be I'm doing wrong
#foreach ($orders as $key => $order)
#foreach ($order->orderFoodItems as $product)
#foreach ($order->orderFoodItemPrice as $price)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $product->name }}</td>
<td>
<img src="{{ asset('/storage/items/food/' . $product->image) }}"
alt="">
</td>
<td>{{ $order->user->name }}</td>
<td>
<span class="badge badge-primary">Pending</span></td>
<td>something</td>
<td>{{ $price->discounted_price }}</td>
</tr>
#endforeach
#endforeach
#endforeach
Can anyone help me?
i got error and i don't know what the error is this
in my controller
public function exportExcel(Request $data)
{
return Excel::download(new VgetAttGeneralExport($data), 'rekap-perkelas.xlsx');
}
here's my request
0:{keyid: 6046, att_seq: 6046, att_enumber: "101415287", att_method: null, att_workcode: null,…}
in my export folder
namespace App\Exports;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
class VgetAttGeneralExport implements FromView
{
use Exportable;
public $data;
/**
* #return \Illuminate\Support\Collection
*/
public function __construct($data) {
$this->data = $data;
}
public function view(): View
{
$data = $this->data;
return view('exports.Kehadiran',compact('data'));
}
}
and this my view
<table>
<tbody>
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->dated }}</td>
<td>{{ $item->employee_id }}</td>
<td>{{ $item->employee_name }}</td>
<td>{{ $item->tingkat }}{{$item->div_name}}{{$item->kelas}}</td>
<td>{{ $item->stat_name }}</td>
<td>{{ $item->stat_desc }}</td>
</tr>
#endforeach
</tbody>
</table>
i've tried many way and i got stuck, anyone please help me, i'am very beginner in api laravel
i new in laravel development,
i have code problem.
i have MotorsController.php like this
class MotorsController extends BaseController{
protected $motor;
public function __construct(Motor $motor){
$this->motor = $motor;
}
public function index(){
$motors=$this->motor->with('motorcategory', 'motorcolor')->get();
return View::make('motors.index', compact('motors'));
}
and i have model like below
Motor.php
<?php
class Motor extends Eloquent {
protected $table = 'motors';
protected $guarded = array();
public function motorcategory(){
return $this->belongsTo('MotorCategory');
}
public function motorcolor(){
return $this->belongsTo('MotorColor');
}
public static $rules = array(
'name' => 'required',
'police_number' => 'required',
'sex' => 'required'
);
}
and MotorCategory.php
<?php
class MotorCategory extends Eloquent {
public function motor(){
return $this->hasMany('Motor');
}
}
and MotorColor.php
class MotorColor extends Eloquent {
public function motor(){
return $this->hasMany('Motor');
}
}
in motors.index i want to get motor data which have relation with table motorcolor and motorcategory,
but now i still have error like:
ErrorException (E_UNKNOWN)
Trying to get property of non-object (View: C:\Users\LalatTempur\Laravel\ironhorse\app\views\motors\index.blade.php)
i hope someone can help me..
by the way,, sorry for my english :)
my motors.index
<tbody>
#foreach ($motors as $motor)
<tr>
<td>{{ $motor->name }}</td>
<td>{{ $motor->police_number }}</td>
<td>{{ $motor->motor_category->name }}</td>
<td>{{ $motor->motor_color->name }}</td>
<td>{{ $motors->purchase_date}}</td>
<td>{{ $motors->status }}</td>
<td>
{{ link_to_route('motors.show', 'Detail', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-primary btn-xs')) }} |
{{ link_to_route('motors.edit', 'Edit', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-info btn-xs')) }} |
<!-- {{ link_to_route('members.destroy', 'Delete', array($member->id), array('role' =>'btn', 'class' => 'btn btn-danger btn-xs')) }} -->
<!-- </td>
<td> -->
{{ Form::open(array('method' => 'DELETE', 'route' => array('motors.destroy', $motor->id), 'style'=>'display:inline;' )) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger btn-xs ')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
You are accidently using the plural of motor inside your foreach:
<td>{{ $motors->purchase_date}}</td>
<td>{{ $motors->status }}</td>
Should be:
<td>{{ $motor->purchase_date}}</td>
<td>{{ $motor->status }}</td>
Although the error message doesn't really fit well, because a collection would be an object too.