How to make dependable dropdrown from pivot table in laravel - php

I have 3 table's:
course table:
id | course
---|-----------
1 | Laravel
---|-----------
2 | PHP
---|-----------
3 | JS
subject table:
id | subject
---|-----------
1 | subject 1
---|-----------
2 | subject 2
---|-----------
3 | subject 3
course_subject (pivot) table:
id | course_id | subject_id
---|-----------|------
1 | 1 1
---|-----------|------
2 | 1 | 2
---|-----------|------
3 | 2 | 3
Course.php
public function subjects()
{
return $this->belongsToMany('App\Subject');
}
Subject.php
public function courses()
{
return $this->belongsToMany('App\Course');
}
I want to make a dependable dropdown in my Laravel application:
when I will select a course from select box another select box will get the subject name.
i got the subject id but not subject name. How can i do this?

Related

Should i separate Pivot tables for each feature in laravel?

I'm building review product application where there are 3 database tables
- Review
- Categories
- Affiliates
At first i created a pivot table with 2 entries review_id & category_id & the sync was successfull. Later i added another column "affiliate_id" to the pivot table & the sync was successfull but with multiple entries. Let me explain
Here's my controller for review -
$result->affiliates()->sync($aff_ids);
$result->category()->sync($product);
The above code is creating sperate pivot entries -
Pivot Table -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | null | 1 |
| 1 | 1 | null |
+-----------+-------------+--------------+
Is the above pivot table correct or should i create seperate pivot tables for affiliates & categories ?
If the pivot table is not correct & if there is no need for seperate pivot tables then what i want to achieve is -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | 1 | 1 |
+-----------+-------------+--------------+
The affiliate_id column can have have multiple entries so final result i want is -
+-----------+-------------+--------------+
| review_Id | category_id | affiliate_Id |
+-----------+-------------+--------------+
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
+-----------+-------------+--------------+
use withPivot;
in Review Model class
public function category()
{
return $this->belongsToMany(Category::class)->withPivot('affiliate_id');
}
Then
foreach ($categories as $category) {
$category_id_array[$category->id] = ['affiliate_id' => $affiliate->id];
}
//Insert into offence_photo table
$review->category()->sync($category_id_array, false);//dont delete old entries= false

How to fetch record from three table in laravel eloquent

I have four tables
jobposts:
id | user_id | cat_id | job_title
1 | 1 | 1 | job 1
2 | 1 | 2 | job 2
3 | 2 | 3 | job 3
4 | 1 | 3 | job 4
categorymasters:
id | category_name
1 | cat1
2 | cat2
3 | cat3
4 | cat4
lastsubcategoryselectedbycompanies:
id | jobposts_id | lastsubcategorymasters_id
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
4 | 1 | 3
lastsubcategorymasters:
id | LastSubCategoryName
1 | lastsubcat1
2 | lastsubcat2
3 | lastsubcat3
4 | lastsubcat4
jobposts have unique rows.
lastsubcategoryselectedbycompanies is a mapping of jobposts and lastsubcategorymasters.
Now assume some user is logged in with their credentials (EX: take user_id 1 in jobposts). Now I need to show LastSubCategoryName in a comma separated list from the lastsubcategorymasters table, grouped by the jobposts, lastsubcategoryselectedbycompanies and lastsubcategorymasters tables.
allpostedjob.blade.php is:
#foreach($jobposteddetails as $jobposteddetail)
<tr>
<td>{{ $jobposteddetail->job_title }}</td>
</tr>
#endforeach
cotroller is:
public function index()
{
$user = Auth::user();
$jobposteddetails = jobpost::with('categorymaster')->where('user_id', '=', $user->id)->get();
return view('jobprovider.allpostedjob', compact('user','jobposteddetails'));
}
jobpost.php model is:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class jobpost extends Model
{
function categorymaster()
{
return $this->belongsTo(categorymaster::class, 'cat_id');
}
}
It is working proper.
But I also need to show LastSubCategoryName grouped by the tables jobposts, lastsubcategoryselectedbycompanies and lastsubcategorymasters.
function lastsubcategory()
{
return $this->belongsTo(lastsubcategoryselectedbycompanies::class);
}
It is not working. How can I fetch my result?
I am not very skilled at applying complex queries with eloquent, I prefer to use DB query builder with the join method
https://laravel.com/docs/5.8/queries

