Call to a member function links() on array in Laravel - laravel

I am receiving this error while trying to create pagination in laravel.
ErrorException in 1e886a45102a3e4898f23b52cd7ca771 line 396: Call to a
member function links() on array (View:
C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\setting.blade.php)
line 396: <?php echo e($themes->links()); ?>
HomeController.php
public function getBackgroundTheme()
{
$query = DB::table('theme_background');
if (request()->has('menu')) {
$theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->first();
$query = $query->where('kategori_id', $theme->kategori_id);
}
//$model = $query->get();
$theme = $query->paginate(4);
return view('soulfy.setting', [
'themes'=>$theme,
'user' => auth()->user(),
]);
// return redirect('/home/setting' .
}
setting.blade.php
<table>
#if(isset($themes))
#foreach($themes as $m)
<tr>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
<td><img width="100px" height="100px" src="{{url('/')}}/uploads/theme/{{$m->pic_name}}.jpg"/></td>
</tr>
<tr>
<td><div class="box"><input type="checkbox" name="pic" value=""></div></td>
<td><input type="checkbox" name="pic" value=""></td>
<td><input type="checkbox" name="pic" value=""></td>
</tr>
#endforeach
{{ $themes->links() }}
#endif
</table>

$theme = DB::table('kategori_name')->where('kategori_theme', request('menu'))->paginate(PAGELIMIT);
Use This In Your Home Controller. Instead Of first();
And Define This In Your Routes : define('PAGELIMIT','4');

Related

Laravel Livewire Edit Role Spatie

Help, I work with laravel and spatie for roles and permissions.
I want to edit my permissions
with a checkbox list
from a window Attached code:
En el componente:
public $role;
public $rol_id, $name;
public $chek = [];
public function mount(Role $role)
{
$this->role = $role;
$this->data = $role->toArray();
$this->chek = $this->role->permissions()->pluck('id')->toArray();
}
protected $rules =[
'role.name' => 'required'
];
public function render()
{
$permisos = Permission::all();
return view('roles.edit-rol-component', compact('permisos'));
}
public function update(){
$role = $this->role;
$this->validate([
'role.name' => 'required',
]);
$this->role->save();
$role->syncPermissions(['permission'=> $this->chek]);
$this->emit('alert','success','El rol se editó correctamente.');
$this->closeModalWithEvents([
RolComponent::getName() => 'renderEvent']);
}
In the view, the list generator shows me but it marks the checks wrong
#foreach($permisos as $key => $value)
<label class="inline-flex items-center">
<input wire:model.defer="chek.{{$value->id}}"
type="checkbox" value="{{ $value->id }}"
class="form-checkbox h-4 w-4 text-green-500"
#if($role->permissions->contains($value->id)) checked #endif >
<span class="ml-3 text-sm">{{ $value->name }} </span>
</label>
#endforeach
enter image description here
I am doing the edit / delete this way (as shown in the image)
Blade
<table class="table card-table table-vcenter text-nowrap table-hover table-striped datatable fs-5">
<thead>
<tr>
<th>#</th>
<th></th>
<th>Role</th>
<th></th>
<th>Permissions</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($roles as $id => $single)
#php
$badgeColor = ['badge bg-blue-lt',
'badge bg-azure-lt',
'badge bg-purple-lt',
'badge bg-pink-lt',
'badge bg-indigo-lt',
'badge bg-red-lt',
'badge bg-orange-lt',
'badge bg-cyan-lt',
'badge bg-yellow-lt',
'badge bg-green-lt'
];
#endphp
<tr>
<td>{{ $id + 1 }}.</td>
<td></td>
<td>{!! strtoupper($single->label) !!}</td>
<td></td>
<td>
<div class="btn-list">
#foreach ($single->permissions as $permission)
<span class="{{ $badgeColor[rand(0, 9)] }} fs-5">{{ $permission->label }} <span class="ps-1" style="cursor: pointer" wire:click='revokePermission({{ $single->id }}, {{ $permission->id }})'>x</span></span>
#endforeach
</div>
</td>
<td class="text-end">
<button class="btn btn-danger p-1 fs-6"><i class="fas fa-trash-alt"></i></button>
</td>
</tr>
#endforeach
</tbody>
</table>
Component:
public function revokePermission($roleId, $permissionId)
{
$role = Role::findOrFail($roleId);
$permission = ModelsPermission::findOrFail($permissionId);
$role->revokePermissionTo($permission->name);
$this->render();
}

How to update when selecting select option will show checkbox in livewire

