0

Looking for best practices, given these 3 tables:

Task (id, title, description) User (id, name) User_task (user_id, task_id)

My class/Object User has a function addToCompletedTasks(Task task) to add a task to his list of completed task (that adds the task into the table User_task).

I'm looking for best practice to send my completed task FROM my view TO my controller. Right now I'm sending an ID but I'm wondering if it's possible to send the object so I don't have to instantiate the task in my controller to add it to the completedtasks list.

public function insertCompletedTask(Request $request)
{
        $task_id = $request->input('task_id');

        $user = \Auth::user();

        $task   = Task::whereId($task_id)->first();
        $update = $user->assignCompletedTask($task);
}

3 Answers 3

1

You have to send the ID via a route

Route::get('/your-url/{id}', 'YourController@addCompletedTasks');

Then you need to have this function declared in your User model:

public function tasks(){
    return $this->belongsToMany('Task');
}

And then in your controller have this function:

public function addCompletedTasks($id) {
     // if you want to you can also just do 
     Auth::user()->tasks()->attach($id);
}

If you want to send it via POST in the request and not via the ID in the URL your function would need to be like that:

public function addCompletedTasks(Request $request) {
  Auth::user()->tasks()->attach($request->get('id'));
}

In either case you can't send a full php object or you will have to serialize it and deserialize it, easier to just send the ID.

But in my opinion you should check first if a task is related to the id first. But if you have this control on the database level it's ok.

Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly what I am doing but I'm wondering if it is the best way to do it as in order to instantiate the Task object I need to query the database, and then another query to add it to the list of completed task. In terms of performance, inserting a record into the table User_task would be quicker.
@Andrew if you don't want to catch the full object first you can directly do the attach method then. Look my edited answer :)
exactly what I was looking for! By only sending the id to the attach function it makes all sense now :) Many thanks!
0

If you return a view you can send back data to the view like this:

return view('view-name',array('variable_view' => $variable));

You will use the key from the array as variable in the view. and the value from the array ($variable) is the variable from the controller. So in your view you would type $variable_view and it would use the $variable, and so you can send the task object to the view.

In my opinion I would use as little PHP as possible in the view.

1 Comment

Sorry I may have not explained my problem properly. I know how to pass the object to my view. However I'm looking to pass my object FROM my view TO the controller. So the contrary, it's working with an id but I'm wondering if I shouldn't pass the full object if it is possible to reduce the nb of queries after.
0

First make a form with hidden field in your blade

  <form id="sendData" method="POST" action="display_policy">
      @csrf
      <input type="hidden" name="sendOBJ">
  </form>

the js code would be like this

function policy_view(obj) {
console.log(obj);
$("[name=sendOBJ]").val(JSON.stringify(obj));
$("#sendData").submit();

}

in your for loop, you pass your object to this js function that does is paste your object to a hidden field and submits the form. then you can get that data with request in controller like this

public function display_policy(Request $request){
 return   $arrayy = json_decode($request->sendOBJ,false);}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.