how to paginate read notifications in Laravel - laravel

I got an error related to the pagination of read notifications. Thank you for your help.
This is my controller code that gives this error when it runs
public function NotificationsRead(User $user)
{
$no = Auth::user()->readNotifications()->paginate(7)->latest()->get();
return view('user-profile.notifications-read', compact('user', 'no'));
}
And when I delete latest(), he gives an error to get()
I do not know how to paginate.
and A have two types notifications

You should do
$no = Auth::user()->readNotifications()->latest()->paginate(7);
In this way you are ordering descending before pagination.
Your code orders the result collection created by pagination.
https://laravel.com/docs/8.x/queries#latest-oldest

#jack this is my Blade
<div class="card-header text-black text-bold">
اعلان ها
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<div class="card">
<div class="card-body">
<h5 class="card-title">موضوع:</h5>
#foreach(auth()->user()->readnotifications->where('type','App\Notifications\NewReplySubmittedRoyal') as $notification)
<p class="card-text">{{$notification->data['name']}}
یک پاسخ برای پرسش شما با محتوای:
{{$notification->data['thread_title']}}
در {{$notification->data['time']}}<br>ثبت کرد.
</p>
<form>
<button type="submit" class=" btn btn-outline-success mb-2 ml-2">لینک پرسش</button>
</form>
<hr>
#endforeach
{{--*********** نوع دوم نوتیفیکیشن--}}
#foreach (auth()->user()->readNotifications->where('type','App\Notifications\FollowerNotifications') as $notification)
<p class="card-text">{{$notification->data['name']}} یک پرسش با محتوای:
{{$notification->data['thread_title']}}
ایجاد کرد.
</p>
لینک پرسش
<hr>
#endforeach
{{$no->render()}}
I have two types notifications

Related

Laravel Error: Too few arguments to function, 0 passed and exactly 1 expected

Using Laravel-5.8, I have this code:
public function manager_employee_list(Request $request)
{
$employees = HrEmployee::paginate(6);
return view('appraisal.appraisal_goals.manager_employee_list')->with('employees', $employees);
}
And it render this view: manager_employee_list
<div class="row d-flex align-items-stretch">
#if (count($employees))
#foreach($employees as $key => $employee)
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Staff ID: {{$employee->employee_code}}</b></h2>
<h2 class="lead"><b>{{$employee->first_name}} {{$employee->last_name}}</b></h2>
<h6 class="lead"><b>Employee Department: </b>{{isset($employee->department) ? $employee->department->dept_name : ''}}</h6>
</div>
<div class="col-5 text-center">
#if($employee->emp_image != '')
<img src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="img-circle img-fluid" />
#else
<img class="profile-user-img img-fluid img-circle" src="{{asset('theme/adminlte3/dist/img/default.png')}}" alt="" class="img-circle img-fluid">
#endif
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
</div>
</div>
</div>
</div>
#endforeach
#else
<h4 style="text-align:center;">No matching records found</h4>
#endif
</div>
I want to pass the parameter
['id'=>$employee->id]
from above to another controller action:
public function manager_employee_goal($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal')->with('goals', $goals);
}
It utilizes it here:
$goals = AppraisalGoal::where('employee_id', $id)->get();
When I clicked on
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
I got this error:
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal(), 0 passed and exactly 1 expected
and this is underlined:
public function manager_employee_goal($id)
route/web.php
Route::get('appraisal_goals/manager_employee_list', 'Appraisal\AppraisalGoalsController#manager_employee_list')->name('appraisal.appraisal_goals.manager_employee_list');
Route::get('appraisal_goals/manager_employee_goal', 'Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
How do I resolve it?
Thank you.
You need to change in your web.php file. add {id} in URL.
otherwise, all are fine as now your get method will be like 'appraisal_goals/manager_employee_goal/{id}'
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
I believe your route need to be updated to
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');

How to compare a value from the search bar to the one from the database in laravel

I am trying to use the search box in my Laravel project but I keep getting the same error since morning
Here is my Search controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function index()
{
return view('search.index');
}
public function Search(Request $request)
{
$serial_number= $request->input('search');
$results =DB::table('animals')->where(function ($query) use ($serial_number) {
$query->where('serial_number','LIKE',"%$serial_number%");
})->latest()->get();
return view('search',compact('results'));
}
}
My routes
Route::get('/search','SearchController#index')->name('search');
Route::post('/search','SearchController#search')->name('search');
and finally my view
#extends('layouts.app')
#section('content')
<form action="{{ route('search') }}" method="POST">
<div class="p-1 bg-light rounded rounded-pill shadow-sm mb-4">
<div class="input-group">
<input type="search" name="search" placeholder="Here the animal serial number..." aria-describedby="button-addon1" class="form-control border-0 bg-light">
<div class="input-group-append">
<button id="button-addon1" type="submit" class="btn btn-link text-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</form>
#if($results)
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h3>Details for the animal</h3>
</div>
<div class="card-body">
<div class="col-12">
<p><strong>Id: </strong>{{ $results->id }}</p>
<p><strong>Animal: </strong>{{ $results->type->category }}</p>
<p><strong>Gender: </strong>{{ $results->gender }}</p>
<p><strong>Place Of Birth: </strong>{{ $results->user->address->city }}</p>
<p><strong>Farm: </strong>{{ $results->user->name }}</p>
<p><strong>Date: </strong>{{ $results->created_at }}</p>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
#endsection
For me, I think that the problem is in the controller on that line $result which makes the view give the error
419|Page Expired or "Facade\Ignition\Exceptions\ViewException
Undefined variable: results (View:
/Users/macair13/MeatracProject/resources/views/search/index.blade.php)"
You need to include the CSRF token or exclude that URL from the CSRF token check.
<form ...>
#csrf
...
</form>
Laravel 6.x Docs - CSRF
Also you are not passing a results variable to your 'search' view from the Controller's index method. You will need to check if $results isset in your view or pass it to your view.
public function view (Request $request)
{
$search=$request['search'] ?? "";
if($search !=""){
//where
$data=File::where('id','like','%'.$request->search.'%')
->orwhere('name','like','%'.$request->search.'%')
->orwhere('file','like','%'.$request->search.'%')->get();
}
else{
$data = File::all();
}
$p = compact('data','search');
return view('upload_data')->with($p);
// $data = File::all();
// return view('upload_data',compact('data'));
}