I'm having a problem I've never experienced before, I'm doing a fix function for a prescription.
I already added it but one problem when I fixed it.
I use livewire so that when I click on the drug it will show the potion by drug
Livewire Class
<?php
namespace App\Http\Livewire;
use App\Models\MedicineCategory;
use App\Models\Medicine;
use App\Models\Prescription;
use Livewire\Component;
class SelectedOldMedicineCategoryTable extends Component
{
public $columns = ['STT', 'Name', 'Quantity', 'Unit'];
public $medicines;
public $oldMedicines;
public $oldMedicineCategory;
public $prescription;
public array $selectedMedicineArray = [];
public $getAllMedicineCategory;
public $selectMedicineCategoryId = null;
public $quantity = [];
public $checkbox = false;
public function render()
{
$this->getAllMedicineCategory = MedicineCategory::where('status', MedicineCategory::ACTIVE)->get(['id', 'name_medicine']);
if ($this->selectMedicineCategoryId != '') {
$this->medicines = Medicine::where('status', Medicine::ACTIVE)->Where('medicine_category_id', $this->selectMedicineCategoryId)->get(['id', 'name', 'unit']);
} else {
$this->medicines = [];
}
return view('livewire.selected-old-medicine-category-table');
}
}
View Blade
<div>
<select name="medicine_category_id" id="" wire:model="selectMedicineCategoryId" class="form-select io-select2">
<option value="">Select Medicine Category</option>
#foreach ($getAllMedicineCategory as $row)
<option value="{{ $row->id }}"#if($prescription->medicines->containsStrict('id', $row->id)) selected="selected" #endif>
{{ $row->name_medicine }}</option>
#endforeach
</select>
#if (!empty($medicines))
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th></th>
#foreach (collect($columns) as $column)
<th>{{ $column }}</th>
#endforeach
</tr>
</thead>
<tbody>
#forelse ($medicines as $item)
<tr>
<td><input class="form-check-input" type="checkbox" name=""
wire:model="selectedMedicineArray" id="" value='{{ $item }}'>
<td>{{ $loop->iteration }}</td>
</td>
<td>{{ $item->name }}</td>
<td><input type="number" wire:model="quantity.{{ $item->id }}" id=""
min="1" value="1" class="form-control">
</td>
<td>{{ $item->unit }}</td>
</tr>
#empty
<tr>
<td>Not exists medicine</td>
</tr>
#endforelse
</tbody>
</table>
View
enter image description here
enter image description here
i want when i fix it, it will show up like that and check the checkbox for me

Returning wrong id in laravel

Departemen =
'nama_departemen',
'nama_manager',
'jumlah_pegawai',
Pegawai = 'nomor_induk_pegawai',
'nama_pegawai',
'id_departemen',
'email',
'telepon',
'gender',
'status'
PegawaiController
public function index()
{
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
return view ('pegawai.index', compact('pegawai'));
}
public function destroy($id)
{
Pegawai::find($id)->delete();
return redirect()->route('pegawai.index')->with(['success'=> 'Item Berhasil Dihapus']);
}
public function edit($id)
{
$pegawai=Pegawai::join('departemens','pegawais.id_departemen','=','departemens.id')->find($id);
$departemen = Departemen::all();
return view('pegawai.edit',compact('pegawai','departemen'));
}
pegawai.index
#forelse ($pegawai as $item)
<tr>
<td class="text-center">{{ $item->nomor_induk_pegawai }}</td>
<td class="text-center">{{ $item->nama_pegawai }}</td>
<td class="text-center">{{ $item->nama_departemen }}</td>
<td class="text-center">{{ $item->email }}</td>
<td class="text-center">{{ $item->telepon }}</td>
#if($item->gender==0)
<td>Wanita</td>
#else
<td>Pria</td>
#endif
#if($item->status==0)
<td>Inactive</td>
#else
<td>Active</td>
#endif
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('pegawai.destroy', $item->id ) }}" method="POST">
Edit
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Hapus</button>
</form>
</td>
</tr>
#empty
<div class="alert alert-danger">
Data Pegawai belum tersedia
</div>
#endforelse
routes.web
Route::get('/', function () {
return view('dashboard');
});
Route::resource('/departemen',\App\Http\Controllers\DepartemenController::class);
Route::resource('/pegawai',\App\Http\Controllers\PegawaiController::class);
so i tried the Hapus and Edit button. it returning id_departemen instead id from table pegawai. i assume this is because im using join. i use join because i want to show nama_departemen from table departemen in pegawai. so what should i do to fix this.
maybe table departemen should look like this
'id', 'nama_departemen', 'nama_manager', 'jumlah_pegawai'
and table pegawai should look like this :
'id','nomor_induk_pegawai', 'nama_pegawai', 'id_departemen', 'email', 'telepon', 'gender', 'status'
and the controller should look like this :
Pegawai::select ('pegawai.nomor_induk_pegawai','pegawai.nama_pegawai','departemen.nama_departemen','pegawai.email','pegawai.telepon','pegawai.gender','pegawai.status','pegawai.id as id_pegawai','departemen.id as id_departemen')->Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
just choose which do you want to use, id_pegawai or id_departemen
you can change the join with leftjoin if you need
read this documention for another resolved
https://laravel.com/docs/9.x/queries#select-statements
The issue here is that your are joining two tables that both have an ID column.
To get around this you need to be specific in what columns you want to return.
For example;
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->select('pegawais.id','departemens.otherColumnName')->paginate(5);
Hope that helps.

