Eloquent selfreferencing hierachie to json - php

I'm trying Eloquent now with high expectations.
I have a category tree.
Everything works fine. But now I want to return the whole tree as json. Therefore I'm doing:
$categories = Category::whereNull('parent_id')->get()->toJson();
And only get the adam and eve nodes.
[{"category_id":1,"name":"Boats","slug":"boats","parent_id":null},
{"category_id":2,"name":"Paddles","slug":"paddles","parent_id":null}]
Which is good basically. How to integrate the childs recursivly? No "native" eloquent way for that?
Tree like this:
select * from categories;
+-------------+----------------+----------------+-----------+
| category_id | name | slug | parent_id |
+-------------+----------------+----------------+-----------+
| 1 | Boats | boats | NULL |
| 2 | Paddles | paddles | NULL |
| 3 | Kayaks | kayaks | 1 |
| 4 | Canoes | canoes | 1 |
| 5 | Carbon Paddles | carbon-paddles | 2 |
| 6 | Vajda K1 | vajda-k1 | 4 |
| 7 | Dagger RPM | dagger-rpm | 3 |
| 8 | Kober Viper | vober-viper | 2 |
+-------------+----------------+----------------+-----------+
8 rows in set (0.03 sec)
and a model like that
class Category extends Eloquent {
protected $table = 'categories';
protected $primaryKey = 'category_id';
protected $fillable = array("name", "slug", "parent_id");
public $timestamps = FALSE;
// each category has many subcategories
public function childs() {
return $this->hasMany('Category');
}
// each category belogs to one parent category
public function parent() {
return $this->belongsTo('Category');
}
}

AS far as I know there is no native way to get a recursive result from eloquent directly.
For the first level you would use:
$categories = Category::whereNull('parent_id')->with('childs')->get()->toJson();
For the next level (and likewise further on):
$categories = Category::whereNull('parent_id')->with(['childs' => function ($query) {
$query->with('childs');
}])->get()->toJson();
Using Lazy Eager Loading you'll be able to build your own PHP giveMeMyCatTree() methode.
Does this help?

Related

How to filter post by category in laravel?

I am currently trying to make a relation between 3 tables.
post
id
name
category
id
name
post_category
id
post_id
category_id
Database
post
| 1 | post1 |
| 2 | post2 |
| 3 | post3 |
category
| 1 | cat1 |
| 2 | cat2 |
| 3 | cat3 |
post_category
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
| 3 | 2 | 2 |
| 3 | 1 | 3 |
Model Post.php
public function getCategory()
{
return $this->belongsToMany(Category::class, 'post_category');
}
PostController.php
$data = Post::with('getCategory')->get();
It returns correct post list.
Now i want to filter the post by category. I try, but it not working
$categoryId = [1,2];
$data = Post::with('getCategory')->whereHas('category', function ($query) use ($categoryId) {
$query->whereIn('id', $categoryId);
})->orderBy('id','DESC')->get();
please help me
use Laravel 5.4
apparently everything is fine!
One suggestion is to add two more parameters to the belongsToMany method, like:
public function getCategory()
{
return $this->belongsToMany(Category::class, 'post_category', 'post_id', 'category_id');
}
https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_belongsToMany
You should rename your getCategory() function to simply category(). That makes the relation names much more straightforward and probably fixes your issue.
THEN, you should be able to call whereHas('category', ...).
If it still does not work, simply chain a ->toSql() to any of your queries and debug the actual query that way.

Eloquent Relations With Multiple Columns

So I have the following match table which contains the numbers of the teams that participated in that match. I want to set up a relationship with the teams which looks something like this:
Teams Table
| id | number | name | etc |
| 1 | 1234 | Example | etc |
| 2 | 2345 | Example | etc |
etc...
Matches Table
| id | match | red1 | red2 | blue1 | blue2 |
| 1 | 1 | 1234 | 1710 | 673 | 2643 |
| 2 | 2 | 2345 | 1677 | 4366 | 246 |
etc...
I want to have something like $this->match->where("match", "=", "2")->first()->teams();.
I have tried using hasMany() but I can't seem to get to use the red1, red2, blue1, blue3 columns.
What I have tried:
class Matches extends Model
{
protected $table = "match_table";
protected $fillable = [
"match_id",
"time",
"bluescore",
"redscore",
"red1",
"red2",
"red3",
"blue1",
"blue2",
"blue3",
];
public function teams()
{
return $this->hasMany("App\Models\Teams", "number", ["red1", "red2", "blue1", "blue2"]);
}
}
What I ended up doing was just looping through each column I wanted and then just returning a new Collection with the results in it.
public function teams()
{
$result = [];
foreach($this::$teamColumns as $column) {
$result[] = $this->hasMany("App\Models\Teams", "number", $column)->first();
}
return new Collection($result);
}

Eloquent Multitable query

