How to catch 1 colomn (id) on table in edit form - laravel

I have 1 table data . and I have function edit on this table . in this edit form I have select dropdown to show pluck from database .
I have 3 table . this name is aduan, ipsrs , teknisi
struckture aduan table
id
user_id
ipsrs_id
teknisi_id
aduan
etc.....
ipsrs table
id
nama_bagian
etc....
teknisi table
id
ipsrs_id
nama_teknisi
etc...
This is my controller :
public function index(Request $request)
{
$ipsrs = DB::table('ipsrs')->pluck('nama_bagian','id');
$belum_kerjakan = Aduan::with('users')->where('status','Belum Dikerjakan')->get();
$teknisi = Teknisi::where('ipsrs_id' , 1)->pluck('nama_teknisi', 'id');
$dalam_proses = Aduan::with('users')->where('status','Sedang Dikerjakan')->get();
$selesai = Aduan::with('users')->where('status','Selesai')->get();
return view('admin.admin_dashboard',[
'belum_dikerjakan' => $belum_kerjakan,
'dalam_proses' => $dalam_proses,
'selesai' => $selesai,
'ipsrs' => $ipsrs,
'teknisi' => $teknisi,
]);
}
Example this variable $belum_dikerjakan is showing table data and I have fucntion edit on this table ( in modal) .
But this I don't know how to catch data (ipsrs_id) to set where clause in the pluck . I want to change 1 to ipsrs_id form table , but how ?

If I understood the problem then here is the answer
pull only the ids from ipsrs table and pass to Teknisi table whereIn method
$ipsrsIds = DB::table('ipsrs')->pluck('id')->toArray();
$teknisi = Teknisi::whereIn('ipsrs_id' , $ipsrsIds)->pluck('nama_teknisi', 'id');

public function edit($id)
{
$category =Category::findOfFail($id);
return view('admin.category.edit',compact('category'));
}
public function update(Request $request, $id)
{
$this->validate($request,[
'name' => 'required|unique:categories'
]);
$category = Category::find($id);
$category->name = $request->name;
$category->slug = str_slug($request->name);
$category->save();
Toastr::success('Category Successfully Updated','Success');
return redirect()->route('admin.category.index');
}

Related

How to filter Laravel 9 Daily Reports table by Team ID in User Table

I have a 'user' table in which there is a column 'team_id'. I also have a 'daily_report' table that includes a 'user_id' column. I am trying to filter the data in the 'daily_report' table by 'team_id'. I am using Laravel 9.
My current code in the index controller is as follows:
public function index()
{
$daily_reports = DailyReport::with('user.team')->with('updated_by_user_id')
->when(request()->q, function ($daily_reports) {
$daily_reports = $daily_reports->where('description', 'like', '%' . request()->q . '%');
})->when(request()->status, function ($daily_reports) {
$daily_reports = $daily_reports->where('status', request()->status);
})->when(request()->user_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user_id', request()->user_id);
})->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user.team_id', request()->team_id);
})->latest()->paginate(5);
$users = User::all();
$teams = Team::all();
return Inertia::render('Apps/DailyReports/Index', [
'daily_reports' => $daily_reports,
'users' => $users,
'teams' => $teams,
]);
}
->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->where('user.team_id', request()->team_id);
})->latest()->paginate(5);
However, I am encountering the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.team_id' in 'where clause'
select count(*) as aggregate from `daily_reports` where `user`.`team_id` = 2
Could you please help me resolve this issue?
user is relationship, so have to use whereHas.
->when(request()->team_id, function ($daily_reports) {
$daily_reports = $daily_reports->whereHas('user', function ($query) {
$query->where('team_id', request()->team_id);
});
})->latest()->paginate(5);
Fixed with:
$daily_reports->whereHas('user', fn ($q) => $q->where('team_id', request()->team_id));

error on multiple data updates that belong to two entities in laravel

error on multiple data updates that belong to two entities all data is updated but child table in emp_id is only first insert in all columns. database image attached.
public function update(Request $request, $id)
{
$announcement = Announcement::find($id);
$dates = explode(' to ' ,$request->date);
$announcement->title = $request->title;
$announcement->date = $dates[0];
$announcement->todate = $dates[1] ?? null;
$announcement->departments = implode(',',$request->department);
$announcement->descriptions = $request->descriptions;
$announcement->update();
if($request->employees){
foreach ($request->employees as $index => $emp_id) {
$announcement->announceduser()->where('announcement_id',$announcement->id)->update([
'emp_id' => $request->employees[$index]
]);
}
}
return redirect()->route('announcement.index')->with('success', 'Announcement Updated!!');
}
enter image description here
it is only update first emp_id in all table

Eloquent oderby using ( with )

