Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting two tables in mysql

Tags:

php

mysql

laravel

I have two tables in my database - vnames and vtypes
vtypes have a name field and a id field, vnames have a id field, name field and a vtypes_id field which is a foreign key connected field. It is connected with the id field in vtypes.

I have Vname and Vtype models -

Vname

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Vname extends Model
{
    public function vtype() {
        return $this->belongsTo('App\Vtype');
    }
}

Vtype

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Vtype extends Model
{
    public function vname() {
        return $this->hasMany('App\Vtype');
    }
}

When i delete any column in vtype table i want to delete all the vname columns associated with it. I found a solution like this -

public function vtypeDestroy($id) {
    $vtype = Vtype::find($id);
    $vtype->vname()->detach();
    $vtype->delete();

    Session::flash('success', 'The vtype was successfully deleted');
    return redirect('/vtypes');
}

but when i run this function i get an error like this - Call to undefined method Illuminate\Database\Query\Builder::detach()

How can i fix it?

And when i want to get the name of the vtype from vname i am not able to do it. I tried like this

@foreach ($vnames as $vname)
    {{ $vname->vtype()->name }}
@endforeach

in a view

But i got an error like this -- Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

Please guide me how to fix this two problems which i face now.

like image 679
Conor Avatar asked Dec 21 '16 04:12

Conor


2 Answers

Your relation should be as:

Vname Model

class Vname extends Model
{
  public function vtype() {
        return $this->belongsTo('App\Vtype', 'vtypes_id', 'id');
  }
}

Vtype

class Vtype extends Model
{
    public function vname() {
        return $this->hasMany('App\Vname', 'vtypes_id', 'id');
    }
}

Then you can use delete method to delete the relations as:

$vtype = Vtype::find($id);
$vtype->vname()->delete();
$vtype->delete();

And in your view it should be as:

@foreach ($vnames as $vname)
    {{ $vname->vtype->name }}
@endforeach

Docs

like image 100
Amit Gupta Avatar answered Oct 13 '22 22:10

Amit Gupta


Please use...

$vtype->vname()->delete();

instead of $vtype->vname()->detach();... Problem solved! For the inverse of it, vname->vtype() you can you dissociate() method... which will set vtype_id to null in vname table. More explanation here

Detach is used for belongsToMany() - belongsToMany() relation... yours is hasMany() - belongsTo().

Also, instead of doing {{ $vname->vtype()->name }}

please do

@foreach ($vnames as $vname)
    {{ $vname->vtype->name }}
@endforeach

The reason for this is... when you put the brackets in front of the relation name, it calls a query builder... But here, what you need is the model... So $vname->vtype will give you Vtype Model while $vname->vtype() will give you query builder.

like image 39
prateekkathal Avatar answered Oct 13 '22 21:10

prateekkathal