mercredi 1 février 2017

Laravel5: How do I handle API post requests for an array of json objects?

Using Laravel 5.3 as backend API, I need to handle post requests for a json object and an array of objects.

For an object:

order = {table_id:1, food_id: 2};

I did the following.

public function postOrder(Request $request) {
    $order = new Order();
    $order->table_id  = $request->input('table_id');
    $order->food_id   = $request->input('food_id');
    $order->save();

    return response()->json(['order' => $order], 201);
}

And for an array of objects,

orders = [{table_id:1, food_id:2}, {table_id:3,food_id:4}];

I want to try:

public function postOrders(Request $request)
{
    foreach ($request as $anOrder)
    {
        $order = new Order();
        $order->table_id  = $anOrder->input('table_id');
        $order->food_id   = $anOrder->input('food_id');
        $order->save();
    }

    return response()->json(???, 201);
}

But that gives me an error while trying to post:

Call to undefined method Symfony\Component\HttpFoundation\ParameterBag::input()

How do I handle this?



via Chebli Mohamed

How to check if data exsists in the database so you can update it

This code above adds data to the database but it does not update if the data already exsists.

     public function updateStudentPastoral(){

   // Getting all post data
    $data = Input::all();


        $pCollection = new PastoralCollection();
        $pCollection->fill($data);
        $response = '';
        if ($pCollection->save())
            $response = 'success';
        else
            $response = 'error';

        return (json_encode($response));
 }

i want to check if the data already exists so i will update it in the database but if it does exist it will save in the database.



via Chebli Mohamed

How to user whereIn with <=?

Ok i have 6 of checkboxes in view:

<input type="checkbox" name="rooms[]" id="room1" value="1" />1
<input type="checkbox" name="rooms[]" id="room2" value="2" />2
<input type="checkbox" name="rooms[]" id="room3" value="3" />3
<input type="checkbox" name="rooms[]" id="room4" value="4" />4
<input type="checkbox" name="rooms[]" id="room5" value="5" />5
<input type="checkbox" name="rooms[]" id="room6" value="6" />6+

User can check all of them, but i have problem when user check for example first where value is 1 and last one where value is 6+. When user check that i want to get all records where value of room is 1 and value of room >= 6. Any suggestion how can i do that?

I tried this but this not working when user check last one.

  $value = 6;
    if (!in_array($value, $option)) {
          $query->whereIn('rooms',$option
         }
          else{
              $query->where('rooms','>=',$value);
         }



via Chebli Mohamed

how to create a blade template in laravel 5?

This is my first time in laravel and sorry for very newby question, I was very confuse between blade template and php file, because every time a create a template with .blade.php that file is cannot find by route, here is my code.

Route::get('/test', function (){
return view('testblade');});

that was the testblade.blade.php

but when I create a php file without .blade extension, it is functioning well

Route::get('/test', function (){
    return view('testphpalone');});

that was the testphpalone.php.

My worry is that if am I able to use the @yield(''), @extends('') and other blade syntax?

Thank you everyone in advance, any comment will be a great helps.



via Chebli Mohamed

Laravel Too many arguments, expected arguments "command" while scheduling

Ce résumé n'est pas disponible. Veuillez cliquer ici pour afficher l'article.

Laravel 5 get compiled Blade & JS view output

I have a view that outputs a string calculated by the view's Blade and some JavaScript.

I would like to know if there is a way to return this output to a variable in my controller? Much like the following in my controller:

$contents = view('pages.view', ['one' => 1, 'two' => 2, 'three' => 3])->render();
dd($contents);

Except render() returns the HTML and I want the compiled output.



via Chebli Mohamed

How can I return to a specified URL after login in Laravel?

I'm using Laravel 5.3 and want to return the user to a user-specified URL after login.

I am using a lot of JavaScript and want to return to a specific URL, that isn't the URL the user is trying to access, after they have logged in.

For example: /login?r=/come/here/after/login

I can pass this URL to the login screen, but I can't find a way to pass it through to the auth controller for redirection after login is successful.



via Chebli Mohamed