client Model
protected $table = 'client';
protected $fillable = [
'client_name',
'supplier_id'
];
public function supplier()
{
return $this->belongsTo(Supplier::class, 'supplier_id', 'id');
}
Supplier table
id | supplier
1 | john
2 | ace
3 | bravo
ClientController
$Client = new Client();
$Client = $Client ->with(
'supplier'
);
$Client = $Client->orderBy('supplier', 'DESC');
Error
Column not found: 1054 Unknown column 'supplier' in 'order clause' (SQL: select * from `client` order by `supplier` desc limit 20 offset 0)
i need to order by supplier from with relationship
Depending on your laravel version you can try:
$Client = new Client();
return $Client ->with([
'supplier' => function ($q){
$q->orderBy('supplier');
}
]);
If you want to order the supplier inside the Client, see this.
If you want to order the Client based on its supplier, see this.
So, From what I have understood is that you want to orderBy your data based on a relationship's column.
If so, I am not sure if it's easy to do this with Eloquent.
Though you can use Query Build as following:
$clientQuery = Client::query();
$clientQuery->selectRaw("clients.*, (SELECT MAX(created_at) from suppliers WHERE clients.supplier_id=suppliers.id) as orderBySupplier")
->orderBy("orderBySupplier", "DESC");

Laravel relathionship with query builder get error

I have 3 tables this table is related to, I will call this table with
belanja (parent)
anak_belanja (child)
supplier (has relation with parent)
I can create this relation from parent and child but on table supplier I get an error. Usually I use "with" and I get no error, but now I use query builder and I have a problem
My Belanja Model
public function supplier()
{
return $this->belongsTo(\App\Models\Suplier::class ,'supplier_id');
}
My Anak_Belanja model
public function belanja()
{
return $this->belongsTo(\App\Models\Belanja::class ,'belanja_id');
}
and this is my controller
public function render()
{
$user_id = Auth::user()->id;
$sub = SubRincianUraianKegiatan::where('user_id', $user_id)
->where('id' , $this->newid)
->pluck('uraian', 'id');
$sup = Supplier::all()->pluck('nama' ,'id');
$data = DB::table('belanja AS t')
->select([
't.id',
't.uraian',
't.tgl_belanja',
't.supplier_id',
DB::raw('coalesce(sum(p.harga * p.qty),0) AS total_order')
])
->leftjoin('suplier AS s','t.suplier_id','=','s.id') // on here i think this error
->leftjoin('anak_belanja AS p','p.belanja_id','=','t.id')
->where('sub_id', $this->newid)
->where('uraian', 'like', '%'.$this->search.'%')
->groupBy('t.id')
->groupBy('t.uraian')
->groupBy('t.tgl_belanja')
->groupBy('t.suplier_id')
->paginate(10);
return view('livewire.detail-belanja-lw',
['data' => $data ,
'sup' => $sup,
'sub' => $sub,
]);
}
In my blade I try to add supplier like that
{{$i->supplier->nama}}
but I get an error
Undefined property: stdClass::$supplier
Can someone explain my mistake?
$data = DB::table('belanja AS t')
->select([
't.id',
't.uraian',
't.tgl_belanja',
't.suplier_id',
DB::raw('coalesce(sum(p.harga * p.qty),0) AS total_order'),
DB::raw('s.nama as nama')
])
->leftjoin('suplier AS s','t.suplier_id','=','s.id') // on here i think this error
->leftjoin('anak_belanja AS p','p.belanja_id','=','t.id')
->where('sub_id', $this->newid)
->where('uraian', 'like', '%'.$this->search.'%')
->groupBy('t.id')
->groupBy('t.uraian')
->groupBy('t.tgl_belanja')
->groupBy('t.suplier_id')
->paginate(10);
try this.

How to count two column with the third column in another table?

The idea is I have a table events:
The column Mark_id has a relationship with another table called mark_events :
The relation in model Event :
public function markevent()
{
return $this->belongsTo('App\MarkEvent', 'mark_id', 'id');
}
What I need is count these columns ( status , diabetes , number_mark_event ) and save the total of these number in sum column
Code of Controller:
$form = [
'mark_id' => $request->mark_id,
'status' => $z,
'diabetes' => $request->diabetes,
];
$event = Event::update($form);
Join the tables and count the columns. This works as you only retrieve the counted columns, joins can provide side effect if returning ambiguous columns.
$eventCounts = Event::leftJoin('mark_events', 'events.mark_id', '=', 'mark_events.id')
->select(DB::raw('count(status) as status_count'), DB::raw('count(diabetes) as diabetes_count'), DB::raw('cout(number_mark_event) as number_mark_event_count'))
->get();
$eventCounts->status_count;
$eventCounts->diabetes _count;
$eventCounts->number_mark_event_count;
I write this solution for the example you posted
In update method in Controller:
public function update(Request $request){
$event = Event::where('mark_id',$request->mark_id)->firstOrFail();
$sum = $event->status + $event->diabetes + $event->markevent->number_mark_event;
$event->update([
'sum' => $sum
]);

Resources