How to fix laravel livewire issue with pagination? - laravel

I have a table which show some record but when I choose page 2 from table pagination and then apply filter on it they still show page no 2 table. My query is when I change anything from filter, pagination need to be remove and by default show record related to status.

after query function add this
$this->resetPage();
Or can you add public function to input search same that
public function updatingInput(){
$this->resetPage();
}

Related

How to Display Single Data With Laravel 5.6

Hello everybody,
Here i want to ask about how to display single row from database in view, usually we use #foreach for looping data. but here i just want to display one row.
Note : Display single row but use this eloquent ItemName::latest()
help me please, thank you :)
In model ItemName.php file, you could create a function. This function would return only one row by your filter.
Ex:
public static latest($itemId) {
return self::where(['item_id' => $itemId])->orderBy('id', 'desc')->first();
}

laravel scout temporary disable toSearchableArray when doing updates

is it possible to disable toSearchableArray when doing updates to a record or is there any way to only update specific fields in a record in my search index?
Eg:
public function toSearchableArray()
{
$item = $this->toArray();
$item['title'] = $this->title;
...
...
...
$item['category'] = $this->category->category_name;
$item['uploaded_at'] = Carbon::now('America/Montreal')->timestamp;
}
The only problem now is each time I update a record it also resets its uploaded_at timestamp and re-loads the relationship which is one more query I dont need since it already has it set when I created the item.
So is there any way I can temporary disable toSearchableArray ? I only need to update a few fields in in the row in my index so there is no need to rerun everything in toSearchableArray
Like bellow only update the title and then update the title in my algolia index without reseting uploaded_at or loading the category relation again
$order = App\Order::find(1);
$order->title = 'a new title'
$order->save();
You can use, unsearchable function available in laravel scout.
$modal->unsearchable();
//etc.....
//Finally save the modal
$modal->save()
This way when you save or update it's won't sync to algolia.
If You again want to sync the model to algolia you may call searchable method as shown below.
$modal->searchable();

Creating a filtered search based on different parameters

i need to create a filtered search based on different paramnters chosen by a user. So, for example, my app is a property app. I want them to be able to define the county, town, max,min bedrooms ect from a dropdown list and press search, and this returns all the properties that match the criteria.
How do I go about doing this? I can't find any tutorials online, but maybe I'm not phrasing it right.
Here is an image of what I'm after from a UI point of view.
https://imgur.com/a/YEEqt
The following is an easy to go solution considering your experience.
Create a new controller or a method in an existing one.
First create a simple form (method GET) in blade that will return predefined values from simple hidden fields
Create a new GET route in which the form must be submitted and link it to the controller method that you created
In your controller method get the submitted form data from your request, make the correct queries and return the blade template file that contains the form with the results
Modify blade template to show results
Finally replace the hidden fields with selects drop-downs and modify your controller in order to populate them
You can make the form submission process and the select drop-down fields population asynchronous but based on your experience with Laravel it should be hard. Following the steps above will do your job.
Always take a look to the official documentation. You will ge a lot of help from there.
You can try like this for filter options in your controller
public function filter(Request $request, Property $property)
{
$property = $property->newQuery();
// Search for a property based on country
if ($request->has('country')) {
return $property->where('country', $request->input('country'));
}
// Search for a property based on their area.
if ($request->has('areas')) {
return $property->where('areas', $request->input('areas'));
}
// Search for a property based on max_price
if ($request->has('max_price')) {
return $property->where('price','<=', $request->input('max_price'));
}
// Continue for all of the filters.
return $property->get();
}
For more info refer the link

Set global where clause when I retrive data using laravel eloquent

I have a table content. So when I retrive anything from that table using laravel eloquent, I need to set a global where clause Content::where('is_playlist', 1)->get();, so that every time I retrieve data from that table, I should automatically filtered with that particular where clause.
You can create a scope function in the model to filter that out e.g:
public function scopePlaylist($query)
{
return $query->where('is_playlist', 1);
}
This means you can easily call, Content::playlist()->get() and you'll have only playlist content.
I hope this is useful?

Laravel Datatables display data from related tables

I'm using Datatables for displaying data. It works good, but when I want to display data from related tables (models), they do not appear.
Model Keyword.php
public function website() {
return $this->belongsToMany('App\Website');
}
Model Website.php
public function keywords() {
return $this->hasMany('App\Keyword');
}
'websites' database table: id, siteName, siteUrl
'keywords' database table: id, website_id, kwName
Url that should display data: projects/'.$website->id.'/edit (example: projects/2/edit - (edit is a blade))
So, at this Url I want to show a datatable that display all kwName for certain website_id(in this example /2/).
Please, help me what should I use and how to do this.
Your Eloquent relationships are the wrong way around.
keywords has a reference to website via website_id, this is a belongsTo relationship.
website is referenced by many keywords, so this is a hasMany relationship.
Laravel 5.4 Eloquent Relationships

Resources