Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My controller:
function index:
public function index(Request $request)
{
//there is error Undefined variable: jobs
return view('jobs.index')->with('jobs', $jobs);
}
Why
You need to add a $jobs variable in the method.
public function index(Request $request)
{
$jobs = // Some code here;
return view('jobs.index')
->with('jobs', $jobs);
}
the best way to achieve this is by doing something like this:
public function index(Request $request)
{
$data['jobs'] = DB::table('your-table')->get();
return view('jobs.index',$data);
}
with the above code snippet, you could be able to use the variable job in your blade like this:
#foreach($jobs as $j)
{{ $j->fieldName }}
#endforeach
the above is use when you use get() in your database query. Hope it helps!!!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I was using data table template for frontend. It already have per page and pagination and search. So, how should I do for query. I did like this. I also wanna use search and paginate from template datatable.
public function index()
{
$users = User::all();
return view('users.list', compact('users'));
}
You can use the laravel built-in pagination:
public function index()
{
$users = User::paginate(15);
return view('users.list', compact('users'));
}
Then in your view you can loop through your user list and use the pagination links like this:
{{ $users->links() }}
You should customised your code to do so and here you go:
public function index(Request $request)
{
$search = $request->query('search');
$customers = customers::where('name', 'like', "%$search%")
->orWhere('email', 'like', "%$search%")
->paginate(10);
return view('App.Customer.customerList', compact('customers'));
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
when trying to view a specific blog article i get the error Trying to get property 'title' of non-object
here is my controller controller code
public function theblog(Request $req, $slug){
$blog = blog::find($slug);
DB::table("blogs")->where('slug', "$slug")->increment('views');
return view('myblog',compact('blog'));
}
public function theblog(Request $req, $slug){
$blog = blog::where('slug', $slug)->firstOrFail();
DB::table("blogs")->where('slug', $slug)->increment('views');
return view('myblog',compact('blog'));
}
Some tips.
your primary key is unlikely to be 'slug' so you need to use a where
don't quote php parameters
your classes should start with an uppercase, ie Blog not blog
you should probably only increment views once per user
public function theblog(Request $req, $slug){ $blog = blog::find($slug);
dd($slug);
DB::table("blogs")->where('slug', "$slug")->increment('views'); return view('myblog',compact('blog')); }
Check if there is value for $blog...if it is null(that is the problem) then you should check you DB?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to get the data of a Model and also the data of the foreign key in the Model
For Example:
A Post Model has a user_id as a foreign key. How can I write an Eloquent query so that I can get the data of a Post and in place of the user_id, I get the data of the user_id ?
Use with method when querying your post to include the user.
$post = Post::with('user')->first();
// or
$posts = Post::with('user')->get();
At Post model
public function user(){
return $this->belongsTo(User::class,'user_id');
}
Now at your PostController
Guess you have a method which is use for showing post
public function show($id){
$post = Post::query()->findOrFail($id);
}
Now if you want to access which user posted then there has two ways.
Way 1 :
public function show($id){
$post = Post::query()->with('user')->findOrFail($id);
}
Way 2 :
public function show($id){
$post = Post::query()->findOrFail($id);
echo $post->user->firstName;
}
Note :
If you just write $post->user then it will show/return User table according to user_id column.
If you want to access these user row column then . Simply $post->user->column_name;
Enjoy Coding.
I've been making a query to return all those people who have answered all the questions in a certain block. I've been successfull in getting all the users who answered like this:
public function getUsers()
{
$question_ids = $this->questions->pluck('id');
return User::query()->whereHas('question_answers', function ($query) use ($question_ids) {
$query->whereIn('question_id', $question_ids);
})->get();
}
And I have a function to check if a certain user has answered all the questions in a certain block.
public function hasAnsweredAll(User $user)
{
return (boolean) ($this->questions->count() == $user->question_answers()->whereHas('question', function($query){
$query->where('question_block_id', $this->id);
})->count());
}
Now I get the users I want like the following:
$users=[];
foreach($question_block->getUsers() as $user){
if($question_block->hasAnsweredAll($user)){
$users[] = $user;
}
};
Now my question is, instead of foreach, can I involve my hasAnsweredAll() method in a whereHas() or can I avoid somehow using foreach? I've searched the net for hours now, but no luck, so any help is appreciated.
You can take a look to this article from Jonathan Reinink, he uses subqueries to solve a problem like the one you mentioned
I hope this works for you
I've managed to work out a solution for my problem. It is so easy, and I've missed this so far when reading the documentations. whereHas() can take an operation and an int as parameters, so it will return instances according to that.
So like this:
public function getUsers()
{
$question_ids = $this->questions->pluck('id');
return User::query()->whereHas('question_answers', function ($query) use ($question_ids) {
$query->whereIn('question_id', $question_ids);
}, '=', $this->questions->count())->get();
}
I don't even need the hasAnsweredAll method anymore, because only users who answered all are returned. So instead the foreach it will look like this:
$users = $question_block->getUsers();
I searched endlessly for a question that answered my question here and I didn't find it. My question is as follows, I have 3 models: User, Post and Comments. Where user has a relationship with one to many posting, and Post has a relationship with one to many comments as well. How can I get all of the user's comments on all posts?
Currently my solution looks like this:
Models Users:
public function comments(){
$comments = array();
foreach ($this->posts()->get() as $el) {
foreach ($el->posts()->get() as $nEl) {
array_push($comments, $nEl);
}
}
return collect($comments);
}
I would like a less expensive and native solution for laravel, if any.
On your User model, something like:
public function getComments()
{
return Comment::whereHas('posts', function ($query) {
$query->where('user_id', $this->id);
})->get();
}
Also see:
https://laravel.com/docs/6.x/eloquent-relationships#has-many-through