I have two query results.I need to merge the query result based on a condition.
$portfolio = DB::table('architects_portfolio')->get();
$img=DB::table('architects_portfolio_images')
->distinct()->get(['architects_images_id']);
and my condition to merge is
architects_portfolio.id = architects_portfolio_images.architects_images_id
I have my first table 'architects_portfolio' which contain portfolio and second table 'architects_portfolio_images' which contain images of portfolio.I need to get only one image for now.But I get all images of portfolio now by using below code.But I need only one
$architect = User::find($user_id)->architect;
$portfolio = ArchitectsPortfolio::join('architects_portfolio_images as images', 'architects_portfolio.id', '=', 'images.architects_images_id')
->where('architects_portfolio.architects_id', $architect->id)
->select('architects_portfolio.id', 'architects_portfolio.architects_id', 'architects_portfolio.name', 'architects_portfolio.description', 'images.filename','images.architects_images_id')
->distinct()->get(['images.architects_images_id']);
My tables
architects_potfolio
architects_portfolio_images
You should try this:
$rsltArchitectDetails = DB::table('architects_portfolio')
->select('architects_portfolio_images.architects_images_id')
->join('architects_portfolio_images', 'architects_portfolio.id' , '=', 'architects_portfolio_images.architects_images_id')
->distinct()
->get();
$querys = DB::table('architects_portfolio');
$querys->select('architects_portfolio_images.architects_images_id');
$querys->join('architects_portfolio','architects_portfolio.id','=','architects_portfolio_images.architects_images_id')->distinct()->get();
The best way is to use relationships, then you could just load portfolios with related images:
$portfolios = ArhitectPortfolio::with('images')->get();
To make it work, you just need to define the images relationship in the ArhitectPortfolio model:
public function images()
{
return $this->hasMany(ArchitectPortfolioImage::class, 'architects_images_id');
}
Instead of merging we can make use of following code
$data = Architect::where("user_id", $user_id)->first();
$architect = User::find($user_id)->architect;
$portfolio = ArchitectsPortfolio::where('architects_portfolio.architects_id', $architect->id)->paginate(6);
foreach ($portfolio as $r) {
$image = ArchitectsPortfolioImages::where('architects_images_id', $r->id)->first();
if ($image) {
$r->filename = $image->filename;
}
}
Related
can someone help me on getting data from multi level relationships in laravel?
I want to do something like
BagageAnnouncement->Announcement->user->profile->"name of the field"
$data = $request->text;
$filter = BagageAnnouncement::whereHas('announcement',function ($query) {
})->whereHas('user', function ($query) {
})->whereHas('profile',function($query){
$query->where('level',$data)
})->get();
Thank you guys for you answers I finally figure out a solution
$data = $request->text;
$filter = BagageAnnouncement::whereHas(
'announcement.user.profile',
function ($q2) use ($data) {
$q2->where('level',$data);
}
)->get();
Not sure what you want as the result, but if you want a single result, you can do like this:
$bagage = BagageAnnouncement::where(specify_your_condition)->first();
$filter = $bagage->user->profile->name_of_the_field;
If you want an array of results, change the first() to get() and make use of foreach
Of course, you will need to set up the relationships in your models first.
I have a table like this:
CourseCategory can have duplicates in this table so that means a courseCategory can have multiple courseNames.
I am fetching all the courseCategory like this:
$courseCategories = Course::all()->pluck('courseCategory')->unique();
But Now I want to map all the courseNames beloging to a courseCategory in this collection.
so that I can have a $courseCategories which I want to be able to access it like this:
foreach($courseCategories as $courseCategory){
foreach($courseCatgory->courseNames as name){
//code
}
}
to get such a $courseCategories So far I have tried:
foreach ($courseCategories as $courseCategory) {
$courseCategories->$courseCategory = Course::where('courseCategory', '=', $courseCategory)->pluck('courseName');
}
dump($courseCategories);
which looks like this which is not quite right:
How Can I do this?
First get all data
$data = Course::all()
Get its category
$categories = $data->pluck('courseCategory')->unique();
Then do some mapping and filtering to get each of those categories child.
$result = $categories->map(function($category) use ($data) {
return $data->filter(function($row) use ($category) {
return $category == $row->courseCategory;
})->pluck('courseName');
});
I presently have 3 tables: Shows, Genres and Show_Genre (associates the two). I have a search form that submits a series of checkboxes with values into an array based on what genres they selected. Presently I want to associate the Shows table and the Genres table into a variable and run a query against it once for every genre checkbox selected. Then, once the selection is filtered, I can display the resulting Show objects that matched the users parameters.
My present setup is the following
public function searchShows(SearchRequest $request)
{
//$ShowsWithGenres give a collection inside a collection which I can't seem to even access its values without doing a bunch of ugly code using count() in a for loop
$ShowsWithGenres = Show::with('genres')->get();
$genres = $request->name;
if(isset($genres))
{
foreach($genres as $genre)
{
//iterate against the selection repeatedly reducing the number of results
}
}
}
Thanks.
You should use whereHas() and whereIn.
Perhaps something like this should do it:
$shows = Show::whereHas('genres', function($q) use($genre_ids)
{
$q->whereIn('id', $genre_ids);
})->get();
EDIT
Try this, however I'm unsure about the performance.
$query= Show::query();
foreach($genre_ids as $id){
$query->whereHas('genres', function($q) use($id)
{
$q->where('id', $id);
})
}
$shows = $query->get();
Using Eloquents whereHas() function you can query results based on the relation's data. http://laravel.com/docs/5.0/eloquent#querying-relations
public function searchShows(SearchRequest $request)
{
// Start a query (but don't execute it at this point as we may want to add more)
$query = Show::with('genres');
$genreNames = (array) $request->name;
// Check there are some genre names, if theres not any it'll cause an SQL syntax error
if (count($genreNames) > 0)
{
$query->whereHas('genres', function($subQuery) use ($genreNames)
{
$subQuery->whereIn('genre_name', $genreNames);
}
}
// Finally execute the query. $shows now contains only shows with the genres that the user has searched for, if they didn't search with any genres, then it contains all the results.
$shows = $query->get();
}
hi :) im parsing my database in a table with pagination !!
this is my controller
$theme = Theme::all();
$questions = questionList::paginate(10);
$the = "";
return View::make('home.home')
->with('user',Auth::user())
->with('theme', $theme)
->with('the' , $the)
->with('questions',$questions);
and my view i have {{ $questions->links(); }} under my table and it's working fine !!
the thing is that i have a list of themes to sort data in the table so when i click on divertisement i get divertisement data.
the problem is that when i paginate it return to the get request and give me all the data !! what is the problem thx :)
To add a filter/sort you also need to add that in a where clause in your query and also you need to append the query string in your pagination links. For example:
public function showPosts()
{
$questions = app('questionList');
if($filter = Input::get('filter')) {
$questions = $questions->where('theme', $filter);
}
$questions = $questions->paginate(10);
if(isset($filter)) {
$questions->appends('filter', $filter);
}
return View::make(...)->with(...);
}
In your view you need to create links to this method (Probably using a route name or url) with filter query string. So, for example:
Divertisement
SomethingElse
I am working on an application where part of it needs to search through a number of different fields on the same model with AND - AKA find age whereBetween $from and $to AND where gender is $gender. Where I am getting lost is this model has a many to many relationship with Category and I need to filter by category in the same query. I am trying to do this in one query because it needs to be pretty fast.
Here is what I have so far:
$categories = Input::get('categories');
$query = Program::with('categories')->whereIn('category', $categories)->query();
if ($ages = Input::get('ages')) {
$query->whereBetween('age',array($from,$to));
}
if ($gender = Input::get('gender')) {
$query->where('gender','like', $gender);
}
// Executes the query and fetches it's results
$programs = $query->get();
I have put this together from so many different sources that I would like to know if this even works, or if it is the most efficient method possible. There is of course a table programs, a table categories, and a table program_category with columns id, program_id, and category_id.
Thanks in advance for your help!
So, in the end figured it out:
$query = Program::whereHas('categories', function($q) use ($categories)
{
$q->whereIn('categories.id', $categories);
});
'categories' is the name of the relationship function on my Program model. $categories is my array of category ids. Thanks again for your help.
This will work if fields are available in the right table as you queried for and you may write it like this:
$categories = Input::get('categories');
$query = Program::with('categories')->whereIn('category', $categories);
// declare $from and $to if not available in the current scope
if ($ages = Input::get('ages')) $query->whereBetween('age', array($from,$to));
if ($gender = Input::get('gender')) $query->where('gender', $gender);
// Executes the query and fetches it's results
$programs = $query->get();