logo
down
shadow

Pivot table for single table in Laravel


Pivot table for single table in Laravel

By : diegoarmando
Date : October 18 2020, 06:10 PM
seems to work fine I'm trying to add a multi-level categories in my website. where a category can have multiple child and also have multiple parent. It's look like many to many relationship in Laravel and I have to use a pivot table. , You can have methods like below on Category model :
code :
//This will get you the list of categories
public function categories()
{
    return ($this->hasManyThrough('App\Models\Category', 'App\Models\CategoryRelation', 'child_id', 'id', 'id', 'parent_id'));
}

//This will get you the list of children
public function childs()
{
    return ($this->hasManyThrough('App\Models\Category', 'App\Models\CategoryRelation', 'parent_id', 'id', 'id', 'child_id'));
}
class CategoryRelation extends Model
{
    protected $table = 'categories_relationship';
}


Share : facebook icon twitter icon
Retrieving a single colum from a pivot table - Laravel 5

Retrieving a single colum from a pivot table - Laravel 5


By : Eclinomillion
Date : March 29 2020, 07:55 AM
Any of those help I think what will help you achieve this behavior is the lists() method. Try something like
$user_genres = auth()->user()->genres()->lists('name','id');
Laravel's pivot table + Pivot table in general

Laravel's pivot table + Pivot table in general


