I want to build a custom find function that retrieves bands for a given genre, i have tried this but the function can't access to the parameter $genre:
public function findGenre(Query $query, array $options)
{
$genre = $options['genre'];
$bands = $this->find()->contain([
'Genres' => function($q){
return $q->where(['Genres.id' => $genre]);
}
]);
return $bands;
}
I can access the $genre outside the contain() method, but not inside it.
My question is, how can i pass the $genre var to the function($q) inside the contain method.
I found where the problem is, i had to use the keyword use after the function($q), so that part of the code will look like this
$bands = $this->Bands->find()->contain('Genres', function($q) use ($genre){
return $q->where(['Genres.name'=>$genre]);
});
Also,the contain() method returns all the data even if the bands don't belong to a genre, but when i replaced it with matching() it worked just fine.
I hope this will help anyone who is having a similar problem in the future.
I was facing same issue but now it's resolved. I will explain you step by step:
My tables are:
articles: id,name,status,created
tags:id,name,status,created
articles_tags: id,article_id, tag_id
my query is this:
I want to pass my $tag_data['slug'] in matching variable but
directly this variable is not working in query. So I put in simple
$uses variable and now it's working properly.
$uses = $tag_data['slug'];
$contain_article = ['Tags'];
$query = $this->Articles->find('All')
->where(['Articles.status' => '1'])
->contain($contain_article)
->matching('Tags', function ($q) use ($uses) {
return $q->where(['Tags.slug' => $uses]);
});
Please try this :-)
Related
I am using paginate() on my Controller to pass data to the View. This is my index function using the paginate()
$userData = User::with('jobUnit')
->select('nip','name','address','phone','email','unit_id')
->paginate(10);
return view('users.index', [
'users' => $userData
]);
This is the result:
In the other function, I needed to add some IF conditions on the queries that is look like this:
$keyword = $request->keyword;
$searchedData = User::with('jobUnit')->select('nip','name','address','phone','email','unit_id');
if ($request->searchFilter == 'nip') {
$searchedData->where('nip','like','%'.$keyword.'%');
}
$searchedData->paginate(10);
The results is different, which is a problem for me because I am using the pagination links in the View. Here is the results:
Does the pagination() not working? Because I tried using the get() as well which should returns "Collection", but it was still returning the "Builder" results.
you need another variable to store the return data
$searchDataPaginated = $searchedData->paginate(10);
or using the current one if you want
$searchedData = $searchedData->paginate(10);
I'm having a problem with laravel., I'm trying to send the variable $codes on my view :
$codes = Code::where('user', $id)->get();
return view('user.edit', ['user' => $user,'codes' => $codes]);
But I get that error Too few arguments to function e(), 0 passed in
The variable $user goes well but not the variable code, anyone have an idea for a solution?
Thank you all
use this:-
return view('user.edit')->with('codes',$codes);
you can get the user with :-
Auth::user()->name;
I'm pretty sure, if you are following Laravel naming conventions, in your eloquent query the column name is user_id and not user. Also if you are using such querys, and you have foreign, you can define relations in your models, like the following:
Code Model
public function user
{
$this->belongsTo(User::class);
}
User Model
public function codes
{
$this->hasMany(Code::class);
}
Instead of line
$codes = Code::where('user', $id)->get();
You can go with:
$codes = $user->codes();
If you don't have the foreigns in your database
$codes = Code::where('user_id', $id)->get();
If non of these helps, please dd() your variables before returning the view, and share result with me in comment. In laravel 7 how you return view is good.
Hello StackOverflow Community, after some research we decided to ask you for the solution.
We would like to have a link with a defined array. The goal would be to have a link such as:
www.testurl.com/restaurants/CUISINENAME/
Then, we would just like to see all restaurants with the particular cuisine. The filter is currently working on the website with a checkbox.
Router
Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
Route::get('/', 'RestaurantController#showAllRestaurants');
Route::post('/', 'RestaurantController#showAllRestaurants');
});
Controller
if (request('cusineName')) {
if (is_array(request('cusineName'))) {
$cusineName = request('cusineName');
} else {
$cusineName = (explode(",", request('cusineName')));
}
$all_restaurant = $all_restaurant->whereIn('restaurant_cuisines.type_cuisine_id', $cusineName);
}
We were thinking of setting the array into the controller. Does anyone have any other suggestions?
Firstly, your array check is fine. However, you should return false if no cusineName has been passed through (As there is no point continuing).
You then are able to do an eloquent query for restaurants which have a particular cuisine by using the whereHas() method:
...
$restaurants = Restaurant::whereHas('cuisine', function($query) use ($cuisines) {
$query->whereIn('id', $cuisines);
})->get();
...
In the example, we pass through $cuisines to be used within the whereHas() eloquent method, to then use in the whereIn() method. This will check against the array if the cuisine's id is found.
I've followed the instructions on the Laravel documentation for pagination with appends([]) however I'm having a little trouble with the persistence of these parameters.
Say for example, I pass home?category=Cars&make=Tesla to my view. What is the best way to paginate with them Get requests?
Right now I've passed the category as a parameter to the view as (where category is the model i've grabbed findOrFail with the request('category');)
$category_name = $category_model->name;
And then in my view it's like so:
{{ $vehicles->appends(['category' => $category_name])->links() }}
But when I go between pages in the pagination, this $category_name value doesn't seem to persist. Whats the recommended way to achieve what I want?
Thanks.
You can append the query string in your controller when you paginate the result. I'm not sure if that was your only question or even regarding applying the query string as a condition. So here is a sample showing you how to do both. This should give you an idea of how to do it. I just assumed the column names in this example.
$category = request('category');
$make = request('make');
$vehicles = Vehicle::when($category, function ($query) use ($category) {
return $query->where('category', $category);
})
->when($make, function ($query) use ($make) {
return $query->where('make', $make);
})
->paginate(10);
$vehicles->appends(request()->query());
return view('someview', compact('vehicles'));
I am writing a function in my controller to return a json response. This is my function:
public function play($id){
$quiz = Quiz::find($id);
$questions = $quiz->question()->get();
return Response::json(array(
'quiz' => $quiz,
'questions' => $questions));
}
This works perfectly, however, I also have answers related to questions.
In my Question model I have this answer() function:
public function answer(){
return $this->hasMany('Answer');
}
And in my Answer model I have of course my question() function:
public function question(){
return $this->belongsTo('Question');
}
How do I get my answers? I can't use;
$answers = $question->answer()->get;
And I can't use
//get the questions using query, but this returns nothing
$questions = Question::where('quiz_id', '=', $id);
$answers = $questions->answer()->get;
I hope I'm clear enough, I tried searching but I can't find anything, anybody please help :)?
You can use eager loading to get a result where answers are nested inside questions:
$questions = $quiz->question()->with('answer')->get();
If you just want to get answers of one single question do this:
$question = Question::find(1);
$answers = $question->answer()->get();
Or simply use the dynamic property:
$answers = $question->answer;
Sidenote: I suggest you name hasMany relations in plural. answers instead of answer. This makes semantically more sense and tells you clearly if you get a collection or a single model as result.