jeudi 1 décembre 2016

Laravel 5: How to use firstOrNew with additional relationship fields

I have a CMS that allows the user to save and create bike tours. Each bike tour also has categories, which are definined using Laravel's Many to Many relationship utilising an intermediary pivot table. At the point of saving a tour, we don't know if the tour is an existing one being edited, or a new one.

I think I should be using Laravel's firstOrNew method for saving the tour, and the sync method for saving categories. However, all the tutorials very simplistically just give the example of passing a single object to the function like so:

$tour = Tour::firstOrNew($attributes);

But what happens when my $attributes also contains extra stuff, like the categories which are linked to a relationship table, and which I will need to save in the next step? For example this very good tutorial gives the following example:

$categories = [7, 12, 52, 77];

$tour = Tour::find(2);

$tour->categories()->sync($categories);

But what happens if the category data is bundled with the data for the rest of the tour, and instead of using find I need to use firstOrNew to create the tour? Should I keep the categories in the $attributes while I instantiate the tour, then run the sync, then unset them before saving the tour, or...? Is there a better way to achieve this?



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

Controller didn't get more arguments laravel

I'm trying to send two arguments and one request to controller with post request. This is the code where I send the arguments:

<form class="form-horizontal" name="form1"  method="post" action="" role="form" enctype="multipart/form-data">

And this is the controller where I'm sending the arguments:

    public function storeShipment(Request $request, $number, $category_name){

           $category = Category::where('category_name', $category_name)->first();
           $user = Auth::user();

           $item = new Item([

           'id' => $user->id,
           'category_id' => $category->id,
           'unq' => $number,
           'fullname' => $request->input('name'),
           ]);

           $item->save();
   }

But when I open the view it gives me error

ErrorException in NewShipmentController.php line 53:
Missing argument 3 for App\Http\Controllers\NewShipmentController::storeShipment()

Any ideas?



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

Laravel delete() static call

I got this error

Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically, assuming $this from incompatible context

here is code in my controller

$file_db = new File();
$file_db = $file_db->where('id',$id)->find($id);
$file_db = $file_db->delete();

Can someone explain what I am doing wrong and how to call it correctly.



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

laravel 5 redirect to previous page after sign in

laravel 5 redirect to previous page after sign in.

When I am hitting a url manually if its not logged in its redrecting to login page but after login it should redirect to url which entered but in my case its going to home page.

Code:

public function index()
    { 

            // Is the user logged in?
        if (Auth::check()) 
        { 
            return Redirect::route('dashboard');
        }

        // Show the page
        return View('auth.login');
    }


public function getSignin()
    { 

            // Is the user logged in?
        if (Auth::check()) 
        { 
            return Redirect::route('dashboard');
        }

        // Show the page
        return View('auth.login');
    }



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

Laravel deleting a file NotFoundHttpException

this is my form in my view

         {!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
         {!! Form::token() !!}
         {!! Form::submit('Delete') !!}
         {!! Form::close() !!}

controller in which i delete file from database and the original file

public function destroyFile($file_name, $id)
{
    File::findOrFail($id)->delete();
    $file_path = storage_path('documents').'/'.$file_name;
    $destinationPath = $file_path; File::delete($file_path);
    return redirect('/documents');
}

This is the route

Route::delete('documents/{file}/{id}','FilesController@destroyFile');

And when i press submit button I get NotFoundHttpException



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

Catching Eloquent Errors

I perform various eloquent statements throughout my app. eg:

User::find($id)->update($data);

Or:

User::create($data);

Or:

User::paginate(10);

I presume the best way to tackle potential errors is to use try/catch? If so, what should I be catching for eloquent errors?



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

PHPunit tests for socialite login with google

I am using socialite in my application so that users can login with their google accounts. I want to write a functional test for this. So far I have got up to clicking the button to login,

public function testAuth()
    {
        $this->visit('/admin')
            ->see('The Admin Dashboard')
            ->click('Login with Google')

            //tests to login should go here

            ->seePageIs('/admin/dashboard');
    }

from here a method calls the socialite google driver, checks if the google email is corporate, and creates an account for the user in the database who can then access the dashboard. The problem is even if I mock a user, the moment the tests hit this ->click('Login with Google') it sends a request to google and returns a request failed error



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