Mysql Relationships Table

Asked

Viewed 49 times

2

I have a view called 'clients.Blade' where the administrator will choose which clients a particular developer can see, but I don’t know how to save these changes to the database.

Model

public function Clientes ()
{
    return $this->hasMany("Clients"); //conectando as tabelas 'users' e 'clients' do banco de dados
}

Controller

public function updateClient()
    {
        var_dump(Input::get("clientes"));
        $user = \App\User::find(Input::get("id"));
    }

Clients.

<form method="POST" action="/user/update/client">

    <input type="hidden" name="id" value="{{$id}}" />

    <h1>BEM VINDO ADMINISTRADOR!</h1>

    <div><input type="button" value="Início" id="inicio" name="Início" onclick="window.location.href='/inicioadm';"></div> <br>

     @foreach ($clients as $client)

    <table style="width:100%">
    <tr>
    <th width="30%"><p>Nome: {{$client->Nome}} <input name="clientes[]" type="checkbox" value="{{$client->ID}}"> </p></th>       
    </tr>
    </table>

    @endforeach

    <br><div><input type="submit" value="Salvar" id="salvar" name="Salvar" onclick="window.location.href='/desenvolvedores"/div>


</form>

  • 1

    I think you need to explain more Victoria, what do you have in the database to track that? What will differentiate in the database clients a particular developer can see from others?

  • I have a table of relationships with id, id_user and id_client.

  • I want that by clicking 'Select Customers', the Administrator will be able to choose which clients the Developer can see and save in the table.

  • you have in the model of desencolvedores the relationship hasmany with the right customers?

  • No... I put 'belongToMany'... it’s wrong???

  • public Function clients () { Return $this->belongsToMany('App client'); }

  • I tried to do this code, but it gives error: public Function clients($id) { $clients = App client::all(); $user = App User::find($id); $UC = App relationModel::Where('user_id', $user->id)->get(); $userClients = array(); foreach($UC as $item) { $userClients[] = $item->client_id; } Return View('/clients')->with(array('clients'; }

  • Ha ok I realized, because a client can be edited by one or more developers, and a developer can edit one or more clients, right?

  • If everything is fine with the relationship you can use attach, https://laravel.com/docs/5.4/eloquent-relationships#updating-Many-to-Many-relationships... you are using Laravel all right?

  • That’s... that’s right! Hence I have a view that has 4 checkbox, then 'check' each client, will be what the developer can 'see/edit'.

  • SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.relation_models' doesn’t exist (SQL: select * from relation_models Where user_id = 1)

  • don’t know needed more data /: . Do you have any place with the code? This link can help, https://laravel.com/docs/5.4/eloquent-relationships#Many-to-Many

  • I got it... thanks man!

  • Good Victory (: I just pushed. This can also help http://answall.com/a/193258/5749

Show 9 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.