How to fix " (1/1) ErrorException compact(): Undefined variable: categories in AdminPostsController.php line 23 '"

I want to create a post but I cannot because it shows me the Undefined variable error for the compact function,
can someone help me?
Below are the samples from files:
AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
return view('admin.posts.index', compact('posts','categories'));
}
admin/posts/index.blade.php
#extends('layouts.admin')
#section('content')
<h1>Posts page</h1>
<table class="table">
<thead>
<th>ID</th>
<th>Owner</th>
<th>Category</th>
<th>Photo ID</th>
<th>Title</th>
<th>Body</th>
<th>Created</th>
<th>Updated</th>
</thead>
<tbody>
#if($posts)
#foreach($posts as $post)
<tr>
<td>{{$post->id}}</td>
<td>{{$post->user->name}}</td>
<td>{{$post->category_id ? $post->category->name : 'No category'}}</td>
<td><img height="50" src="{{$post->photo ? $post->photo->file : 'http://placehold.it/400x400'}}"></td>
<td>{{$post->title}}</td>
<td>{{str_limit($post->body,10)}}</td>
<td>{{$post->created_at->diffForHumans()}}</td>
<td>{{$post->updated_at->diffForHumans()}}</td>
<td>View post </td>
<td>View comments</td>
</tr>
#endforeach
#endif
</tbody>
</table>
<div class="row">
<div class="col-sm-6 col-sm-offset-5">
{{$posts->render()}}
{{--pagination--}}
</div>
</div>
#stop
Make sure your $categories variable is defined in the index function inside your controller.
// AdminPostsController.php
public function index(){
$posts = Post::paginate(5);
$categories = Category::all();
return view('admin.posts.index', compact('posts','categories'));
}

Stuck at the passing data from database to view

I am still beginner at laravel, and i have problem about undefined variable when i want to pass data from the controller
here is my route
Route::get('/jelajahi-search', 'TourismController#srch');
Route::get('/jelajahi','TourismController#index');
here is my search function in the controller
public function srch(Request $request)
{
$this->validate($request, ['cari'=>'required']);
$search = $request->get('cari');
$posts = Tourism::where('namatempat', 'like', "%$search%")
->orWhere('kabupaten', 'like', "%$search%")
->paginate(1)
dd($posts);
return view('vendor.jelajahi')->with('tourismSearch',$posts);
}
and here is my view
#foreach ($tourismSearch as $tour)
<tr>
<th>
<td>
<img src="{{ URL::asset('/jelajahi-image/'.$tour->path_gambar) }}" style="width: 25%; height: 25%;"><br>
</td>
<td>
Nama tempat : {{$tour->namatempat}}<br>
Kabupaten : {{$tour->kabupaten}}<br>
Kecamatan : {{$tour->kecamatan}}<br><br>
</td>
</th>
</tr>
#endforeach
thank you
It seems your search doesn't return any results.
Try to use this check:
#if ($tourismSearch)
#foreach ($tourismSearch as $tour)
<tr>
<th>
<td>
<img src="{{ URL::asset('/jelajahi-image/'.$tour->path_gambar) }}" style="width: 25%; height: 25%;"><br>
</td>
<td>
Nama tempat : {{$tour->namatempat}}<br>
Kabupaten : {{$tour->kabupaten}}<br>
Kecamatan : {{$tour->kecamatan}}<br><br>
</td>
</th>
</tr>
#endforeach
#endif
Also, you can check for $posts contents by using dd($posts); before return view clause.

Resources