Posts

Showing posts with the label laravel-5

Multilevel relationship whereHas on eloquent Model in Laravel

Multilevel relationship whereHas on eloquent Model in Laravel I'd like to find Project::with('tasks.tags')->get(); Project::with('tasks.tags')->get(); where only projects with a particular id of tag return in the result set. For ex. I'd like to find a project with tasks and tasks with tags with only id of 1. In other words, filter the tasks return inside of the Project - Task relationship. I have tried various ways but have failed so far. I have tried: $project = Project::with('tasks.tags')->whereHas('tasks', function($query){ $query->whereHas('tags', function($query) { $query->where('id', 1); }); })->get(); And: $project = Project::with('tasks.tags')->whereHas('tasks', function($query){ $query->whereHas('tags', function($query) { $query->where('tag_id', 1); }); })->get(); This is how the re...

Laravel: How to enable stacktrace error on PhpUnit

Laravel: How to enable stacktrace error on PhpUnit I have a fresh installation of laravel 5.4 I've tried to modify the default test just to see a failing test. tests/ExampleTest.php class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this->get('/ooops'); $response->assertStatus(200); } } I was expecting to see more detailed error like no route has been found or defined etc, but instead just this error saying no route has been found or defined Time: 1.13 seconds, Memory: 8.00MB There was 1 failure: 1) TestsFeatureExampleTest::testBasicTest Expected status code 200 but received 404. Failed asserting that false is true. /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:51 /var/www/tests/Feature/ExampleTest.php:21 Its really hard to do TDD without meaningful error (yeah I know 404 in this case is enough, bu...

Remove time() string from filenames in Laravel when showing

Remove time() string from filenames in Laravel when showing I am using time() function to add time string to the file names like this and storing them in the database $filename = time() . $file->getClientOriginalName(); But How Can I remove this string when I show the files to the user and when the user downloads the file. I am using Laravel by the way. 1 Answer 1 The timestamp is 10 digits I believe, so doing: substr($filename,10): Would trim it from the beginning of the filename. When downloading a file, you can give the file name which is displayed/suggested to the user like this: return response()->download($filename, substr($filename, 0, 10), $headers); substr($filename, 0, 10): this has removed the file name except the time() string but using this substr($filename, 10) has solved my problem. and I wanna ask how could I make users download the file. I mean how...

Laravel: Sending Mail with Queue ignores Locale

Laravel: Sending Mail with Queue ignores Locale My Email template looks like this: @component('mail::message') # {{ $helloUser }} @lang('welcome.message') This App::setLocale('de); $activeMail = new AppMailRegisterActivate($user); Mail::to($user)->send($activeMail); will send an mail with German text. However, when I use a queue App::setLocale('de); $activeMail = new AppMailRegisterActivate($user); Mail::to($user)->queue($activeMail); The mail is send in English, which is the default language of my app. How can I send a message in German with the queue without changing the default language? 2 Answers 2 In Laravel 5.6. the Mailable class has gotten a locale method to care for this: Mailable locale $activeMail = new AppMailRegisterActivate($user)->locale('tr'); Mail::to($user)->queue($activeMail); For Laravel < 5.6 you have to save the text in the ma...

Laravel: Reactivating listeners after flushEventListeners()

Laravel: Reactivating listeners after flushEventListeners() I have some functionality that is being enacted by my model's event handler that I need to prevent from firing under a specific circumstance. protected static function bootLogSubject() { static::created(function ($subject) { $subject->logContent('create', Auth::user()); }); I am aware that I can use flushEventListeners() to stop the event firing. This works fine, but is it possible to switch the listeners back on again (unflush? bind?) once I have finished what I need to do? flushEventListeners() 1 Answer 1 This seems to work: Model::boot(); By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Laravel Passport - which grant to use for my users apps?

Laravel Passport - which grant to use for my users apps? I have application where users can register and add their own applications (websites, mobile applications). For each of these applications, I want to give access to my API and allow to get products form my database. For example: User X signup in my app, adds his blog user-X-blog.com and get access token. Next he can call to my API and get some products to show on his blog post. Which grant should I implement to make my API based on Laravel Passport safe and useful (each user application with its own token, no user login required to make api call, long-lived tokens)? Is it good idea to create for each user apps dedicated client and use client credentials grant? It doesn't look very safe for me (or maybe I'm wrong). 2 Answers 2 In your situation I think using Personal Access Tokens is the best option: Each unique user who signs up and ad...

Vue JS Chat App UI

Vue JS Chat App UI Good day everyone! I am developing a Chat App using Laravel + Laravel Echo + Pusher and Vue JS all the functionality are fine but I have a problem with the UI. If you have a new chat, your message will display on the right side and if someone sends a chat on the chatbox, his/her message will display to the left. I already made the UI {{message.user.firstname}} {{message.user.lastname}} 10:12 AM, Today {{message.chat}} </li> </ul> </div> {{message.user.firstname}} {{message.user.lastname}} 10:12 AM, Today {{message.chat}} </li> </ul> </div> </div> </div> whenever I make a message, all messages will display on the right, if someone sends a chat, all messages will display on the left. Does anyon...

How do I run PHPUnit in Laravel from my Windows Command prompt

How do I run PHPUnit in Laravel from my Windows Command prompt I have a Laravel app installed in my directory f:lara_app. I use PHP artisan serve to run the app. I have Laravel version 5.4.36 (via Composer Install) I am not trying my hand in using the PHP Unit to do testing. Inside f:/lara_app/tests/Unit/ExampleTest.php, I have the following codes: namespace TestsUnit; use TestsTestCase; use IlluminateFoundationTestingDatabaseMigrations; use IlluminateFoundationTestingDatabaseTransactions; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $this->assertTrue(true); } public function myFirstTest() { $value = 'Hello World'; $this->assertTrue( 'Hello' === $value, 'Value should be Hello World'); } } I now try to run the test from my command prompt: f:lara_app> .vendor/phpunit/phpuni/phpunit But I'm getting the m...