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>
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?
– Miguel
I have a table of relationships with id, id_user and id_client.
– Vitória
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.
– Vitória
you have in the model of desencolvedores the relationship hasmany with the right customers?
– Miguel
No... I put 'belongToMany'... it’s wrong???
– Vitória
public Function clients () { Return $this->belongsToMany('App client'); }
– Vitória
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'; }
– Vitória
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?
– Miguel
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?
– Miguel
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'.
– Vitória
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.relation_models' doesn’t exist (SQL: select * from
relation_models
Whereuser_id
= 1)– Vitória
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
– Miguel
I got it... thanks man!
– Vitória
Good Victory (: I just pushed. This can also help http://answall.com/a/193258/5749
– Miguel