I'm using Laravel4 on my project and I have those tables on Mysql Database ..
Users :
- id - username
1 - user1
2 - user2
-
Blogs :
- id - blog_name
1 - blog1
2 - blog2
-
Roles :
- id - role_name
1- owner
2- editor
-
Assigned_roles:
- id - role_id - user_id - blog_id
1 - 1 - 1 - 1 // user1 owns blog1
2 - 2 - 1 - 2 // user1 editor in blog2
In laravel, I can successfully deal with three tables using hasMany and BelognsToMany relationship .. but 4 tables! it was too complecated for me,
I have spent 2 days on this but I couldn't figur out how to achieve this in a simple way with one query or so.
What I'm trying to do is, When I login in to blog1 , I need to list all blogs that I have access on them , for example :
blog (owner)
blog2 (editor)
something like..
$user = User::find(1);
print_r($user->access_blogs); // list blogs that I have assigned roles to them
and maybe later ..
$myrole = Blog::find(1)->where('user_id', 2);
echo $myrole->myrole; // editor
--
// 1
use LaravelBook\Ardent\Ardent;
class User extends Ardent {
public function blogs()
{
return $this->hasMany('Blog');
}
}
// 2
use LaravelBook\Ardent\Ardent;
class Assignedroles extends Ardent {
protected $table = 'assigned_roles';
}
.. etc , you got the idea .. :)
Related
I have 2 tables i.e. users and user_managers, with the following structure and data:
users
id
name
employee_number
1
Employee One
ACB1234
2
Employee Two
XYZ1234
3
Employee Three
EFG1234
4
Employee Four
HIJ1234
user_managers
id
user_id
manager_of_user_id
1
1
2
2
2
3
3
4
1
4
5
4
I want a recursive function to get the complete chain of the managers. For example, if query for user_id = 4, I should get the following result:
result
user_id
name
employee_id
comments
1
Employee One
ABC1234
user managed by 4
2
Employee Two
XYZ1234
user managed by 1
3
Employee Three
EFG1234
managd by user 2
5
Employee Five
JKL1234
manager of user 4
The table above is just for the clarification, I am looking for a recursive function to get the above result.
I have tried the below solution but it is working for only 1 level.
public static function getCompleteChain(User $user)
{
$managerUsers = $user->managers;
foreach ($managerUsers as $manager) {
$manager->user = User::where('employee_id', $manager->manager_of_user_id)->first();
self::getCompleteChain($manager->user);
}
return $managerUsers;
}
Thanks in advance.
vehicles (Parent of car_details)
id - some_column1
1 - some_value
2 - some_value
...
cars (Parent of car_details):
id - some_column2
1 - Benz
2 - BMW
...
car_details (Child):
id - vehicle_id - car_id - some_column3
1 - 1 - 1 - some_value
2 - 1 - 1 - some_value
3 - 1 - 2 - some_value
4 - 1 - 2 - some_value
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
Now in my blade view I want to iterate over the cars and its child's details separately:
First Car:
#foreach($carDetails as $carDetail)
<ul>
<li>Car: {{$carDetail->car->some_column2}}</li>
<li>Car Detail: {{$carDetail->some_column3}}</li>
</ul>
#endforeach
Second Car:
#foreach($carDetails as $carDetail)
<ul>
<li>Car: {{$carDetail->car->some_column2}}</li>
<li>Car Detail: {{$carDetail->some_column3}}</li>
</ul>
#endforeach
//So on...
The problem is that the car details are showing continuously however I want to separate First Car details from Second details and the cars showing after that.
How can I achieve this?
I found it :)
I used groupBy not in SQL, but after fetching the data.
$vehicle = Vehicle::find($id);
$vehicle->cars = $vehicle->cars->groupBy('car_id');
Thanks anyway
CONTEXT
I am managing products. This is a shoe store. I would like to offer a view of the other variants.
The database is shaped like this:
For example you can have a leather shoe (id 1), and there is 3 variants of this shoe: a black (id 1), a brown (id 2), and a grey (id 3).
What I try to do is to construct a Laravel relationship to be able, from one variant, to get its siblings. Here is what it looks like in the database according to the example I mentioned.
SHOE
id
====
1
SHOE_VARIANT
id shoeId colorId
===================
1 1 1
2 1 2
3 1 3
...
8 2 5
9 3 2
10 3 4
In this case, if the user is viewing the black variant (id 1), I whish I could show him the 2 others variants (brown, id 2, and grey, id 3).
QUESTION
How can I construct a Laravel relationship in order to retrieve siblings from a parent id, and make sure the current record itself is not included?
EXPERIMENTS
I already tried to construct a relationship like below, but the current record itself is included and I can't figure out how to exclude the record itself because I did not find how to get the current id of the record.
// app/ShoeVariant.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ShoeVariant extends Model {
public function siblings() {
return $this->hasMany("App\ShoeVariant", "shoeId", "shoeId");
}
}
This means:
For the current shoe variant, get the shoe variants that matches knowing that you should match the foreign column named "shoeId" and the local column named "shoeId"
So if 2 shoe variants share the same column "shoeId", this works. Stuck in excluding the current record from these results.
This should do what you want:
public function siblings() {
return $this->hasMany('App\ShoeVariant', 'shoeId', 'shoeId')
->where('id', '!=', $this->id);
}
Just filter out the current variant by id and get all the others.
Alternatively, you can just make a new property:
public function getVariantsAttribute() {
return $this->siblings->reject(function($elem) {
return $elem->id == $this->id;
});
}
And then use it in code like:
$variants = $model->variants; // all except this one
I want to know how to make relations between tables:
I have a shops table and a users table.
I want to associate to the users table: shops (id) AND area (that's a group of shops).
I don't know how to do that in a beautiful and right way.
For example, in a very complex way we can have:
Area A contains [Area B (3 shops) and Area C (1 shop)] and 1 shop : total of 5 shops.
John is associate to the Area A, Area X and 3 other shops (no area).
How to represent this in database?
It's like a recursive thing :(
Thanks by advance!
You'd probably want to have the following tables:
users
- id
- username
shops
- id
- name
areas
- id
- name
- parent_id
user_shop
- user_id
- shop_id
area_shop
- shop_id
- area_id
Then for your example, a row might look like
users
id: 1
username: jappleseed
shops
id: 1
name: some shop name
id: 2
name: some other shop name
areas
id: 1
name: Area A
parent_id: null
id: 2
name: Area B
parent_id: 1
id: 3
name: Area C
parent_id: 1
area_shop
area_id: 2
shop_id: 1
area_id: 2
shop_id: 2
Then you could define your relationship for nested areas like:
class Area extends Model {
/**
* Parent Area
*/
public function parent_area()
{
return $this->belongsTo('Area', 'parent_id');
}
/**
* Child Areas
*/
public function child_areas()
{
return $this->hasMany('Area', 'parent_id', 'id');
}
}
The other relationships should be fairly straight forward.
This is untested and might need a little tweaking, but the overarching idea should get you there. Otherwise, using a prebuilt nested set library would also work.
I have a table in my DB with following headers: user, category, title, marks
And values in it are like this
User1 - Food - Pizza - 19
User2 - Drinks - Cola - 18
User2 - Food - Spaghetti - 19
User1 - Food - Potato - 15
User1 - Drinks- Pepsi- 10
User2 - Food - Onion - 11
User1 - Food - Tomato - 10
This is also the way it printed in my CSV file, But what I'm trying to achieve in my CSV file is this format
A(user) B(=category) C D E
1. User 1 Food Pizza -19 Potato - 15 Tomato - 10
2. Drinks Pepsi
3. Empty row
4. User 2 Food Spaghetti-19 Onion-11
5. Drinks Cola-18
6. Empty Row
7. A third user with his category's
This basically what I'm trying to achieve - Sorting the user then sorting the category's that the user has and then at every category the user has print the titles and the scores. But How can I sort and do logic for putting in a CSV?
I have the following code
Model:
function CSV(){
$this->load->dbutil();
$this->db->select('user, category, title, score');
$query = $this->db->get('Table_restaurant_scores');
return $this->dbutil->csv_from_result($query, ";", "\r\n");
}
Controller:
function exporting(){
$this->load->model('restaurant_mdl');
$this->load->helper('download');
$name = 'restaurant_scores.csv';
$data = $this->restaurant_mdl->CSV();
force_download($name, $data);
}
Can someone help me out with this?
It depends on how you fetch your data in your model. You need to add order_by statement in your query e.g
$this->db->order_by("user", "asc");
$this->db->order_by("category", "asc");
// Produces: ORDER BY user DESC, category ASC