🔥یلدا با راکت با (( ۷۰ درصد )) تخفیف! یلدا طولانی است، اما این تخفیف نه.
مشاهده دورههاسلام دوستان من تازه api نویسی وکار بالاراول یاد گرفتم یه کمکی ازتون میخوام
سه تا جدول دارم
۱- users
۲-profiles
۳-stores
داخل profiles ستون user_id گذاشتم و با hasOne ارتباطش دادم به users
مشکلی هم نداشت ولی الان میخوام بگم هر profile هم یک store داره
باید چکار کنم.
مدل ُStore
public function profiles()
{
return $this->belongsTo(Profile::class);
}
مدل Profile
public function stores()
{
return $this->hasOne(Store::class);
}
مایگریشن profiles
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->string('fullName')->nullable();
$table->integer('province')->nullable();
$table->integer('city')->nullable();
$table->string('***')->nullable();
$table->string('image')->nullable();
$table->date('birthday')->nullable();
$table->bigInteger('code')->unique();
$table->boolean('enable')->default(true);
$table->integer('wallet')->default(۰);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
مایگریشن stores
Schema::create('stores', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('profile_id')->unsigned();
$table->integer('province');
$table->integer('city');
$table->text('address');
$table->string('name');
$table->string('license_image');
$table->string('logo_image');
$table->string('count_person');
$table->string('type');
$table->string('phone');
$table->string('description');
$table->float('latitude');
$table->float('longitude');
$table->float('altitude');
$table->float('accuracy');
$table->boolean('enable')->default(true);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('profile_id')->references('id')->on('profiles')->onDelete('cascade');
});
وقتی تو StoreController اینو میزنم
auth()->user()->profiles()->stores()->create([
این ارور رو میده
"message": "Call to undefined method Illuminate\\Database\\Eloquent\\Relations\\HasOne::stores()",
"exception": "BadMethodCallException",
"file": "E:\\Project_backend\\barbers\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php",
"line": ۵۰,
"trace": [
{
"file": "E:\\Project_backend\\barbers\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\ForwardsCalls.php",
"line": ۳۶,
"function": "throwBadMethodCallException",
"class": "Illuminate\\Database\\Eloquent\\Relations\\Relation",
"type": "::"
},
{
"file": "E:\\Project_backend\\barbers\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Relations\\Relation.php",
"line": ۳۸۵,
"function": "forwardCallTo",
"class": "Illuminate\\Database\\Eloquent\\Relations\\Relation",
"type": "->"
},
میتونین از has-one-through استفاده کنین به صورت
auth()->user()->stores()->create([])
https://laravel.com/docs/6.x/eloquent-relationships#has-one-through
@tefo.ha27
میشه خودتون بگید دقیق چکار کنم چون hasOneThrough به جای hasOne گذاشتم ولی زیرش خط قرمز میکشه چطوری باید ازش استفاده کنم؟
به این صورت تست کنید :
$profile=auth()->user()->profiles()
سپس
$store=new Store()
$store=$store->name="foo"
و...
$profile->stores->save($store)
@rezajamalzadeh900
نشد همون ارور رو میده
@ali.bayat ببخشید اقای بیات شما نمیدونید مشکل چیه که این ارور رو میده
درود
"message": "Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::stores()",
"exception": "BadMethodCallException",
به نظر میرسه در تعریف روابط جایی دچار اشتباه شده باشید
@hesammousavi
اقای موسوی به نظرتون کجا اشتباه کردم؟
تو جدول stores دوتا ستون کلید خارجی دارم یکی user_id یکی profile_id به نظرتون چکار کنم خودشون اتومات این ایدیها از جدول profile گرفته بشه ؟
اینطوری که مینویسم ارور میده
([])auth()->user()->profiles()->stores()->create
ولی اینطوری ارور نمیده ولی میگه profile_id از کجا پر کنم خالیه
([])auth()->user()->stores()->create
من مشکلمو اینطوری حل کردم ممنون میشم یکی بگه مشکلی پیش نمیاد بعدا؟
auth()->user()->stores()->create([
'profile_id' => auth()->user()->profiles()->first()->id,
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