I am doing a crud, and when I get to the modification part I have the following problem, previously I had already done other projects and in this part everything worked fine, the problem comes out because the id's of the tables were always simple, but in this case the id's are different example
producttype_id
When using the Find() method, it focuses only on the primary key (id) and this method does not work for me, so I looked for another one because I also use it for the delete method and it is the following
$tipo_producto->tipo_producto::whereRaw('id_tipoproducto = id_tipoproducto', [$id_tipoproducto])->get();
The problem is that when submitting the form, the following error returns:
"Class name must be a valid object or a string"
I show you the code that I am using to modify my registry.
public function update(Request $request, tipo_producto $tipo_producto)
{
$id_tipoproducto = $request->id_tipoproducto;
$tipo_producto->tipo_producto::whereRaw('id_tipoproducto = id_tipoproducto', [$id_tipoproducto])->get();
//$tipo_producto= tipo_producto::find($id_tipoproduto);
$tipo_producto->tipo = $request ['tipo'];
$tipo_producto->update();
return redirect('tipop');
}
I attach the structure of my model and my table
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tipo_producto extends Model
{
protected $table="tipo_producto";
protected $fillable=['id_tipoproducto','tipo'];
}
and here is the error that I get if I try to use the find() method
Let's take a look at some observations:
You're not declaring the property needed to tell Eloquent that your primary key has a different name than expected, so it goes like this :
In addition to the above, you are filtering by the primary key, therefore it is unnecessary for me (not that it is wrong) inject the model and an object ( because by doing this I consider that it would be more useful, for example, if you were working with route model binding )
So our update could look like this:
What we do is :
tipo_producto
tipo
we assign the property the valuetipo
that comes to us by the request