Home > Software engineering >  How can I fix this error related to to controller?
How can I fix this error related to to controller?

Time:11-09

I have a problem, thank you for your help. I want this to be done in the Store method when a person submits a question But it gives me an error that says: thread_id required ???!

  Subscribe::query()->create([
        'thread_id'=>$thread->id,
        'user_id' => auth()->user()->id
    ]);

This is the code for my Store method, which includes the subscribe model:

 public function store(Request $request,Thread $thread)
{
    $request->validate([
        'title' => ['required', 'min:3'],
        'description' => ['required'],
        'channel_id' => ['required'],
        'thread_id' => ['required']
    ]);

    Thread::create([
        'title' => $request->title,
        'description' => $request->description,
        'user_id' => auth()->user()->id,
        'channel_id' => $request->channel_id,
    ]);

    Subscribe::query()->create([
        'thread_id' => $thread->id,
        'user_id' => auth()->user()->id
    ]);
    return redirect('/');
}

This is also the code related to View

<form action="{{route('threads.store')}}" method="post">
@csrf
<input type="hidden" name="thread_id" value="{{$thread->id}}">

<div class=" container-fluid w-75 border border-secondary rounded shadow">
    <div style="font-weight: bolder; font-size: large;" class=" my-3">فرم ارسال پرسش</div>
    <div class="input-group mb-3">
        <div class="input-group-prepend">

        </div>
        <input type="text" name="title" class="form-control" placeholder="موضوع" aria-label="Username" value="{{old('title')}}"
               aria-describedby="basic-addon1">
    </div>
    
    <div hljs-string">">
        <textarea name="description" hljs-string">" aria-label="With textarea" placeholder="متن پرسش"
                  style="height: 200px;">{{old('description')}}</textarea>
    </div>


    <div hljs-number">3 " style="direction: ltr;">
        <select name="channel_id" hljs-string">" id="channel_id" >
            <option selected disabled value="{{old('channel_id')}}">انتخاب عنوان</option>
            @foreach(\App\Models\Channel::all() as $channel)
                <option value="{{$channel->id}}">{{$channel->name}}</option>
            @endforeach
        </select>

    </div>

    <div hljs-number">3" style="width: 100px;">
        <button hljs-string">" type="submit">ارسال</button>
    </div>


</div>

the problem about this code on view

<input type="hidden" name="thread_id" value="{{$thread->id}}">

also i sent the Thread model on view

 public function create(Thread $thread)
{
    return view('answer-question.thread.thread-create',compact('thread'));
}

this is subcribe database enter image description here

CodePudding user response:

public function store(Request $request)
{
    $request->validate([
        'title' => ['required', 'min:3'],
        'description' => ['required'],
        'channel_id' => ['required'],
        'thread_id' => ['required']
    ]);

    $thread = Thread::create([
        'title' => $request->title,
        'description' => $request->description,
        'user_id' => auth()->user()->id,
        'channel_id' => $request->channel_id,
    ]);

    Subscribe::query()->create([
        'thread_id' => $thread->id,
        'user_id' => auth()->user()->id
    ]);
    return redirect('/');
}

you cant take store function to thread model when you save it it will come then you need to define thread end then you can call it.

CodePudding user response:

when you send it in form body laravel don't detect it as a route param. you can use this.

 Subscribe::query()->create([
        'thread_id'=>$request->thread_id,
        'user_id' => auth()->user()->id
    ]);
  • Related