How to creat and Update Laravel Eloquent?

0 votes

This is my code:

<?php

$shopOwner = ShopMeta::where('shopId', '=', $theID)
    ->where('metadataKey', '=', 2001)->first();

if ($shopOwner == null) {
    // Insert new record into database
} else {
    // Update the existing record
}

What's the shorthand for inserting a new record or updating if it exists?

Oct 18 in PHP by kartik
• 35,300 points
21 views

1 answer to this question.

0 votes

Hello @kartik,

Here's a full example of what "lu cip" was talking about:

$user = User::firstOrNew(array('name' => Input::get('name')));
$user->foo = Input::get('foo');
$user->save();

Hope it works!!

answered Oct 18 by Niroj
• 76,540 points

Related Questions In PHP

0 votes
1 answer

How to set a default attribute value for a Laravel / Eloquent model?

Hello @kartik, This is what I'm doing now: protected ...READ MORE

answered Nov 8 in PHP by Niroj
• 76,540 points
26 views
0 votes
1 answer

How to route GET and POST for same pattern in Laravel?

Hello @kartik, You could try the following: Route::controller('login','AuthController'); Then in ...READ MORE

answered 1 day ago in PHP by Niroj
• 76,540 points
8 views
0 votes
2 answers
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13 in PHP by manish
248 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,441 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
4,142 views
0 votes
1 answer

How to get the Last Inserted Id Using Laravel Eloquent?

Hello @kartik, Firstly create an object, Then set ...READ MORE

answered Jul 21 in PHP by Niroj
• 76,540 points
1,556 views
0 votes
1 answer

How to include External CSS and JS file in Laravel?

Hello @kartik, The right way is this one: <script ...READ MORE

answered Oct 27 in PHP by Niroj
• 76,540 points
287 views