I can not save changes using the save () method laravel 5.5 - php

I'm using a method to find the records to change. Follow the code:
public function findId($id)
{
$subCategoria = $this->model::join(
'categoria', 'categoria.id', '=', 'sub_categoria.id_categoria')
->select(
'sub_categoria.id AS id_sub_categoria',
'sub_categoria.nome AS nome_sub_categoria',
'categoria.nome AS nome_categoria',
'categoria.id as id_categoria'
)
->where('sub_categoria.id', $id)
->where(
'sub_categoria.id_unidade_de_trabalho',
Auth::user()->id_unidade_de_trabalho
)
->where(
'categoria.id_unidade_de_trabalho',
Auth::user()->id_unidade_de_trabalho
)
->whereNull('sub_categoria.deleted_at')
->whereNull('categoria.deleted_at')
->first();
return $subCategoria;
}
My update method looks like this:
public function update(array $data, $id)
{
$model = $this->findId($id);
$model->nome = $data['nome'];
$model->id_categoria = $data['id_categoria'];
return $model->save();
}
Funny that when I use the find method, which is extended from Model, it works!

Related

I am trying to pass searched query based on user input to my dompdf view

i want to use the result of that query and pass it into my pdfview and create a pdf based on the result. i have using sessions but did not work, is there any other solution
my controller for the search query:
public function index(Request $request){
$rsearch= $request->rsearch;
$fromdate= $request->fromdate ;
$todate= $request->todate ;
$rents = DB::table('rentcollection')
->join('tenants','rentcollection.AccountNo','=','tenants.AccountNo')
->join('propertys','tenants.PropertyID','=','propertys.PropertyID')
->select(DB::raw("CONCAT(tenants.OtherNames,' ',tenants.Surname) as
Fullname"),'rentcollection.Date as Date','rentcollection.Amount as
Amount','rentcollection.Period as Period','rentcollection.ReceiptNo as
Receipt','tenants.RegDate
as RegDate', 'tenants.AccountNo as Accno','tenants.Rent as Rent',
'tenants.NoOfProperty as
NoOfProperty','propertys.Address as Address','propertys.Property as Property')
->where('propertys.Address','like','%'.$rsearch.'%')
->whereBetween('Date', [$fromdate, $todate])
->paginate(20);
return view('reports.rent', compact('rents',));
}
my controller for creating pdf:
public function createPDF(Request $request){
$datas = $request->session()->get('cart');
$pdf = PDF::loadView('pdf.rent', ['orderedrent' => $datas,])->setPaper('A4','landscape');
return $pdf->stream('invoice.pdf');
}

Method Illuminate\Http\Request::isDirty does not exist. Laravel

I'm using voyager admin panel. I want to check the attributes that are changed when I submit the form. I'm using isDirty() & getDirty() but that is not working.
It is showing that the error
Method Illuminate\Http\Request::isDirty does not exist.
Sometimes showing this error.
Call to a member function isDirty() on string
update Controller
public function update(Request $request, $id)
{
$slug = $this->getSlug($request);
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();
// Compatibility with Model binding.
$id = $id instanceof \Illuminate\Database\Eloquent\Model
? $id->{$id->getKeyName()}
: $id;
$model = app($dataType->model_name);
$query = $model->query();
$data = $query->findOrFail($id);
$this->insertUpdateData($request, $slug, $dataType->editRows, $data);
// That part is showing an error
dd($request->isDirty(['status']));
return $redirect->with([
'message' => __('voyager::generic.successfully_updated')." {$dataType->getTranslatedAttribute('display_name_singular')}",
'alert-type' => 'success',
]);
}
Request will not show the results it will work only with the model. So I used $data instead of $request. And I've to show it in an array. Something like this.
$data = $query->findOrFail($id);
$data->status = $request->status;
dd($data->isDirty('status'));
I works on my side.

Laravel Eloquent: How to add where condition from the with function model relation

Anyone can help me? How to add where condition from the with function model relation.
I tried ->where('driver.group_id',$group_id) but it does not work. please look my code below.
public function getDriversDailyEarnings()
{
$group_id = Auth::user()->group->id;
$today = Carbon::today();
$data = DailyEarnings::with(['payout_cost',
'status:id,name',
'driver' => function ($query) {
$query->with('user');
}])
->where('driver.group_id',$group_id)
->whereDate('created_at', $today)
->get();
return response()->json($data, 200);
}
You can try this. Haven't tested it yet.
DailyEarnings::with([
'payout_cost',
'status:id,name'
'driver' => function($query) {
$query->where('group_id', auth()->user()->group->id)->with('user');
}
])
->whereHas('driver', function($query) {
$query->where('group_id', auth()->user()->group->id);
})
->whereDate('created_at', now()->format('Y-m-d'))
->get();
To pass other clauses to a relation, just use it inside an array, where the key and the name of the relation and the value is a function that receives an instance of the query, something like this:
public function getDriversDailyEarnings()
{
$data = DailyEarnings::with([
'payout_cost' => function ($myWithQuery) {
$myWithQuery->where('column-of-relation-here', 'value-here')
->orWhere('name', 'LIKE', '%Steve%');;
}
])->get();
return response()->json($data, 200);
}
Perhaps something like this:
// ->where('driver.group_id',$group_id)
->whereHas('driver', fn($qry) => $qry->where('group_id', $group_id)) // if group_id a field on driver

Paginate Results of Model Laravel

I have a problem with pagination in laravel 5.3
The code:
public function deals()
{
return $this->belongsToMany('App\Models\ListsDeals', 'list_has_deals' , 'list_id', 'deal_id')->withPivot('list_id');
}
public function form_edit_list( $id ){
$list = Lists::find( $id );
PAGINATE THIS -----> $deals = $list->deals;
$user = User::find( $list->id_user );
$categoriesArray = ListsCategories::all();
$featuresArray = ListsFeatures::all();
$images = ListsGalleries::all();
return view( "admin.forms.form_edit_list" )
->with( "list", $list )
->withCategoriesArray( $categoriesArray )
->withFeaturesArray( $featuresArray )
->withImages( $images )
->with( "user", $user );
}
I have tried this,
$deals = $list->deals->paginate(5);
How can I paginate the results of deals ?
Because paginate is not a method of that.
I believe you can add the paginate() call to the deals() relationship definition (which will paginate all uses of it).
That may not be ideal, so you can also do $deals = $list->deals()->paginate();
$list->deals is a collection of items, while $list->deals() is an Eloquent query builder instance you can make further adjustments to before fetching the reuslts.

Selecting required fields using relations in Laravel

I have a model Meetings like this:
public function meeting_comments(){
return $this->hasMany('App\MeetingsComments', 'meeting_id', 'id');
}
public function meeting_users() {
return $this->hasMany('App\UserMeetingDetails', 'meeting_id', 'id');
}
The Controller is like this:
$res = Meetings::with('meeting_comments', 'meeting_users')
->select('')->get()->toArray();
I only need comments from meeting_comments and user_id from meeting_users.
What do I put in select to only get the required fields from meeting_comments and meeting_users ??
You do it through a closure in the with call:
$res = Meetings::with(['meeting_comments' => function($query) {
$query->select('comments', 'meeting_id');
}, 'meeting_users' => function($query) {
$query->select('user_id', 'meeting_id');
}])
->get()->toArray();
I'm taking this from memory, so the syntax may be slightly incorrect, but it should work. :)

Categories