<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: anditsung</title>
    <description>The latest articles on DEV Community by anditsung (@anditsung).</description>
    <link>https://dev.to/anditsung</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F260668%2F328a6da0-9426-44b1-99d0-e1edc2c97ac8.png</url>
      <title>DEV Community: anditsung</title>
      <link>https://dev.to/anditsung</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anditsung"/>
    <language>en</language>
    <item>
      <title>remove already attach item on laravel nova</title>
      <dc:creator>anditsung</dc:creator>
      <pubDate>Fri, 12 Nov 2021 09:36:38 +0000</pubDate>
      <link>https://dev.to/anditsung/remove-already-attach-item-on-laravel-nova-4k3b</link>
      <guid>https://dev.to/anditsung/remove-already-attach-item-on-laravel-nova-4k3b</guid>
      <description>&lt;p&gt;on nova resource class check the relatableQuery function&lt;/p&gt;

&lt;p&gt;add this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; /**
     * Build a "relatable" query for the given resource.
     *
     * This query determines which instances of the model may be attached to other resources.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function relatableQuery(NovaRequest $request, $query)
    {
        if ($request-&amp;gt;is('nova-api/*/*/attach*/*')) {
            if (! $request-&amp;gt;current) {
                $query = self::removeAlreadyAttachItem($query, $request);
            }
            else if ($request-&amp;gt;current &amp;amp;&amp;amp; ! $request-&amp;gt;first) {
                $query = self::removeAlreadyAttachItem($query, $request);
            }
        }

        return parent::relatableQuery($request, $query);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;remove already attach item function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static function removeAlreadyAttachItem($query, $request)
    {
        $relationName = Str::plural(Str::snake(class_basename($query-&amp;gt;getModel())));

        $model = $request-&amp;gt;findResourceOrFail()-&amp;gt;model();

        $model-&amp;gt;load($relationName);

        $items = $model-&amp;gt;getRelation($relationName)-&amp;gt;map(function ($model) {
            return collect($model-&amp;gt;toArray())-&amp;gt;only('id')-&amp;gt;all();
        });

        return $query-&amp;gt;whereNotIn('id', $items);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>nova</category>
    </item>
    <item>
      <title>override delete button on laravel nova crud</title>
      <dc:creator>anditsung</dc:creator>
      <pubDate>Tue, 12 Nov 2019 07:47:06 +0000</pubDate>
      <link>https://dev.to/anditsung/override-delete-button-on-laravel-nova-crud-3hid</link>
      <guid>https://dev.to/anditsung/override-delete-button-on-laravel-nova-crud-3hid</guid>
      <description>&lt;p&gt;to override delete button on nova add this to model class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function delete()
{
    $this-&amp;gt;setKeysForSaveQuery($this-&amp;gt;newModelQuery())-&amp;gt;update(['active' =&amp;gt; false]);
    return true;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will make delete button to update active field instead of delete it.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>nova</category>
    </item>
    <item>
      <title>disable view on laravel nova attach</title>
      <dc:creator>anditsung</dc:creator>
      <pubDate>Tue, 12 Nov 2019 07:44:31 +0000</pubDate>
      <link>https://dev.to/anditsung/disable-view-on-laravel-nova-attach-oll</link>
      <guid>https://dev.to/anditsung/disable-view-on-laravel-nova-attach-oll</guid>
      <description>&lt;p&gt;to disable view on attach item add this to the resource that want to be disable to view&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function authorizedToView(Request $request)
{
    if($request-&amp;gt;query-&amp;gt;get("viaResource")) {
        return false; 
    }
    return true;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"$request-&amp;gt;query-&amp;gt;get("viaResource")" to find if the resource is accessed from other resource&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>nova</category>
    </item>
  </channel>
</rss>