I trying to create a search function in php with Slim and Eloquent 5.1, but i have a problem to get the results from the database.
This are my tables
Listing Table
id | title | description | price | size |etc...
-----|--------|-------------|-------|--------|-------
1 | list1 | some text | 500 | 50 |
2 | list2 | some text | 700 | 80 |
3 | list3 | some text | 350 | 120 |
Listings Option Table
id | id_listing | opt_name | opt_value
-----|-------------|-------------|----------
1 | 1 | rooms | 3
2 | 1 | baths | 4
3 | 2 | rooms | 8
4 | 3 | baths | 1
5 | 3 | rooms | 6
Listings Images Table
id | id_listing | file_name
-----|-------------|-------------
1 | 1 | file_1.png
2 | 1 | file_2.png
1 | 2 | file_3.png
2 | 3 | file_4.png
Now i found a way to get all the results with all they options with
listado = $app->listing->with(['options'],['imgs'])->get();
My classes
//Listing Class
class Listing extends Eloquent{
protected $table = 'listings';
public function options(){
return $this->hasMany('Casas\Listing\ListingOption', 'id_listing', 'id');
}
public function imgs(){
return $this->hasMany('Casas\Listing\ListingImg', 'id_listing');
}
}
// ListingOption class
class ListingOption extends Eloquent{
protected $table = 'listings_options';
public function listings()
{
return $this->belongsTo('Casas\Listing\Listing', 'id_listing');
}
}
// ListingImg Class
class ListingImg extends Eloquent{
protected $table = 'listings_imgs';
public function listings()
{
return $this->belongsTo('Casas\Listing\Listing', 'id_listing');
}
}
but i cannot find a way to filter the query the way i want to. I tried many thing.
Example: I want to get all the listings that have price less than 600 and have more than 2 rooms.
So that be listing id 1 and 3
If anyone can help create this query i'll appreciate that.
What you want want to use is whereHas.
When accessing the records for a model, you may wish to limit your results based on the existence of a relationship
$result = $app->listing->with('options', 'imgs')
->whereHas('options', function ($query) {
$query->where('opt_value', '>', 2)
->where('opt_name', 'room');
})
->where('price', '<', 600)->get();
So what we are doing here is, eager loading options and imgs tables and fetching only the listings that have at least one option where opt_value is bigger than two and opt_name is room.
You can find more in info about querying relationships in here
I would try something like this:
$result = $app->listing->with(
['options' => function ($query) {
$query->where('opt_value', '>', 2);
}
],
['imgs'])->where('price', '<', 600)->get();
The last, outer where should be assigned to listing. The inner where, passed in a callback, should be assigned to options directly.
Take a look at the docs (the "Constraining Eager Loads" section).

Laravel Eloquent how to return one to many to many collection

Is it possible to get an eloquent relation based on a One->Many->Many relationship? In the below example, I would want to get all of the gallery_votes for a given user.
users
+----+---------------+
| id | user_name |
+----+---------------+
| 1 | bob |
| 2 | sam |
+----+---------------+
galleries
+----+---------------+-----------+
| id | gallery_name | user_id |
+----+---------------+-----------+
| 1 | Alaska Pics | 1 |
| 2 | Texas Pics | 1 |
| 3 | California | 2 |
| 4 | Cars | 2 |
+----+---------------+-----------+
gallery_votes
+----+---------------+--------+
| id | gallery_id | vote |
+----+---------------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 2 | 1 |
+----+---------------+--------+
The relationships are setup as follows.
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {{
...
public function galleries()
{
return $this->hasMany('App\Gallery');
}
}
and
class Gallery extends Model {
....
public function votes()
{
return $this->hasMany('App\GalleryVote');
}
}
$user->galleries returns a collection of associated galleries, so that works correctly. and $gallery->votes returns a collection of associate votes, so that also works correctly. But $user->galleries->votes gives
Undefined property: Illuminate\Database\Eloquent\Collection::$votes on line 1
Is what I'm trying to do even possible with straight Laravel? Or do I need to write the SQL directly?
The "has many through" relation provides a convenient short-cut for accessing distant relations via an intermediate relation.
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {{
...
public function galleries()
{
return $this->hasMany('App\Gallery');
}
public function votes()
{
return $this->hasManyThrough('App\GalleryVote', 'App\Gallery');
}
}
Now $user->votes will return all votes for that user. Remember you need to create the GalleryVote eloquent model as well.
You can read more about that type of relation, as well as some example usage in the documentation.

Laravel getting results from model belongsToMany relationship back to controller

I'm using Laravel 5 and Eloquent, I have 3 tables setup:
photos
+----+---------------+------------------+
| id | photo_name | photo_location |
+----+---------------+------------------+
| 1 | kittens.jpg | C:\kittens.jpg |
| 2 | puppies.jpg | C:\puppies.jpg |
| 3 | airplanes.jpg | C:\airplanes.jpg |
| 4 | trains.jpg | C:\trains.jpg |
+----+---------------+------------------+
photo_set (pivot table)
+------------+----------+
| set_id | photo_id |
+------------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
+------------+----------+
sets
+----+----------------+
| id | description |
+----+----------------+
| 1 | cute animals |
| 2 | transportation |
+----+----------------+
I created a belongsToMany relationship in my photos and sets models to link these two together.
class Photo extends Model {
public function sets()
{
return $this->belongsToMany('App\Set');
}
}
and
class Set extends Model {
public function photos()
{
return $this->belongsToMany('App\Photo');
}
}
However I'm trying to reference the $Sets->photos() model function in my controller, so that given a set of id=1, I can return the photo_location value for each row [C:\kittens.jpg,C:\puppies.jpg], but I don't know how to access it..
Also, I can "sort of" access this information in the view with:
#foreach($sets as $set)
{{$set->images}}
#endforeach
but it looks like it only iterates through once and returns a json (?) of the necessary information, though I'm not sure how to parse that to regular HTML either.
So in short, I'm having trouble accessing this data (photo_location) from both the controller and the view
Use Collection::lists()
$locations = Set::find(1)->photos->lists('photo_location');
It will return an array ['C:\kittens.jpg', 'C:\puppies.jpg']
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_lists
In your controller try this :
$result = PhotoSet::where('set_id', $set_id)
->with('sets')
->with('photos')
->get();
$photo_location = $result->photos->photo_location;
here PhotoSet is the model for photo_set table, because it is a pivot table we will be able to access both sets and photos table from it.

Categories