Laravel Eloquent multiple relationship in a football game schedule

I am new to Laravel and Eloquent and i am trying to create a football game plan.
For now i have 4 tables (with some example entries):
teams (all teams)
+---------+-----------+
| team_id | team_name |
+---------+-----------+
| 1 | Arsenal |
| 2 | Chelsea |
+---------+-----------+
competition (all competitions)
+----------------+------------------+
| competition_id | competition_name |
+----------------+------------------+
| 1 | Premier League |
+----------------+------------------+
schedule (schedule to the competitions)
+----+----------------+----------+--------------+--------------+-----------+-----------+
| id | competition_id | matchday | home_team_id | away_team_id | home_goal | away_goal |
+----+----------------+----------+--------------+--------------+-----------+-----------+
| 1 | 1 | 1 | 1 | 2 | 3 | 2 |
| 2 | 1 | 2 | 2 | 1 | 0 | 3 |
+----+----------------+----------+--------------+--------------+-----------+-----------+
schedule_teams (matches the schedule teamid with the teams id over the competition_id and the schedule_team_id)
+----+------------------+----------------+----------+
| id | schedule_team_id | competition_id | teams_id |
+----+------------------+----------------+----------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | 2 |
+----+------------------+----------------+----------+
And here are my current classes:
Schedule.php
public function competition()
{
return $this->belongsTo(Competition::class, 'competition_id', 'competition_id');
}
Competition.php
public function schedule()
{
return $this->hasMany(Schedule::class, 'competition_id', 'competition_id');
}
With
$id = \request('competition_id');
$schedule = Schedule::where('competition_id', $id)->with('competition')->get();
i get the schedule with the home and away id's from schedule.
The question now is, how can i get the entries from the teams table over the schedule_teams table to a specifiy home and away id, also for example home_team_id = 1:
home_team_id (=1) -> schedule_team_id (=1) and competition_id (=1) -> teams (Arsenal)
I want the data from schedule and the associated teams in a collection to output in a blade.
can anyone help or give me improvement tips for a football database?
You should make use of the hasManyThrough relationship.
If you create say Schedule\Team, and then have that like the following.
public function schedule() {
$this->belongsTo(Schedule::class, 'schedule_id');
}
public function team() {
$this->belongsTo(Team::class, 'team_id');
}
Now in your Schedule class, you can have the following.
public function teams() {
$this->hasManyThrough(Team::class, Schedule\Team::class, 'schedule_id');
}
It should also be noted, that you don't need competition_id in your schedule team. Since a team belongs to a schedule, which belongs to competition, you can get it like that.
If you also want your Team to know about its schedules, you can add this to Team.
public function schedules() {
return $this->hasManyThrough(Schedule::class, Schedule\Team::class);
}
You Schedule\Team class becomes essentially, a glorified representation of a pivot table, but having it as a model, allows you to expand upon it in the future. It also helps keep everything neat.
Hope that makes sense.

Laravel Relationship result can not fetch for same foreign key

My table looks like below,
donation_requests
---------------------------------------------------------------
| id name donation_type_id donation_request_type_id
|--------------------------------------------------------------
| 1 xyz 1 3
| 2 pqr 3 2
| 3 abc 3 1
| 4 klm 4 1
donation_types
------------------------
| id name
-----------------------
| 1 jakat
| 2 sadka
| 3 lillah
| 4 fitra
donation_request_types
------------------------
| id name
-----------------------
| 1 widow
| 2 masjid
| 3 madresha
i want donation_request data with donation_types name and donation_request_types name
In my DonationRequest model code is as below
public function donation_types()
{
return $this->belongsTo('App\DonationType','id','donation_type_id');
}
public function donation_request_types()
{
return $this->belongsTo('App\DonationRequestType','id','donation_request_type_id');
}
but in first two row i get proper result but for third row has same donation_type_id as 3 so it give blank and same as in donation_request_type_id
public function donation_types()
{
return $this->belongsTo('App\DonationType');
}
public function donation_request_types()
{
return $this->belongsTo('App\DonationRequestType');
}
As per my above question relationship is working fine, and it has issue in laravel tinker. While testing in postman i found perfect result, but in tinker there will be some issue. Update tinker but still have same problem.

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