Ordering with search in brackets ui laravel - php

I am using brackets ui in my laravel admin panel
I am trying to sort my list by desc order with search as well but it is not working
My Controller and I have tried that
public function index(Request $request)
{
// create and AdminListing instance for a specific model and
$data = AdminListing::create(Order::class)->attachOrdering('id', 'desc')->processRequestAndGet(
// pass the request with params
$request,
// set columns to query
['id', 'name', 'created_at', 'phone', 'total_amount', 'status','discount','user_order_note'],
// set columns to searchIn
['id', 'name', 'created_at', 'phone', 'total_amount', 'status','discount','user_order_note']
);
if ($request->ajax()) {
return ['data' => $data];
}
return view('admin.orders.index', ['data' => $data]);
}
Have done this according to the documentation.
It does not give an error, but does not work as well.

Please add get() method
please modify the query like below.
$data = AdminListing::create(Order::class)->attachOrdering('id', 'desc')->processRequestAndGet(
// pass the request with params
$request,
// set columns to query
['id', 'name', 'created_at', 'phone', 'total_amount', 'status','discount','user_order_note'],
// set columns to searchIn
['id', 'name', 'created_at', 'phone', 'total_amount', 'status','discount','user_order_note']
)->get();

Related

Using callable params to modify a query

I am using craftable to generate an admin panel for my app.
I have an Organisation model that belongs to an Organisation Type model.
In the index listing, I want to be able to display the Organisation Type name rather than the _id. To do this, I have to modify this query, to eager load the relationship using the 'with' method.
The method signature for the listing is:
public static function processRequestAndGet($request, $columns = array(), $searchIn = null, $modifyQuery = null, $locale = null)
and the index method is:
$data = AdminListing::create(Organisation::class)->processRequestAndGet(
// pass the request with params
$request,
// set columns to query
['id', 'organisation_type_id', 'name', 'active'],
// set columns to searchIn
['id', 'name']
);
if ($request->ajax()) {
return ['data' => $data];
}
return view('admin.organisation.index', ['data' => $data]);
Craftable, provides a modifyQuery method to, but i'm not sure how to use it:
public function index(IndexMovie $request)
{
$data = AdminListing::create(Movie::class)
->modifyQuery(function($query) use ($request){
if ($request->has('author_id')) {
$query->where('author_id', $request->author_id);
}
})
->get();
Can someone help me use the callback to modify the query so that I can include the related table data?
Okay so I've came across the exact same problem and I'd like to share my way of doing it.
craftable uses a processRequestAndGet function that takes as a 4th parametre the intended callable query. So when you need to use that parametre rather than trying to access the modifyQuery function directly.
$data_r = AdminListing::create(Organisation::class)
->processRequestAndGet(
// pass the request with params
$request,
// set columns to query
['id', 'organisation_type_id', 'name', 'active'],
// set columns to searchIn
['id', 'name'],
function($query) use ($request){
if ($request->has('author_id')) {
$query->where('author_id', $request->author_id);
}
}
);
You were almost right, just pass the intended callback inside your processRequestAndGet and voilĂ .

Collection only with relationship fields?

I want to return the only fields I needed but when the collection has relationship it would not work.
For example:
$items = $items->map(function($item) {
return collect($item)->only([
'id',
'posts.title',
'name',
'created_at'
]);
});
Only thing that did not return is 'posts.title'
How to solve this?

apply class to row in yajra/datatable if a given model instance has been soft deleted

In yajra/laravel-datatables, I want to determine that current model is soft-deleted or not. if it is deleted , a success bootstrap class apply to row containing that model details.
this is my backend code:
$courses =
Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date']);
if ($request->has('showDeleted') && $request->get('showDeleted') == 1) {
$courses = $courses->withTrashed();
}
$datatable = app('datatables')->of($courses)
->orderBy('created_at', 'desc')
//columns come here
->setRowClass(function ($course) {
return ($course->trashed() ? 'danger' : 'sdasd');
});
return $datatable->make(true);
As you see I used:
->setRowClass(function ($course) {
return ($course->trashed() ? 'danger' : ' ');
});
to apply desired class But $course->trashed return false always for all model instances even those is not trashed.
what is best solution?
I found the solution.
It was my mistake that I should included deleted_at field in columns selection on select method:
$courses =
Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date','deleted_at']);

Laravel Validation not returning error

I am new in Laravel.
This is my laravel controller:
public function store()
{
$validator = Validator::make($data = Input::all(), City::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$image_temp=Input::file('image');
$name = Input::file('image')->getClientOriginalName();
$data['image']='';
if(Image::make($image_temp->getRealPath())->save('public/up/city/'.$name)){
$data['image']='up/city/'.$name;
}
City::create($data);
return Redirect::route('admin.cities.index');
}
and This is my model:
class City extends \Eloquent {
protected $primaryKey='city_id';
// Add your validation rules here
public static $rules = [
'title' => 'required',
'image' => 'mimes:jpeg',
'parent_id' => 'required',
'name' => 'required',
'english_name'=>'unique:cities,english_name|required'
];
// Don't forget to fill this array
protected $fillable = ['name', 'parent_id', 'english_name','population','phone_prefix','image'];
}
And I have a form I use {{ $errors->first('inputName','<p class="error">:message</p>') }} bellow my form inputs, when I send form without filling inputs I get error under each form input. But when I fill out all form inputs and then submit the Laarvel validation return fail (I mean mass assignment not working and not registering, and redirects back to create page.) what is the problem?
Almost always the reason for a mass assignment error is a missing attribute in the $fillable array. In your case it is title.
protected $fillable = ['title', 'name', 'parent_id', 'english_name','population','phone_prefix','image'];
^
Edit
Apparently the problem was actually that the title in the $rules array, which should have been name...

laravel belongstomany with condition

I have the following model.
class Training extends \Eloquent {
// Add your validation rules here
public static $rules = [
'name' => 'required',
'city' => 'required',
'province' => 'required',
'budget_year' => 'required|integer',
's_date' => 'required|date',
'e_date' => 'required|date'
];
// Don't forget to fill this array
protected $fillable = [
'name',
'city',
'province',
'budget_year',
's_date',
'e_date'
];
public function material(){
return $this->hasMany('Material');
}
public function budget(){
return $this->belongsToMany('Budget')->withPivot('amount');
}
public function budgetById($training_id){
$this->belongsToMany('Budget')->where('training_id', '=', $training_id)->get();
}
}
when I debug the budgetById method using DB::getQueryLog, the query is as follow
select budgets.*,
budget_training.training_id as pivot_training_id,
budget_training.budget_id as pivot_budget_id
from budgets inner join budget_training on budgets.id = budget_training.budget_id
where budget_training.training_id is null and training_id='6'
which return 0 rows, but when I try to modify the query and run it in pgadmin, the following script works well.
select budgets.*,
budget_training.training_id as pivot_training_id,
budget_training.budget_id as pivot_budget_id
from budgets inner join budget_training on budgets.id = budget_training.budget_id
where budget_training.training_id='6'
notice I remove training_id is null and from Laravel generated query. What is wrong with my budgetById method?
You have called get() and didn't use return here:
public function budgetById($training_id){
// = in where is optional in this case
$this->belongsToMany('Budget')->where('training_id', '=', $training_id);
}
You should use like this:
public function budgetById($training_id){
// = in where is optional in this case
return $this->belongsToMany('Budget')->where('training_id', '=', $training_id);
}
In Lavarel 7.X, you can use the wherePivot method to filter columns on the pivot table, like this:
return $this->belongsToMany('Budget')->wherePivot('training_id', '=', $training_id);
or
return $this->belongsToMany('Budget')->wherePivotNotNull('training_id');

Categories