راکت
حل شده

پیدا نکردن آبجکت

6 پاسخ

سلام وقت بخیر. از طریق فایل model یک ارتباط ایجاد کردم کد کدهاش رو قرار میدم ولی متاسفانه خطای پیدا نکردن آبجکت رو بهم میده. ممنون میشم راهنماییم کنین
کد های فایلmodel مربوط به article

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable=['name','slug','description','status','user_id'];

    protected $attributes =[
        'hit' => 1,
    ];

    public function categories(){
        return $this->belongsToMany(Category::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

قطعه کدهای مربوط به view که خطا میده

 @foreach($articles as $article)

                        @switch($article->status)
                            @case(0)
                                @php
                                $url = route('admin.articles.status',$article->id);
                                $status= '<a href="https://roocket.ir/'.$url.'" class="badge badge-warning">disable </a>' @endphp
                            @break

                            @case(1)
                                @php
                                $url = route('admin.articles.status',$article->id);
                                 $status= '<a href="https://roocket.ir/'.$url.'" class="badge badge-success">active</a>' @endphp
                            @break

                            @defualt

                        @endswitch

                        <tr>
                          <td>{{$article->name}}</td>
                          <td>{{$article->slug}}</td>
                          <td>{{$article->user->name}}</td>
                          <td>
                              @foreach($article->categories()->pluck('name') as $category)
                                    <span class="badge badge-success">{{$category}}</span>
                              @endforeach
                          </td>
                          <td>{{$article->hit}}</td>
                          <td>{!!$status!!}</td>
                          <td>
                              <a href="https://roocket.ir/{{route('admin.articles.edit',$article->id)}}"><label class="badge badge-success">edit</label></a>
                              <a href="https://roocket.ir/{{route('admin.articles.destroy',$article->id)}}"
                                  onclick="return confirm('are you delete the item?');"><label class="badge badge-danger">delete</label></a>
                          </td>

                        </tr>
                      @endforeach

اینم عکس خطایی که میده
توضیح تصویر رو وارد کنید

mehdi
mehdi
@mehdi.abl
ثبت پرسش جدید

پاسخ‌ها

6
فرشید مرادی
فرشید مرادی
@eniack

تخصص: noob

بهترین پاسخ

خط ۲۱ مایگریشن article رو پاک کنید و اینو بنویسید

$table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
فرشید مرادی
فرشید مرادی
@eniack

تخصص: noob

سلام وقت بخیر ، ابتدا یک خروجی از تموم داده هایی که دارید بگیرید ،
return $articles
که مشخص بشه اصلا چی داریم میفرستیم واسه ویو...

mehdi
mehdi
@mehdi.abl

تخصص: عاشق برنامه نویسی

@eniack
سلام از حلقه dd گرفتم و تمام آیتم هارو نشون میده ولی به خطی که ازش خواستم بره به جدول user و name رو بگیره همون خطارو میده
توضیح تصویر رو وارد کنید
توضیح تصویر رو وارد کنید

شاید پاسخ شما همین‌جا باشد

فرشید مرادی
فرشید مرادی
@eniack

تخصص: noob

اکر ممکنه کدهای قسمت migration ها روهم بزارید

mehdi
mehdi
@mehdi.abl

تخصص: عاشق برنامه نویسی

@eniack
کد های مربوط به جدول article

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('Slug')->unique();
            $table->text('description');         
            $table->integer('user_id');             
            $table->integer('hit');              
            $table->tinyInteger('status');         
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

کدهای مربوط به جدول user

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->tinyInteger('role')->default('2');
            $table->tinyInteger('status')->default('1');
            $table->string('phone');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
mehdi
mehdi
@mehdi.abl

تخصص: عاشق برنامه نویسی

@eniack
ممنون درست شد 🙏🏻🌹

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام