PaulThomasWeb
PaulThomasWeb

Reputation: 57

Delete entrys in Database with foreignkey and pivot relations laravel 8

Hey im searching for a method to delete adn entry which is connected to some other with ManytoMany and belongstoMany Relations, my question is how can i get an query that it finds the relations and checks it, if there are none it should be deleted and if there are some it should not delete it.

this is my Controller:


    public function index()
    {
        $tracks = Track::all();
        $seasons = Season::all();

        return view('index.track', compact('tracks', 'seasons'));
    }


    public function create(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
    {
        $seasons = Season::all();
        $topics = Topic::all();
        $speakers = Speaker::all();
        return view('create.track', compact('topics', 'seasons', 'speakers'));
    }

    public function store(TrackStore $request): \Illuminate\Http\RedirectResponse
    {
        $hashedName = Hash::make($request->file('track_data')->getClientOriginalName()) . "." . $request->file('track_data')->getClientOriginalExtension();
        $request->track_data->storeAs('public/tracks', $hashedName);
        $track = new Track();
        $track->title = $request->track_title;
        $track->description = $request->track_description;
        $track->data = $hashedName;
        $track->season_id = $request->season_id;
        $track->save();
        $track->speakers()->attach($request->input('speakers'));
        $track->topics()->attach($request->input('topics'));
        if($request->input('moderators')) {
            $data = [];
            foreach ($request->input('moderators') as $moderatorId) {
                $data[$moderatorId] = ['is_moderator' => 1];
            };
            $track->speakers()->attach($data);

            return redirect()->route('admin.trackShow');
        } else {
            return redirect()->route('admin.trackShow');
        }
    }

    public function delete(Track $id): \Illuminate\Http\RedirectResponse
    {
        $id->delete();

        return redirect()->route('admin.trackShow');
    }

    public function edit(Track $id)
    {
        return view('edit.track');
    }

This is my Model:



class Track extends Model
{
    use HasFactory;

    protected $table = 'tracks';

    protected $primaryKey = 'id';

    protected $fillable = [
            'title',
            'description',
            'data',
            'season_id',
        ];

    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }

    public function speakers(): BelongsToMany
    {
        return $this->belongsToMany(Speaker::class, 'speakers_tracks', 'track_id', 'speaker_id')->withPivot('is_moderator');
    }

    public function topics(): BelongsToMany
    {
        return $this->belongsToMany(Topic::class, 'topics_tracks', 'track_id', 'topic_id');
    }

}

This is my migration:


        Schema::create('tracks', function (Blueprint $table) {
            $table->id('id');
            $table->string('title');
            $table->string('description');
            $table->string('data');
            $table->integer('season_id')->unsigned();
            $table->timestamps();
            $table->softDeletes();
        });

As you see the Tracks are connected to many other stuff they are connected via relations. thanks in advance!

Upvotes: 0

Views: 45

Answers (1)

Suresh Hemal
Suresh Hemal

Reputation: 40

It will be easy with count()

if ($supplier->items()->count() == 0) $supplier->delete();

It is not your model. But you will get the idea.

Upvotes: 1

Related Questions