mardi 26 septembre 2017

Return back from edit form after several updates in Laravel 5.4

I'm trying to add functionality to my edit form. Perhaps not a best practice but...

I want to keep the original url for several updates. When a user first lands on edit form (from list or single view) I want a Cancel button that redirects user back to.. "referrer". After an update I redirect back to edit form with success message. I want to hide the Cancel button and show Back button. User can edit multiple times.

I tried to pass the original url through Request, but that didn't end well. Now I'm trying with storing the url in session. Ifs work, but the session item value gets overwritten in second iteration.

Perhaps I'm using the session the wrong way? Any idea? Perhaps another "better" way?

The code in my edit.blade

@if(url()->current() != URL::previous())
    @if(Session::has('myreferrer'))
        
    @endif
    
    <a href="" class="btn">Cancel</a>
@else
<a href="" class="btn">Back</a>
@endif



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

How to inspect the current Named Route using Laravel 5.2

according to the Laravel 5.5 docs, there is a named() method for accessing route names:

if ($request->route()->named('profile')) {
    //
}

Inspecting the source, I learned that this named method simply fetches the 'as' property of the action object:

$this->action['as'] 

My problem is I'm stuck using Laravel 5.2, which doesn't have a named() method. I can't use route()->action['as'] in my blade template, because the actionobject is protected. Is there an equivalent getter method in 5.2, to check the name of the current route? I want to flow control in my blade.php file like this:

@if(route()->action['as'] == 'blog.edit')
    //
@endif

Maybe I missed it, but I didn't see anything in the Laravel 5.2 docs: http://ift.tt/2xDOCSx

I succeeded in checking the route using

@if(request()->is('blog/add'))
    //
@endif

But that is using the route URI. I prefer to use the route name instead as it's less clunky



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

Getting Unauthorized error trying to send email with Sparkpost (Laravel 5.4)

I've been following the Laravel Mail docs http://ift.tt/2o4SyIX to send email via a SparkPost account. When I try and send I'm getting

ClientException
Client error: `POST http://ift.tt/1xBMTcd` resulted in a `401 Unauthorized` response:
{"errors": [ {"message": "Unauthorized."} ]}

As per the docs I've got the sparkpost key in config/services.php and though the docs don't mention anything about it (why not!?) a bit of Googling convinced me to set the mail driver etc. in my .env file. I then have a web route to check the config being used,

Route::get('test', function()
{
    dd(Config::get('mail'));
});

which gives

array:9 [▼
  "driver" => "sparkpost"
  "host" => "smtp.sparkpostmail.com"
  "port" => "587"
  "from" => array:2 [▶]
  "encryption" => "tls"
  "username" => "my-sparkpost-account-login-email-address"
  "password" => "my-sparkpost-account-login-password"
  "sendmail" => "/usr/sbin/sendmail -bs"
  "markdown" => array:2 [▶]
]

I actually want to use a mailable and views which I've set up, but just to test the sending I bypassed these and set the following route to test:

Route::get('/sparkpost', function () {
  Mail::send('emails.test', [], function ($message) {
    $message
      ->from('marketing@my-sparkpost-sending-domain.com', 'Me')
      ->to('my-email-address', 'My-Name')
      ->subject('A Test Email');
  });
});

which got the error at the top (testing with my mailables and views gave the exact same error which makes me think they are correctly set up - assuming the above test route is correct).

The on thing I'm wondering about is I'm testing on my local machine, so test URL is http://localhost/seg/public/sparkpost - I'm wondering if this is tripping SparkPost up (i.e. do I have to actually be sending from a server running at the same domain as the sending domain configured in sparkpost, or is it just the sending address in the email that has to match?)

Thanks for any suggestions!



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

Laravel 5 - Allowed memory size of 134217728 bytes exhausted (tried to allocate 60067000 bytes)

This question builds on my previous question.

Basically, we upload a base64 image that is 50MB, when we have a hard limit of 10MB per image. Anything greater than 10MB should be detected before the php.ini memory limit is hit, so we can return an image is too big error to the user.

I know this would work:

ini_set('memory_limit', '-1');

But is a bad way to handle the problem. The user can send a file that's 1GB+ and my server will choke. I am looking for a solution to detect that the file is over the size limit without increasing the memory_limit, but determining it is over the 10MB limit as soon as possible so we can return a graceful error.

The solution in the previous question does not ever get reached since the memory_limit gets hit and returns the log error before anything in the controller gets to run.

Any idea how to solve this?



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

Eager load related models in Laravel's Revisionable history

Using Laravel 5.4 and the VentureCraft/revisionable package.

I have 3 models: User, Company and Order.

The Order model implements the Revisionable trait:

namespace App\Models;

class Order extends Eloquent {
    use \Venturecraft\Revisionable\RevisionableTrait;

    // ...
}

The User model has a relationship with the Company model:

public function company() {
    return $this->belongsTo('App\Models\Company');
}

And I would like to eager load the company of a user from an order revision. Something like this

$order = Order::with('revisionHistory.user.company')->find(1);

foreach($order->revisionHistory as $revision) {
    $company = $revision->user->company;
}

I've tried overwriting various methods (e.g. revisionHistory and identifiableName) from the Revisionable trait without any luck.



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

Laravel Relationships: hasManyThrough, belongsTo, belongsToMany

Heyy, I have a Laravel project here, can u guys help me with this question about relationship?

I have the following database structure:

users
    id
    name
    email
    password

event
    id
    description
    city_id

block_range
    id
    event_id

user_block_ranges
    user_id
    block_range_id

Explanation

users: A normal user authentication table. (has a belongsToMany relationship with user_block_ranges)

event: Stores event info. (has a hasMany relationship with block_range)

block_range: Save blocks of time of event. (has a belongsTo relationship with event)

The real question is: how do I get all the events of the user? Through the user_block_ranges then block_range relationship? Maybe using hasManyThrough?

Thanks in advance.



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

Heroku error: git push heroku master

I am working with laravel and I am trying to work with Heroku however an error appears when I try to git push the code:

1) Install the Heroku CLI (DONE)

2) Execute...

$ heroku login

$ cd my-project/

$ git init

$ heroku git:remote -a jaquenews

$ git add .

$ git commit -am "make it better"

$ git push heroku master <--- PROBLEM :S



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