Reputation: 409
I know if i want to update database on laravel i could just use
$user = User::find($request->id);
$user->name = $request->get(‘name’);
$user->save();
But what if my table doesn’t have id column in it? It primary column name isn’t id? I’ve tried this method and it didn’t work
$user = User::where(‘userID’, $request->id);
$user->name = $request->get(‘name’);
$user->save();
This are my table content
accountReceiveAble
This are my Controller
public function store(Request $request)
{
$ra = accountRecieveAble::where('sales',$request->sales)->firstOrFail();
$ra->invoice = $request->get('invoice');
$ra->taxInvoice = $request->get('taxInvoice');
$ra->dpp = $request->get('dpp');
$ra->ppn = $request->get('ppn');
$ra->paymentDate = $request->get('date');
$ra->paymentMethod = $request->get('method');
$ra->save();
return redirect(action('AccountRecieveAbleController@index'));
}
this are my model
class accountRecieveAble extends Model
{
protected $table = "account_recieve_ables";
public $primaryKey = 'sales';
public $timestamps =false;
public function sale(){
return $this->belongsTo('App\sales','sales');
}
}
Upvotes: 0
Views: 84
Reputation: 4020
If you want to change the primary key
to something else other than id
, you need to override it in your Model.
class YourModel
{
/**
* Your primary key column name.
*
* @var integer
*/
$primaryKey = 'sales';
}
Also, if it is non integer value, then again, you will have to add the following to your model..
class YourModel
{
/**
* Your primary key column name.
*
* @var integer
*/
protected $primaryKey = 'sales';
/**
* If your primaryKey is not integer or non numeric value
*
* @var string
*/
protected $keyType = 'string';
/**
* And if it is not going to increment the value
*
* @var boolean
*/
public $incrementing = false;
}
From the documentation - Scroll down to Primary Keys
section:
Eloquent will also assume that each table has a primary key column named id. You may define a protected
$primaryKey
property to override this convention.
Upvotes: 1
Reputation: 4236
In this query $user = User::where(‘userID’, $request->id);
append first() like this $user = User::where(‘userID’, $request->id)->first();
Upvotes: 1