Where can I put paginate()? - ajax

I am trying put paginate(10) to add a pagination later, but when I added it it gives me this error:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$product
Here is my controller where I want add the pagination(10):
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('your')->with('product', $user->product);
}
I did try this:
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->paginate(10);
return view('your')->with('product', $user->product);
}
How can I avoid the undefined property error?

Actually find function is used for getting only 1 result and you are applying pagination on that which is resulting into an error.
You might be wanting the products data to be paginated, For that do like this
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->product()->paginate(10);
return view('your')->with('product', $user);
}

Related

why i can't call name from table food?

public function orderlist(Request $request){
$id = $request->id;
$data['order'] = Order::where('shop_id',$id)->orderBy('id')->get();
foreach($data['order'] as $orders){
$orders->shop = Shop::where('id',$orders->shop_id)->first();
$orders->food = Food::where('id',$orders->food_id)->get();
}
return $orders->food->name;
return view ('administrator.users.list_order.index',$data);
}
If you set up proper Eloquent relationships, this code could be rewritten like so:
public function orderlist(Request $request, Shop $shop){
$orders = $shop->orders()->with(['food'])->get();
return view ('administrator.users.list_order.index',compact('orders'));
}
Check out the docs here: https://laravel.com/docs/5.7/eloquent-relationships

how to use $request->all() on controller and blade view in laravel 5.5

in Controller
public function index(Request $request)
{
$search_cons = $request->all();
$search_con = $search_cons->name; //error place
return $search_cons.$search_con;
}
->name this place has the error
Trying to get property of non-object
Or in blade.view
<p>{{$search_cons->name}}</p> has the error
Trying to get property of non-object
But if I use
$search_cons=$request->input('name');
on controller
the blade view
<p>{{$search_cons}}</p> will work ok!
I want the $search=request->all() so I can freely use $search->name on my blade view
How can I fix the question?
PS: I tried $resquest('name') still not to work
Request::all() ->tell me the
When you do $request->all() it returns all the inputs submitted in array format. So in your case, you can do
$search_cons = $request->all(); // dd($search_cons) so you can see its structure
$search_con = $search_cons['name']; // instead of ->name since it's not an object anymore
And anyway, you can skip the $request->all() thing - you can actually just do this directly:
$request->name.
EDIT:
You can cast the array as an object using (object)
$search_cons = (object) $request->all();
this will still let you use $search_cons->name
$search_cons is an array, not an object:
$search_con = $search_cons['name']
public function index(Request $request)
{
$search_cons = $request->all(); //returns array
$search_con = $search_cons['name']; //error place
return $search_cons.$search_con;
}
Or you can do like this
request()->name //request() isa global helper function
Try
In Controller
public function index(Request $request)
{
$search = $request->all();
return ['search' => $search];
}
In Blade
<p>Name : {{$search['name'] ? $search['name'] : ''}} </p>

Laravel and algolia, ignore array if null

I have this code:
public function toSearchableArray()
{
$data = $this->toArray();
$data['_geoloc'] = $this->_geoloc->toArray();
$data['address'] = $this->address->toArray();
return $data;
}
However sometimes $data['entities'] is null therefore throwing me an error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to a member function toArray() on null
Is there any way to by-pass that?
You need to check elements if they exist and not null before call methods on them, like this:
public function toSearchableArray()
{
$data = $this->toArray();
$data['_geoloc'] = !empty($this->_geoloc) ? $this->_geoloc->toArray() : null;
$data['address'] = !empty($this->address) ? $this->address->toArray() : '';
return $data;
}
Also $this->toArray(); will convert the model instance to an array with all relations. So you need to load them like: $this->load('_geoloc', 'address'); and call only $data = $this->toArray();
I assume address is a relation to another table.
toArray() will convert it, if it was loaded before
public function toSearchableArray()
{
$this->address;
$data = $this->toArray();
return $data;
}
Is _geoloc also a relation to another table?
I think you can try this:
public function toSearchableArray()
{
$data = $this->toArray();
$data['_geoloc'] = $this->_geoloc->toArray();
$data['address'] = $this->address->toArray();
print('<pre style="color:red;">');
print_r($data);
print('</pre>');
exit;
return $data;
}
Hope help for you !!!

Elequent Relationships Error

I have created a posts table & comments table for posts comments.
I want to get my posts with their comments ...
In my Post Model :
public function comments()
{
return $this->hasMany('App\Comment');
}
In my Comment Model :
public function post()
{
return $this->belongsTo('App\Post');
}
And this is my Controller :
public function show($ID)
{
try {
$post = Post::findOrFail($ID);
$comments = Post::find($ID)->comments;
$randomPosts = Post::all()->random(3);
return view('show', compact('post','comments','randomPosts'));
} catch (ModelNotFoundException $e) {
$posts = Post::orderBy('ID', 'DESC')->paginate(5);
return view('welcome', compact('posts'));
}
}
But I get this error :
Undefined property: Illuminate\Database\Eloquent\Collection::$ID
What is my problem ?
You need to assign the $ID variable to your id that you want to retrieve the post
For example let's say that you want the post with id 1 and its comment.
First you can retrieve the comments for first article like this:
$comments = App\Post::find(1)->comments;
foreach ($comments as $comment) {
echo $comment
}
Of course you can retrieve the post represents one comment.
For example the post that has the first comment:
$comment = App\Comment::find(1);
echo $comment->post->title;

Knp Paginator doesn't works with Query

I am settting up knp paginator, my composer is set to "knplabs/knp-paginator-bundle": "2.3.*" however It is not working for Query object.
Here is my query:
public function getCommentariesByUser(User $user) {
$qb = $this->createQueryBuilder('c');
$qb->innerJoin('c.item', 'i');
$qb->andWhere($qb->expr()->eq('i.user', ':user'))
->setParameter(':user', $user);
$qb->andWhere($qb->expr()->neq('c.user', ':user1'))
->setParameter(':user1', $user);
return $qb->getQuery();
}
How I am calling it in my action:
public function commentariesAction($page) {
$user = $this->getUser();
$commentaryRepository = $this->getDoctrine()->getRepository('My\MyBundle\Entity\Commentary');
$query = $commentaryRepository->getCommentariesByUser($user);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query,
$page/*page number*/,
15/*limit per page*/
);
return array('pagination' => $pagination);
}
It simply doesn't show results, however If I put $query->execute() It works. What's wrong?
Currently this is possible to use next combination of paginator packages for code above to work:
"knplabs/knp-components": "1.2.1",
"knplabs/knp-paginator-bundle": "~2.3"

Resources