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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kpSMYs
via IFTTT

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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jVxyAq
via IFTTT

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);
         }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jVCtSd
via IFTTT

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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kfGsr0
via IFTTT

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

This should be straigh forward buti don't know why it is not working . I am creating a command in laravel to send birtday email reminders on a user's birtday .

Everything works fine and the schedule function is triggered but comes with an error

 [Symfony\Component\Console\Exception\RuntimeException]
  Too many arguments, expected arguments "command".

This is my command

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\User;
class SendBirthdayReminderEmail extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email:birthday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Email users a birthday Reminder message';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

     $users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get();   
    foreach($users as $user) {   
        Mail::queue('emails.birthday', ['user' => $user], function ($mail) use ($user) {
            $mail->to($user['email'])
                ->from('info@XXXXXX.com', 'Company')
                ->subject('Happy Birthday!');
        });

     }

    $this->info('Birthday messages sent successfully!');

    }
}

And this is my kernel.php file

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
     Commands\SendBirthdayReminderEmail::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {


        $schedule->command('email:birthday')->->dailyAt('13:00')->timezone('Africa/Dar_es_Salaam');
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

Any help will be appreciated . Thanks :-)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kqEJRT
via IFTTT

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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kTPNnF
via IFTTT

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.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jV8vgR
via IFTTT