لارافيل, Model / 2024-10-18

تغيير حقل البحث الإفتراضي في eloquent find, findOrFail

تغيير حقل البحث الإفتراضي في eloquent find, findOrFail

2024-10-18 وقت القراءه : أقل من دقيقة

 

بالوضع الإفتراضي يتم البحث في لارافيل بإستخدام find, findOrFail في حقل id، لكن اذا كنا نريد تغيير هذا الحقل يجب إن يتم تحديد ذلك في الـ model

model

class Product extends Model{
    protected $primaryKey = 'title';
    //tell Laravel to use title as primary key
}

eloquent

public function search(Request $request){
    $user = Product::findOrFail($request->title);
    return $user;
}
إضافة تعليق
Loading...