How to use pagination on ajax data

I want to use default pagination in laravel in view fetched by ajax function. put I get an error.
ajax function
public function get_products($id){
$products = Product::where('category_id',$id)->where('is_hidden','0')->paginate(9);
$category = Category::find($id);
$returnHTML = view('products_ajax')->with('products', $products)->with('category', $category)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
returned view by ajax
<h3>{{$category->name}}</h1>
<hr>
<div class="cards">
#foreach($products as $product)
<div class="card" data-item-id="{{$product->id}}">
<img style="width:50%;" src="{{asset('storages/images/products/'.$product->image)}}">
<div class="card-details">
<p class="card-brand">{{$product->brand->name}}</p>
<p class="card-name" title="Food Name Here Food Name Here Food Name Here Food Name Here">
{{$product->code}}
</p>
<p class="card-price" hidden> {{$product->price}}</p>
<p hidden class="card-full-des">
{{strip_tags(html_entity_decode($product->description))}}
</p>
<p class="card-packing">
<span>{{$product->packing}}</span>
</p>
{{-- <p class="card-packing">
<span>Packing: 12-8 oz (225g)</span>
</p> --}}
<div class="card-foot">
<button class="mbtn5" onclick="CardAddToCartOrDetails(this, true)">Add to Cart</button>
<button class="mbtn4" onclick="CardAddToCartOrDetails(this, false)">Details</button>
</div>
</div>
</div>
#endforeach
{{$products->links()}}
</div>
</div>
but I get this error:
so, can anyone help me?
The MethodNotAllowedHttpException is caused by a calling a route with a wrong method.
i.e. defining a GET route for /users and calling it with POST would result in this error.

How to increment the count value in DB when button is clicked in laravel?

I have Home.blade.php when I click the vote button in that page, the count value should increment in the candidate table.
Here my Home.blade.php
#foreach ($file as $show)
<div class="col-md-3">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<!-- <p><button>VOTE</button></p> -->
<button type="button" class="vote">VOTE</button>
</div>
</div>
</div>
#endforeach
I have table name called the candidate, when vote button is clicked, the count should be incremented in database. And i have controller called HomeController pls help me.
Thank you
class Homecontroller extends Controller
{
public function pre()
{
$file = DB::table('candidate')
->select('name','branch')
->where('candidate.post_id', '=', 1)
->get();
return view('/home')->with('file',$file);
}
}
#foreach ($file as $show)
<div class="col-md-3" style="margin-bottom: 20px;">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<p><button>VOTE</button></p>
</div>
</div>
</div>
#endforeach
i have used funtion redirect to controller
in controller i have used this function
public function Count(){
DB::table('candidate')->increment('count',1);
return view('/home');
}
still its not working
this is my Route
Route::get('/home','Homecontroller#count');

How to work with pagination in Laravel Algolia search

Pagination is not working. It shows empty page on clicking 2 page.It is not fetching the second page from the search query. And also token is not showing on
2nd page url. Is this is the reason then how to add csrf_token to pagination
Route.
I am using algolia to search
Route::get('posts/search','PostController#search')->name('posts.search');
Controller
public function search(Request $request){
if($request->has('q')) {
$request->flashOnly('q');
$results = Post::search($request->q)->paginate(5);
} else {
$results = [];
}
return view('posts.search')->with('results', $results);
}
View
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Search for Posts</h1>
<form action="{{ route('posts.search') }}" method="get">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" class="form-control input-lg" placeholder="Search for a post..." value="{{ old('q') }}"/>
<span class="input-group-btn">
<button class="btn btn-default btn-lg" type="submit">Search</button>
</span>
</div>
</form>
<hr />
#foreach ($results as $post)
<div class="row" style="margin-top: 20px;">
<div class="col-md-8">
<h3>{{ $post->title }}</h3>
</div>
<div class="col-md-4">
#if ($post->published)
<h4><span class="label label-success pull-right">PUBLISHED</span><h4>
#else
<h4><span class="label label-default pull-right">DRAFT</span><h4>
#endif
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>
{{ str_limit($post->content, 250) }}
</p>
</div>
</div>
#endforeach
#if (count($results) > 0)
<hr />
<div class="text-center">
{{ $results->links() }}
</div>
#endif
#endsection
Try this one may be help
// Making sure the user entered a keyword.
if($request->has('q')) {
// Using the Laravel Scout syntax to search the posts table.
$results = Post::search($request->get('q'))->paginate(15);
}
Update my answer sometimes help this
return view('posts.search',['results' => $results]);
Try :
{{$results->appends(request()->query())->links()}}
insted of
{{ $results->links() }}

Resources