By : Bilal Khan
Date : March 29 2020, 07:55 AM
this one helps. When learning, focus only the pivot tables concept in Laravel (or eloquent). When I was learning I did not care about the general meaning of pivot table. I focused only on facts in the documentation (https://laravel.com/docs/5.5/eloquent-relationships#many-to-many)
many-to-many relationships require an additional table.And we can insert other useful data to this table as well. And can be used as a model in the system.
code :
//get user by id
$user = App\User::find(1);

//get roles of this user
foreach ($user->roles as $role) {

  //pivot attribute returns a model which represent user_role table
  echo $role->pivot->created_at;

}
class Student extends Model
{
    /**
     * The users that belong to the role.
     */
    public function Rooms()
    {
        return $this->belongsToMany('App\Room')->using('App\Room_Student');
    }
}

class Room extends Model
{
    /**
     * The users that belong to the role.
     */
    public function Students()
    {
        return $this->belongsToMany('App\Student')->using('App\Room_Student');
    }
}

class Room_Student extends Pivot
{
    //
}
It is possible to relate a pivot table to another pivot table in laravel 5.5?

It is possible to relate a pivot table to another pivot table in laravel 5.5?


By : Ehtisham Khalid
Date : March 29 2020, 07:55 AM
Hope that helps There can be no such relationship. If you have a pivot table, it binds two tables that are not pivot.
What is a pivot table? This is a table that has no increments. And it binds to the increments of tables for which it is a pivot. If the table has increments then it does not automatically pivot.
code :
plan_user_category // what ???

 plan_user_id // BelongsTo or BelongsToMany -> plan_user
 category_id // BelongsTo or BelongsToMany -> categories
class Plans
public function users() 
{
  return $this->belongsToMany('App\User', 'users_plans', 'user_id', 'plan_id');
}

class User
public function categories() 
{
  return $this->belongsToMany('App\Category', 'users_catogories', 'category_id', 'user_id');
}

foreach($plans->users() as $user) {
  $categories[] = $user->categories();
}
Laravel remove single data from a many to many pivot table

Laravel remove single data from a many to many pivot table


By : Dmitrijs Solovjovs
Date : March 29 2020, 07:55 AM
it fixes the issue i'm trying to delete single item in my pivot table, and i don't know how to make it work? is it possible? if my method is wrong please correct me . i will show below my own approach. , You need term id and level id in `detach_term
Route
code :
Route::post('term/{id}/deleteLevel', 'ListController@detach_term');
public function detach_term(Request $request, $id)
{
   $term = Term::with('level')->find($id);

   if($term){
      $postData = $request->all();
      $term->level()->detach($postData['level_id']);
      return redirect('term/get/' . $term->id);
   }else{
      abort(400, 'Invalid term');
   }

}
<form action="/term/{{$term->id}}/deleteLevel" method="POST" >
    {{ csrf_field() }}
    @foreach($term->level as $levels>
       <input type="checkbox" name="level_id[]" value="{{ $levels->id }}">
            {{ $levels->levelname }}
    @endforeach
    <button type="submit">Delete</button>
</form>
How to setup Laravel Many to Many-relationship with intermediate table which is not only a pivot table, but also represe

How to setup Laravel Many to Many-relationship with intermediate table which is not only a pivot table, but also represe


By : Simon Bourguignon
Date : March 29 2020, 07:55 AM
Any of those help look at docs
Defining Custom Intermediate Table Models
Related Posts Related Posts :
  • Best way to have unique key over 500M varchar(255) records in mysql/innodb?
  • How to make a remote connection to a MySQL Database Server?
  • MySQL: Indexing large amount of columns (150M rows) with varied queries
  • Is it possible through SQL injection to launch an UPDATE/DELETE statement from an INSERT/SELECT statement in MySQL?
  • MySQL Select names with last names starting with certain letter
  • How to remove more than one space between 2 or more words?
  • MYSQL: Get Previous Row but on base of Non primary Column
  • Access denied error while connecting to MySQL in App in Azure App Service
  • My POST request is working with Node.js but not with API
  • I am creating a database for a community to store details of all the members. What would be the best way to create such
  • Select between range of values in 2 tables
  • What's the Best way to select the min value from several columns and then calculate their sum?
  • MYSQL: AND statement causing expected results to not display
  • mysql - Select where image_url=0 but return all rows?
  • Create function that returns a "SELECT" statement result
  • Subquery returns more rows
  • Unrecognized Keyword and Statement Type (FROM)
  • Optimise mysql query with group by
  • FOREIGN KEY ON DELETE SET NULL
  • Avoid duplicate data in mySQL table
  • Why is SQL Count(*) returning 1 from an empty table?
  • Symfony 4 - How to dynamically add field in an entity?
  • MySQL inconsistently altering name of indexes associated with foreign keys on InnoDB tables
  • SQL: Alternative to COALESCE() function that groups all values into one column
  • SUM from different column and from different table and show result in one row of each year
  • MySQL query too much slow
  • How to count quantity of duplicate data?
  • Find the number of unique users who have visited at least two different countries per site
  • Restarting Mysql Database in Cpanel on a shared Server
  • Can we create sql DB server backup on different database(free database)?
  • Convert time into range in SQL
  • MariaDB 10.3 implicit cast of string parameter to integer column fails
  • UNION ALL and SELECT
  • How to access redmine log folder inside a docker after a docker-compose?
  • mysql json where clause
  • What is causing the error "Column does not match the value count at row 1"?
  • MySQL Return JSON array index based on property value
  • Finding users with at least one of every item
  • Multi-event tournament standings
  • MySQL delete duplicate records but keep more than one (ex. 5)
  • Display the users that have not yet created projects this month in specific city (count=0)
  • Have a Syntax problem related to an event which I cannot find
  • How can I get all the devices from database table whose RAM is in between 1 MB to 2 GB?
  • Slow query for join tables
  • RDS Upgrade Fails despite prepatchcompatibility showing no errors
  • MySQL query really slow as loading benchmark
  • How to improve query speed in mysql query
  • Count values of MySQL
  • How to copy values from one table to other table with some additional data
  • Why my WHERE and COUNT clause do not work?
  • How to update SQL on JOIN
  • MySQL: Use select in update query
  • Selecting rows that are within 2 hours from current time
  • SQL Query Including Joins
  • How does one update MySQL database periodically separate from website
  • How data backup is handled in production via Docker
  • Sphinxsearch: 1064 can not use HAVING with attribute not related to GROUP BY
  • MySQL root password reset -bash: syntax error near unexpected token `('
  • Laravel where query for getting record if the difference of two fields is not 0
  • Group or eliminate duplicates fields in columns
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk