How can I change the default prompt to save to the database in Laravel?

All kind time of day!
The real question is this:
There is a table in DB which stores the values of the network adapters, therefore the table is not created the field "id" as key field was assigned to the field "mac" because the physical addresses are unique to itself.
To save the data on the network adapter after editing, I wrote the following function:
public function saveNic (Request $request)
{
 $data = $request->all();
 $host = $data['host'];
 unset ($data['host']);
 $tmp= Networkcard::select(['mac', 'name', 'network', 'wol'])->where('mac',$data['mac'])->get();
$nic=$tmp[0];
$nic->fill($data);
$nic->save();
 return redirect("/host/".$host);
 }

As you can see in the code, the problem with the sample from the base I walked in the following way (if it can be improved, please tell me how).
But when you save and call save() method Laravel generates a query in which the key field considers the id field. Therefore, it is reasonable tells me error.
cb71f757aa1d4377a6c0c6f33270533b.PNG
Please tell me how can I change the query tools Laravel to save data, or maybe to add some parameters to the method save(), what would the query not to look for the mac line on the field, not the id field?
June 26th 19 at 14:25
3 answers
June 26th 19 at 14:27
Solution
In the model of Networkcard to add:
protected $primaryKey = 'mac';
June 26th 19 at 14:29
Solution
https://laravel.com/docs/5.4/eloquent

Primary Keys

Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.

June 26th 19 at 14:31
Model:
protected $primaryKey = 'mac';
public $incrementing = false;


And how can you gonococal on laravel?

Something like this can do:
Networkcard::where('mac', $request->get('mac'))->update($request->all());
return redirect()->back(); // Think this will do

In the model to determine the $fillable field (for fast updates)
Well, that something like this is possible, I have written any suggestions on how to improve, I'm happy to read)
With Laravel and only began Dating earlier written in pure php where he was himself the Creator, there is still no ... it is not clear how it works, manuals are all different it is necessary to write so as you can see. - Haven_Wilderman commented on June 26th 19 at 14:34
Still it is possible so:
return redirect()->route('host', ['id' => $request->id]);

And in the Declaration to stick NetworkCardCreateRequestto inherit from BaseRequest your where is code return fields. - Gerda57 commented on June 26th 19 at 14:37
: Thank you very much! Do not really understand about the Declaration, but try to understand) - Haven_Wilderman commented on June 26th 19 at 14:40
Thank you, I tried to implement your suggestion, but due to the fact that the update method is passed the entire query, you receive the error because of a missing _token field in the database.
According to the rules of the framework, the tokens should be written to the database? - Haven_Wilderman commented on June 26th 19 at 14:43
What is your version? By default _token is not included in $request->all(), if I'm not mistaken. And about the Declaration I am about this:
public function saveNetworkCard(NetworkCardCreateRequest $request)


Before that php artisan make:request NetworkCardCreateRequest
Then it modificeres as it should. I, for example, there are BaseRequest, which has a method bar():

public function needed() {
 return $this->only($this->getFields());
}

 protected function getFields() {
 return array_keys($this->container->call([$this, 'rules']));
 }


Thus we fill only the validation code in the right places, instead of $request->all() use $request->needed()

This approach is significantly more convenient than $fillable as $fillable - General for the whole project, and needed() - specifically under request. - Gerda57 commented on June 26th 19 at 14:46
: well, when you use the fill method and then save, when you debug the watched content token but database is not affected, and when using update($request->all()) I understand he is trying to write directly, since the error is quite clear it says that not found the column _token. Version 5.4 put the latest, mastering)
For example thanks, it looks complicated, but I think you will understand ) - Haven_Wilderman commented on June 26th 19 at 14:49

Find more questions by tags PHPLaravel